简体   繁体   中英

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 ) . I managed to get my application send email to my gmail account. 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:

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:

<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 ? 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.

    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,现在它可以工作了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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