Pages

Tuesday, January 13, 2009

Email people!

Send Email Explanation:
This sends email using gmail service.

Code:
This is the function that can email files. I haven't tested it. I had this working in last workterm personal project but today made changes so that I take information from function parameters instead of my program controls.
files array contain the location of all files that should be attached. Can pass empty string array

private void SendFile(string fromAddress, string subject, string body, string
toAddress, string [] files)
{
try
{
MailMessage message = new
MailMessage();
message.IsBodyHtml = false;
BWProgress.ReportProgress(10);
//from address
MailAddress
mailAddress = new MailAddress(fromAddress,fromAddress);
message.From =
mailAddress;

//attaches files
for (int i = 0; i < files.Length;
i++)
{
Attachment attachment = new Attachment(files[i]);
message.Attachments.Add(attachment);
}

//email, body, subject
message.Subject = subject;
message.Body = body;
message.To.Add(new
MailAddress(toAddress));
//send email
string hostAddress =
"smtp.gmail.com";
int port = 587;
SmtpClient client = new
SmtpClient(hostAddress, port);
client.Credentials = new
NetworkCredential(TBEmail.Text, TBPassword.Text);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
}
catch (Exception err)
{
throw err;
}
}

No comments:

Post a Comment