使用 MVC5 ASP.NET 和机架空间发送电子邮件



我使用教程在我的 MVC5 应用程序联系表单中设置电子邮件并通过电子邮件发送。我不知所措。我已经尝试了几种不同的选择,但我只是错过了一些东西。非常感谢帮助!

我的邮件主机是机架空间,它们的设置 http://www.rackspace.com/apps/support/portal/1088 在这里可用。

一切都很好,除了我不断收到smpt的此错误。SendFinalMail 函数中的 Send(MailInfo)。

SmtpException occurred
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
Additional information: The operation has timed out.

根据评论添加了堆栈跟踪(如果您需要更多,请告诉我):

System.Net.Mail.SmtpException occurred
  _HResult=-2146233088
  _message=The operation has timed out.
  HResult=-2146233088
  IsTransient=false
  Message=The operation has timed out.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
  InnerException: 

这是我的代码:

Web.config
  <appSettings>
    <!--EMAIL SERVICES-->
    <add key="FromAddress" value="myfrom@address" />
    <add key="UserID" value="myfrom@address" />
    <add key="Password" value="myPassword" />
    <add key="SMTPPort" value="465" />
    <add key="SmtpClient" value="secure.emailsrvr.com" />
    <add key="EnableSSL" value="Yes" />
    <add key="UnobtrusiveJavaScriptEnabled" value ="true"/>
  </appSettings>

模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
namespace SandBox.MailManager
{
    class MailManager
    {
        #region Private for email
        private string FromAddress { get; set; }
        private string EmailHost { get; set; }
        private string Pwd { get; set; }
        private string UserID { get; set; }
        private string SMTPPort { get; set; }
        private Boolean bEnableSSL { get; set; }

        #endregion

#region Default Functions for Mailing
            public MailManager()
            {
                FromAddress = System.Configuration.ConfigurationManager.AppSettings["FromAddress"];
                UserID = System.Configuration.ConfigurationManager.AppSettings.Get("UserID");
                SMTPPort = System.Configuration.ConfigurationManager.AppSettings.Get("SMTPPort");
                Pwd = System.Configuration.ConfigurationManager.AppSettings.Get("Password");
                EmailHost = System.Configuration.ConfigurationManager.AppSettings.Get("SmtpClient");
                if (System.Configuration.ConfigurationManager.AppSettings.Get("EnableSSL").ToUpper() == "YES")
                {
                    bEnableSSL = true;
                }
                else
                {
                    bEnableSSL = false;
                }
            }

private bool SendFinalMail(MailMessage MailInfo)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = EmailHost;
                smtp.Port = Convert.ToInt16(SMTPPort);
                smtp.Credentials = new System.Net.NetworkCredential(UserID, Pwd);
                smtp.EnableSsl = bEnableSSL;
                //smtp.Send(MailInfo);
                System.Threading.Thread emailThread;
                emailThread = new System.Threading.Thread(delegate()
                {
                    smtp.Send(MailInfo);
                });
                emailThread.IsBackground = true;
                emailThread.Start();
                return true;
            }
            catch (Exception e)
    {
        Console.WriteLine(e);
        return false;
        }
        }
        #endregion

 public bool SendEnquiryEmail(string ToUserEmail, string htmlBody)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(FromAddress);
                mail.To.Add(ToUserEmail);
                mail.CC.Add("another@email");
                mail.Subject = "Thank you. Our team will be in touch shortly";
                mail.Body = htmlBody;
                mail.IsBodyHtml = true;
                return this.SendFinalMail(mail);
            }
            catch
            {
                return false;
            }
        }
    }
}

控制器

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Contact(ContactModel Contact)
        {
            if (ModelState.IsValid) { 
                try
                {
                    MailManager.MailManager oMail = new MailManager.MailManager();
                    string htmlBody = "<html><body>Hi, ";
                    htmlBody += "<p>We've receive your message and we will in touch shortly to responded to your message,</p>";
                    htmlBody += "<br /><br /><p>You can email us directly at <a href='#'>.</p>";
                    htmlBody += "<br /><br /><p>The message we have received from you is:</p>";
                    htmlBody += "<br /><br /><p>" + Contact.Message + "</p>";
                    htmlBody += "<p><br /> Regards,</p> </body></html>";
                    bool SendEmail = oMail.SendEnquiryEmail(Contact.Email, htmlBody);
                    if (SendEmail == true)
                    {
                        return View();
                    }
                    else { 
                        return View();
                    }
                }
                catch (Exception)
                {
                    return View();
                }
            }
            return View("Model not Valid");
        }

我已经知道了答案。答案是:因为System.Net.Mail不支持"隐式"SSL,只支持"显式"SSL。

我通过端口 465 使用 SSL,它导致了这些问题。我更改了主机,端口和启用SSL,使其连接不安全,并且工作正常。

从那以后,我发现,如果我使用 TLS 端口 465 并启用 SSL,它就不会从 ssl 端口 587 发送,而是可以工作。这篇文章为我提供了答案

通过 Gmail 在 .NET 中发送电子邮件

最新更新