Отправка электронных писем из приложения MVC, размещенного в Azure, не работает - PullRequest
0 голосов
/ 02 ноября 2018

Я просмотрел много сообщений, но, похоже, ни одна из них не работает.

У меня есть приложение MVC, размещенное в Azure в качестве службы приложений, и отправка писем не работает. Это работает на моем местном.

Мои данные 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, но электронные письма не приходят.

Есть определенный порт, который нужно использовать, или я что-то не так делаю?

Спасибо за любую помощь заранее!

...