Tuesday, April 30, 2013

Send Email Through Office 365 Outlook Account Using ASP.NET


Add the Below reference.

using System.Net.Mail;

Below is the Email sending code.

  private void SendMail()
  {
      try
        {
            //You will be using System.Net.Mail
            //Your SmtpClient gets set to the SMTP Settings you found in the "POP, IMAP, and SMTP access" section from Web Outlook
            SmtpClient mailClient = new SmtpClient("smtp.office365.com");

            //Your Port gets set to what you found in the "POP, IMAP, and SMTP access" section from Web Outlook
            mailClient.Port = 587;

            //Set EnableSsl to true so you can connect via TLS
            mailClient.EnableSsl = true;

            //Your network credentials are going to be the Office 365 email address and the password
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential("YOURMAILl@YOUROFFICE.COM", "PASSWORD");

            mailClient.Credentials = cred;

            MailMessage message = new MailMessage();

            //This DOES NOT work
            message.From = new MailAddress("YOURMAILl@YOUROFFICE.COM");

            //This DOES work
            message.From = new MailAddress("YOURMAILl@YOUROFFICE.COM", "Chamara");
            message.To.Add("YOURMAILl@YOUROFFICE.COM");

            message.Subject = "Test";
            message.Body = "http://itzonesl.blogspot.com";
            mailClient.Send(message);

       }
       catch (Exception ex)
        {
            throw ex;
        }

  }



10 comments:
Write comments
Recommended Posts × +