加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Outlook.com SMTP发送电子邮件

发布时间:2020-12-15 05:39:58 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Outlook.com smtp支持发送自动电子邮件.但是我得到以下异常: System.Net.Mail.SmtpException: Failure sending mail. --- System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly
我正在尝试使用Outlook.com smtp支持发送自动电子邮件.但是我得到以下异常:
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.

我的代码:

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

解决方法

我知道这是一个非常古老的问题,我甚至可能无法提供帮助,但是当我尝试使用C#发送电子邮件时,我遇到了类似的问题.

结果我用这个允许我发送电子邮件:

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

这可能对你没用了,但万一有人偶然发现这个问题,至少有一个答案,其中有一个工作代码可以作为修复!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读