Send email using Outlook.com SMTP

一个人想着一个人 提交于 2019-12-29 08:43:29

问题


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;
        }
    }

回答1:


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!



来源:https://stackoverflow.com/questions/18862932/send-email-using-outlook-com-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!