Friday 10 August 2012

sending email in c#

Following self explanatory code snippets will demonstrate how to use .Net Mail to send Mail in C#. First of all, add “Using Directive” to .Net Mail and .Net Mime. The later one is for Mail attachment.

using System.Net.Mail;
using System.Net.Mime;
Let's start with SmtpClient. Here, I have created SmtpClient Object using my Gmail Account.

SmtpClient smtpServer = new SmtpClient();

smtpServer.Credentials = new System.Net.NetworkCredential("khan.rahim@gmail.com", "xxxxxx");
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;
This following code snippet shows how to prepare your MailMessage.

MailMessage mailMessage = new MailMessage();

mailMessage.From = new MailAddress("khan.rahim@gmail.com", "A Rahim Khan", System.Text.Encoding.UTF8);
mailMessage.To.Add("rahim767@yahoo.com");

mailMessage.Subject = "Mail Using C# Code";
mailMessage.Body = "I have Used .Net Mail to send this Mail";

mailMessage.Attachments.Add(new Attachment("FILE PATH");

mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mailMessage.ReplyTo = new MailAddress("khan.rahim@gmail.com");

Finally, send your MailMessage by SmtpClient in this way.

smtpServer.Send(mailMessage);
 

 More Details :  Live Training in jaipur

 

No comments:

Post a Comment