Send SMTP email using System.Net.Mail via Exchange Online (Office 365)

后端 未结 8 571
悲&欢浪女
悲&欢浪女 2020-11-29 23:34

We are testing the new Office 365 beta, and i have a mail account on the Exchange Online service. Now I\'m trying to connect a LOB application that can send smtp emails from

8条回答
  •  长情又很酷
    2020-11-30 00:11

    Fixed a few typos in the working code above:

    MailMessage msg = new MailMessage();
    msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
    msg.From = new MailAddress("you@yourdomain.com", "You");
    msg.Subject = "This is a Test Mail";
    msg.Body = "This is a test message using Exchange OnLine";
    msg.IsBodyHtml = true;
    
    SmtpClient client = new SmtpClient();
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
    client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
    client.Host = "smtp.office365.com";
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    try
    {
        client.Send(msg);
        lblText.Text = "Message Sent Succesfully";
    }
    catch (Exception ex)
    {
        lblText.Text = ex.ToString();
    }
    

    I have two web applications using the above code and both work fine without any trouble.

提交回复
热议问题