简体   繁体   English

部署后,SMTP“发送邮件失败”异常与本地主机配合使用

[英]SMTP “Failure sending mail” exception after deploy, works well with localhost

Using the tutorial in this link How to Implement Contact Us Page in ASP.NET MVC (ASP.NET 5 ) . 在此链接中使用教程如何在ASP.NET MVC(ASP.NET 5)中实现“联系我们”页面 I managed to get my application send email to my gmail account. 我设法让我的应用程序将电子邮件发送到我的Gmail帐户。 However, it is failing after publish and gives "Failure sending mail" error. 但是,它在发布后失败,并显示“发送邮件失败”错误。 I have done the possible fixes which you can see below but didn't help. 我已经做了可能的修复,您可以在下面看到,但没有帮助。 Thank you. 谢谢。

Here is my HomeController code: 这是我的HomeController代码:

public ActionResult Contact(Contact c)
    {
        if (ModelState.IsValid)
        {
            try
            {
                MailMessage msg = new MailMessage();
                StringBuilder sb = new StringBuilder();
                msg.From = new MailAddress("example1@gmail.com");
                msg.To.Add("example1@gmail.com");
                msg.Subject = c.Subject;
                //msg.IsBodyHtml = false;
                sb.Append("<b>First name: </b>" + c.FirstName + "<br/><br/>");
                sb.Append("<b>Last name: </b>" + c.LastName + "<br/><br/>");
                sb.Append("<b>Email: </b>" + c.Email + "<br/><br/>");
                sb.Append("<b>Phone: </b>" + c.PhoneNumber + "<br/><br/>");
                sb.Append("<b>Message: </b>" + c.Message + "<br/><br/>");
                msg.Body = sb.ToString();
                msg.IsBodyHtml = true;
                // We use gmail as our smtp client
                SmtpClient smtpClient = new SmtpClient(); 
                smtpClient.Send(msg);
                msg.Dispose();
                ModelState.Clear();
                ViewBag.ContactMessage = "Your Message has been sent. Thank you for Contacting us. ";
            }
            catch (Exception ex)
            {
                ModelState.Clear();
                ViewBag.ContactMessage = $" Sorry we are facing the following Problem: {ex.Message}";
            }
        }
        return View();
    }

And my Web.config file: 和我的Web.config文件:

<system.net>
    <mailSettings>
        <smtp deliveryMethode="Network">
            <network host="smtp.gmail.com" port"587" userName="***" password="***" enableSsl="true"/>
        </smtp>
    </mailSettings>
</system.net>

If I understand your problem correctly then here is your scenario. 如果我正确理解了您的问题,那么这就是您的情况。

  1. When you use same credential locally it is working. 当您在本地使用相同的凭据时,它将起作用。
  2. Same credential after publish it is not working. 发布后相同的凭证不起作用。 Is this issue of google ? 这是Google问题吗? Yes. 是。 Reason: It seems that your publish server is located some where else. 原因:您的发布服务器似乎位于其他位置。 So when you put site over there gmail assume that somebody try to access gmail from another location. 因此,当您将网站放在那儿gmail时,假设有人尝试从另一个位置访问gmail。

    Solution: Your account will receive one email stating that is that really you that trying to access account ? 解决方案:您的帐户将收到一封电子邮件,说明您实际上是在尝试访问该帐户吗? Allow permission over there. 允许那里的权限。 It might works. 它可能会起作用。 As I have faced same issue and it solve after that. 由于我遇到了同样的问题,之后就解决了。

不是一个精确的解决方案,但是。我更改了域电子邮件,将SMTP端口更改为80,并删除了enableSsl,现在它可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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