smtp有时无法在c#中发送邮件

时间:2018-04-04 12:54:50

标签: c# asp.net email smtp

我已经就这个主题提出了一些问题。所有答案都与发送电子邮件始终失败有关。就我而言,它有时只会出现异常消息:

  

SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应是:5.7.57 SMTP;客户端未通过身份验证,无法在MAIL FROM ...

期间发送匿名邮件

如果我第二次尝试它可行。我正在使用以下配置。

using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("emailid", "displayname");
                    mail.To.Add("TOAddress");
                    mail.Subject = subject1;
                    mail.Body = body1;
                    mail.IsBodyHtml = true;

                    using (SmtpClient smtp = new SmtpClient("Outlook.office365.com", 587))
                    {
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("emailid", "password");
                        smtp.EnableSsl = true;
                        smtp.Send(mail);
                    }
                }

检查了类似的问题here,但解决方案无效。

1 个答案:

答案 0 :(得分:1)

试试这个(第二个答案):Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

我使用此代码发送电子邮件:

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("sender@gmail.com");
            msg.To.Add("receiver@gmail");
            msg.Subject = "Hello";
            msg.Body = "Test";

            SmtpClient smt = new SmtpClient();
            smt.Host = "smtp.gmail.com";
            System.Net.NetworkCredential ntcd = new NetworkCredential();
            ntcd.UserName = "sender@gmail.com";
            ntcd.Password = "senderPassword";
            smt.Credentials = ntcd;
            smt.EnableSsl = true;
            smt.Port = 587;
            smt.Send(msg);

同时检查您的病毒扫描程序是否阻止您的电子邮件发送。