简体   繁体   中英

Send email using Outlook.com SMTP

I am trying to send an automated email using Outlook.com smtp support. However I am get the following exception:

System.Net.Mail.SmtpException: Failure sending mail.  
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.  
---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host" Exception while sending email.

My code:

    public bool SendEmail(MailMessage msg)
    {
        try
        {
            SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com")
            {
                UseDefaultCredentials = false,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential("userAddress", "userPassword"),
                Port = 587,
                EnableSsl = true,
            };
            smtpClient.Send(msg);
            msg.Dispose();
            smtpClient.Dispose();
            return true;
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.ToString());
            return false;
        }
    }

I know that this is an extremely old question and I might not even be able to help, however I had a similar problem when I tried to send an email using C#.

As a result I used this which allowed me to send the emails:

string _sender = "";
        string _password = "";

        SmtpClient client = new SmtpClient("smtp-mail.outlook.com");

        client.Port = 587;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        System.Net.NetworkCredential credentials =
            new System.Net.NetworkCredential(_sender, _password);
        client.EnableSsl = true;
        client.Credentials = credentials;

        MailMessage message = new MailMessage(_sender, "recipient of email");
        message.Subject = "";
        message.Body = "";
        client.Send(message);

This probably will be of no use to you anymore, but in case anyone stumbles onto this question at least there is an answer which has working code acting as a fix!

I had this exact issue today on an old Windows 7 laptop. I also noticed that while the Internet was working fine through my browser, I could not ping ANYTHING from outside the router via the Command Prompt, therefore any attempts at SMTP were bound to fail. The problem was IPv6 was set to default on the network card. If you get this issue, check by pinging www.google.com : ping www.google.com If you get "Unreachable", force it to ping using IPv4 with ping -4 www.google.com . If you get a reply now, you can likely solve your problem by disabling IPv6 via your network card properties:

在此处输入图像描述

Just uncheck the box next to IPv6 and reboot your machine, go back to the Command Prompt and retry a straight "default" IPv ping: ping www.google.com . If it replies, your machine is now using IPv4 as default, so now re-try your SMTP e-mail code. I used the exact code in this article and it started working. Note that you can also apply the same logic for pinging with whatever your SMTP mail server address is eg. smtp.live.com which can help you test that you can communicate with the SMTP server regardless of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM