【问题标题】:Sending email using Hotmail SMTP C# works locally, but doesn't on live server使用 Hotmail SMTP C# 发送电子邮件在本地工作,但不在实时服务器上
【发布时间】:2020-11-11 07:34:46
【问题描述】:

我有一个 asp.net Web API,用于将电子邮件发送到另一个电子邮件 (gmail),另一个前端项目调用该 API,当后端和前端项目在本地时,它可以完美地发送电子邮件,但它不适用于实时托管 (SmarterAsp)。

这是我的大火和代码:

    //This method only sends email as per the data sent
    [System.Web.Http.HttpPost]
    public EmailResponseModel SendEmail()
    {
   
        NetworkCredential basicCredential =
        new NetworkCredential("mysenderemail@hotmail.com", "mysenderemailpassword");
        MailMessage ProviderMail = new MailMessage();

        ProviderMail.From = new MailAddress("mysenderemail@hotmail.com");
        ProviderMail.To.Add("myemail@gmail.com");


        ProviderMail.Subject = "Title";
        ProviderMail.IsBodyHtml = true;
        ProviderMail.Body = "Body";

        SmtpClient smtp = new SmtpClient();
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Host = "smtp.office365.com";
        smtp.Credentials = basicCredential;
        smtp.EnableSsl = true;


        try
        {
            smtp.Send(ProviderMail);
            return Response;
        }
        catch (Exception ex)
        {
            return Response;
        }
        finally
        {
            smtp.Dispose();
        }
    }

这里是我从 API 得到的错误:

System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP

我尝试做很多事情,例如:

  • 尝试了这些主机 smtp.live.com、smtp.office365.com、smtp-mail.live.com 等
  • 尝试了其他端口,例如 25
  • 试图将此 UseDefaultCredentials 设置为 false 和 true
  • 确保用户名和密码正确
  • 从 SMPT Config 代码的顺序确定

我在这方面花了几个小时,但我没有找到任何解决方案!我需要任何见解。

【问题讨论】:

  • 我在使用 gmail 时看到了这个错误,在 smtp.office365.com 中是否有用于发送电子邮件的“应用程序密码”,其中用户密码不起作用?
  • 您可以选择使用 SmarterAsp SMTP 服务器吗?
  • 您好像在使用Gmail服务,对吗?如果是,您需要确保您的 MX 记录已指向 Gmail 服务。
  • 谢谢,@pete-S- 我通过使用 SmarterAsp 电子邮件尝试了您的想法,它奏效了,将您的答案添加为单独的帖子,我将使其成为适合我的案例的正确答案

标签: c# asp.net email smtp hotmail


【解决方案1】:

您是否可以选择使用 SmarterAsp SMTP 服务器?

来自他们的网站:

<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Net.Mail" %> 
<script runat="server"> 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
 
        Dim strFrom = "postmaster@yourdomain.com"  ''IMPORTANT: This must be same as your smtp authentication address.
        Dim strTo = "postmaster@yourdomain.com" 
        Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo)) 
        MailMsg.BodyEncoding = Encoding.Default 
        MailMsg.Subject = "This is a test" 
        MailMsg.Body = "This is a sample message using SMTP authentication" 
        MailMsg.Priority = MailPriority.High 
        MailMsg.IsBodyHtml = True 
        'Smtpclient to send the mail message 
 
        Dim SmtpMail As New SmtpClient 
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("postmaster@mydoamin.com", "password") 

''IMPORANT:  Your smtp login email MUST be same as your FROM address.
 
        SmtpMail.Host = "mail.yourdomain.com" 
        SmtpMail.UseDefaultCredentials = False 
        SmtpMail.Credentials = basicAuthenticationInfo
        SmtpMail.Port = 25;    //alternative port number is 8889
        SmtpMail.EnableSsl = false;
 
        SmtpMail.Send(MailMsg) 
        lblMessage.Text = "Mail Sent"     
    End Sub 
</script> 
<html> 
<body> 
    <form runat="server"> 
        <asp:Label id="lblMessage" runat="server"></asp:Label> 
    </form> 
</body> 
</html> 

【讨论】:

    【解决方案2】:

    好的,问题是 Hotmail 检测到我的帐户有异常行为,这是因为我尝试在本地和实时托管上发送电子邮件,所以如果尝试发送电子邮件的人是,他们阻止了我并向我发送电子邮件不管是不是我,一旦我确认是我,一切都会好起来的。

    【讨论】:

      猜你喜欢
      • 2014-03-26
      • 2019-09-20
      • 2016-08-25
      • 2017-05-19
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      相关资源
      最近更新 更多