Trouble Sending Email To Office 365

2019-08-08 04:22发布

问题:

We are moving from a private Exchange server to Office 365. Previously, the following code worked:

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("Old Server Name");
smtp.Send(email);

From everything I've read, I should be able to do something like the following:

    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;
    client.Send(msg);

However, if I try to use port 587, I simply get a timeout. I cannot telnet to port 587 either (telnet smtp.office365.com 587). My own Outlook client is set up for https (port 443), and I can telnet to that port - but my code just returns "SMTP server returned an invalid response". Here's some sample code that I've tried:

SmtpClient smtp = new SmtpClient("smtp.office365.com");
smtp.Port = 443;

Followed by any combination of:

            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("ID", "password");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            smtp.TargetName = "STARTTLS/smtp.office365.com";

Followed by smtp.Send(email);

回答1:

Check your firewall it might block the port.



标签: c# .net smtp