Sending Email from C#

Pure code is here. You can just copy and use it, and you can understand easily from the comments.

string to = "someone@hotmail.com"; // here the username can be any address // this is the address that the reciever will reply to when he/she hits the Reply button // this doesn't have to be the same as the credential address below. string from = "username@gmail.com"; string subject = "My mail from C#"; string body = "<a href=\"http://www.microsoft.com\"> Click here </a><br/><img src=\"http://t0.gstatic.com/images?q=tbn:2LUYVF-U1KxK2M:http://img.bollywoodsargam.com/albumsbolly/Priyanka_Chopra/Priyanka_Chopra_In_Barsaat_Movie_Stills_002_18_08_2005.jpg\"/>"; System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(from, to, subject, body); msg.IsBodyHtml = true; // adding attachement System.Net.Mail.Attachment at = new System.Net.Mail.Attachment(@"C:\Users\Thurupathan\Desktop\myfile.zip"); msg.Attachments.Add(at); // sending from the gmail account // for the hotmail / live / outlook => smtp.live.com port => 587 System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587); // provide credentials System.Net.NetworkCredential gmailAuthentication = new System.Net.NetworkCredential("username@gmail.com", "password"); smtp.Credentials = gmailAuthentication; // SSL enable based on the smtp.EnableSsl = true; try { smtp.Send(msg); } catch (Exception ex) { Console.WriteLine(ex.Message); }

Advertisements

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: