Social Icons

Pages

Tuesday, July 10, 2012

How to send an Email using C#


using System.Net.Mail;
using System.Net;

void SendEmail()
        {
            try
           {
           MailMessage msg = new MailMessage();
           msg.From = new MailAddress(SenderAddress);
           msg.To.Add(new MailAddress(ReceiverAddress));
           msg.Body = Bodymessage;
           msg.Subject = subject;
           msg.IsBodyHtml = true;
           System.Net.Mail.Attachment attachment;
           attachment = new System.Net.Mail.Attachment(imagepath);
           msg.Attachments.Add(attachment);
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.Credentials = new NetworkCredential(NetworkCredential mailid, NetworkCredential password);
           smtp.EnableSsl = true;
           smtp.Send(msg);
           }
           catch (Exception)
           {
            MessageBox.Show("Error in sending the mail", "Error");

           }
        }

No comments:

Post a Comment