65.9K
CodeProject is changing. Read more.
Home

Send email with smtp authentication using ASP.NET 3.5

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Oct 11, 2013

CPOL
viewsIcon

26945

Following Codes demonstrates how to send an email with SMTP Authentication using ASP.NET 3.5using System.Net.Mail        MailMessage msgMail =

Following Codes demonstrates how to send an email with SMTP Authentication using ASP.NET 3.5

using System.Net.Mail

        MailMessage msgMail = new MailMessage();

        MailMessage myMessage = new MailMessage();
        myMessage.From = new MailAddress("sender's email","sender`s name and surname");
        myMessage.To.Add("recipient's email");
        myMessage.Subject = "Subject";
        myMessage.IsBodyHtml = true;

        myMessage.Body = "Message Body";


        SmtpClient mySmtpClient = new SmtpClient();
        System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("email", "password");
        mySmtpClient.Host = "your smtp host address";
        mySmtpClient.UseDefaultCredentials = false;
        mySmtpClient.Credentials = myCredential;
        mySmtpClient.ServicePoint.MaxIdleTime = 1;

        mySmtpClient.Send(myMessage);
        myMessage.Dispose();


Note : You can avoid mySmtpClient.ServicePoint.MaxIdleTime, as it is to force the SmtpClient to send mail immediately with the .Send method.