当加密连接为TLS时,邮件发送失败,通过c#

时间:2017-05-26 10:48:15

标签: c# asp.net outlook smtp

我在我的代码中设置邮件发送如下

    NetworkCredential nc = new NetworkCredential(ConfigurationManager.AppSettings["EmailId"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
    MailMessage mm = new MailMessage();
    mm.From = new MailAddress(SendEmailaddress);
    mm.Body = "Test Mail";
    mm.IsBodyHtml = true;
    mm.BodyEncoding = System.Text.Encoding.UTF8;
    mm.To.Add(ToEmailAddress);
    mm.Subject = "Test";
    SmtpClient sp = new SmtpClient();
    sp.UseDefaultCredentials = false;
    sp.Credentials = nc;
    sp.EnableSsl = true;
    sp.DeliveryMethod = SmtpDeliveryMethod.Network;
    sp.Port = 587
    sp.Host = ConfigurationManager.AppSettings["SMTP"].ToString();
    sp.Send(mm);

发送邮件时会抛出错误。如果我使用TLS作为加密连接将这些设置配置在同一台PC上,则邮件发送有效。 我已经检查了很多帖子,建议应该为TLS设置EnableSsl = true,但它对我不起作用。它会抛出错误

  

交易失败。服务器响应是:5.7.1   :收件人地址被拒绝:访问被拒绝   在System.Net.Mail.RecipientCommand.CheckResponse(SmtpStatusCode   statusCode,String response)at   System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,   MailAddressCollection收件人,String deliveryNotify,   SmtpFailedRecipientException&安培;例外)   System.Net.Mail.SmtpClient.Send(MailMessage消息)

由于找不到解决方案,我遇到了障碍。有没有需要在服务器上完成的设置?

1 个答案:

答案 0 :(得分:1)

protected void SendAlertEmail(string smtpserver, string smtpport, string smtpuser, string smtppass, int ssl, int auth, string subject, string from, string to, string body)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(SplitEmailStrging(from), HttpUtility.HtmlDecode(Request.Form["senderName"]));
                string emails = to;

                if (emails.Contains(","))
                {
                    string[] emailslist = Regex.Split(emails, @",");
                    foreach (string email in emailslist)
                    {
                        mail.To.Add(SplitEmailStrging(email));
                    }

                }
                else
                {
                    if (emails.Contains("<"))
                    {

                        mail.To.Add(SplitEmailStrging(emails));
                        // Response.Write(SplitEmailStrging(emails));
                    }
                    else
                    {

                        mail.To.Add(emails);
                        // Response.Write(emails);
                    }

                }

                mail.Subject = subject;
                mail.IsBodyHtml = true;
                mail.Body = HttpUtility.HtmlDecode(body);
                SmtpClient client = new SmtpClient(smtpserver);

                if (int.Parse(smtpport) == 465)
                {
                    client.Port = 25;
                }
                else
                {
                    client.Port = int.Parse(smtpport);
                }

                if (ssl == 1)
                {
                    client.EnableSsl = true;
                }
                else
                {
                    client.EnableSsl = false;
                }

                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(smtpuser, smtppass);
                client.UseDefaultCredentials = false;
                client.Credentials = credentials;
                client.Send(mail);
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.InnerException.Message);
                Response.End();
            }


        }