从Azure托管的MVC应用程序发送电子邮件不起作用

时间:2018-11-02 08:56:29

标签: asp.net-mvc azure smtp smtpclient

我已经浏览了很多帖子,但是似乎都没有用。

我在Azure中托管了一个MVC应用程序作为应用程序服务,并且邮件发送无法正常进行。它适用于我的本地计算机。

我将SMTP详细信息存储在Web.Config中:

Web.Config

 <appSettings>
    <!--To Send Mail-->
    <add key="MailServerSMTP" value="mail.appname.com" />
    <add key="MailServerSMTP_UserName" value="alerts@appname.com" />
    <add key="MailServerSMTP_Password" value="Password" />
  </appSettings>

以下是我的发送功能:

电子邮件发送功能

public void SendMessage(string subject, string messageBody, string fromAddress, string toAddress, string ccAddress, string sFileName, string sFileName2)
        {

            try
            {

                MailMessage message = new MailMessage();
                SmtpClient client = new SmtpClient();

                //Thread T1 = new Thread(delegate ()
                //{
                    //Set the sender's address
                    message.From = new MailAddress(fromAddress);

                    //Allow multiple "To" addresses to be separated by a semi-colon
                    if ((toAddress.Trim().Length > 0))
                    {
                        foreach (string addr in toAddress.Split(';'))
                        {
                            message.To.Add(new MailAddress(addr));
                        }
                    }

                    //Allow multiple "Cc" addresses to be separated by a semi-colon
                    if ((ccAddress.Trim().Length > 0))
                    {
                        foreach (string addr in ccAddress.Split(';'))
                        {
                            message.CC.Add(new MailAddress(addr));
                        }
                    }

                    //Set the subject and message body text
                    if (!string.IsNullOrEmpty(sFileName))
                    {
                        Attachment obAttachement = new Attachment(sFileName);
                        message.Attachments.Add(obAttachement);
                    }

                    if (!string.IsNullOrEmpty(sFileName2))
                    {
                        Attachment obAttachement = new Attachment(sFileName2);
                        message.Attachments.Add(obAttachement);
                    }

                    message.Subject = subject;
                    message.Body = messageBody;
                    message.IsBodyHtml = true;

                    string path = System.AppDomain.CurrentDomain.BaseDirectory;
                    path = path.Replace("bin\\Debug\\", "Content\\img");

                    //if (path.Substring(path.Length - 6) != "Images")
                    //{
                    //    path = path + "Images";
                    //}

                    if (path.GetLast(6) != "Content\\img")
                    {
                        path = path + "Content\\img";
                    }

                    Attachment ImageAttachment = new Attachment(path + "\\SystemicLogic_Transparent.png");

                    // Set the ContentId of the attachment, used in body HTML
                    ImageAttachment.ContentId = "SystemicLogic_Transparent.png";

                    // Add an image as file attachment
                    message.Attachments.Add(ImageAttachment);

                    message.Body = messageBody;



                    //Set the SMTP server to be used to send the message
                    //client.Host = "smtp.jumpstartcom.co.za"
                    string sSMTP_Username = ConfigurationManager.AppSettings["MailServerSMTP_UserName"].ToString();
                    string sSMTP_Password = ConfigurationManager.AppSettings["MailServerSMTP_Password"].ToString();
                    string appname = ConfigurationManager.AppSettings["MailServerSMTP"];

                    client.Credentials = new System.Net.NetworkCredential(sSMTP_Username, sSMTP_Password);

                    client.Host = appname;
                    client.Port = 587;
                //client.EnableSsl = true;

                //Send the e-mail message
                //client.SendAsync(message, null);
                client.Send(message);
                //});

                //T1.Start();


            }
            catch (Exception ex)
            {

            }
        }

我尝试启用SSL并使用端口25。尝试将端口设置为465和587,但是电子邮件仍未通过。

是否需要使用特定的端口,或者我做错了什么?

感谢您的任何帮助!

0 个答案:

没有答案