Showing posts with label send email via external account. Show all posts
Showing posts with label send email via external account. Show all posts

Tuesday, January 25, 2011

Send Emails using C#.Net

Following is the code extract to send emails via C#.Net using an external SMTP server e.g. via a Gmail account or something similar.


System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
NetworkCredential loginInfo = new NetworkCredential("user", "password");
string hostname = "hostname";
int port = 99;

client.Host = hostname;
client.Port = port;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
msg.Subject = emailSubject;
msg.Body = emailMessage;
msg.IsBodyHtml = true;
client.Send(msg);

Remember to alter the hostname, port and credentials accordingly.