System.Net.Mail没有在生产中发送 [英] System.Net.Mail doesn't send in production

查看:70
本文介绍了System.Net.Mail没有在生产中发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站中实施忘记密码"方法.它在调试中完美地工作.使用相同的代码和数据库,但是发布到我们的Web服务器时,它在尝试发送消息时失败.

I am trying to implement a "forgot password" method into my site. It works perfectly in debug. Using the same code and db, but published to our web server it fails when it tries to send the message.

我得到的错误是:

There was an error sending you an email.
The specified string is not in the form required for an e-mail address.

该电子邮件有效,所以我不知道为什么会失败.

The email is valid, so I have no idea why it is failing.

因为它是一个实时环境,所以我无法单步执行代码以确切了解失败的原因和原因.我实现了db日志记录,因此我可以看到它在失败之前能走多远,并且可以成功执行到此为止的所有代码:

Because it is a live environment I cannot step through the code to see exactly where and why it is failing. I implemented db logging so I can see how far it gets before it fails and it successfully executes all code up to this point:

 var smtp = new SmtpClient
 {
     Host = host,
     Port = port,
     EnableSsl = ssl,
     DeliveryMethod = SmtpDeliveryMethod.Network,
     UseDefaultCredentials = false,
     Credentials = new NetworkCredential(fromAddress.Address, fromPw)
 };
 using (var message = new MailMessage()
 {
     Subject = subject,
     Body = body,
     IsBodyHtml = ishtml,
     From = fromAddress
 })

 {
     foreach (MailAddress t in toCol)
     { message.To.Add(t); }
     foreach (MailAddress c in ccCol)
     { message.CC.Add(c); }
     foreach (MailAddress b in bccCol)
     { message.Bcc.Add(b); }
     smtp.Send(message);
 }

它永远不会到达下一个数据库日志,因此它必须在这里失败.在我的测试中,对于to,我只有一个电子邮件地址,而对于bcccc,则没有电子邮件地址.逐步调试时,它会正确加载单个电子邮件地址,而不会为ccbcc加载任何电子邮件地址.我不知道它认为无效的电子邮件地址是什么.

It never gets to the next db logging so it has to be failing here. In my test I have exactly one email address for the to and none for bcc and cc. When stepping through in debug it correctly loads the single email address and doesn't load any for cc and bcc. I have no idea what it is considering to be an invalid email address.

我们使用Google Apps作为我们的邮件服务器,因此我的工作站和服务器都必须连接.我正在使用以下内容:

We use Google Apps as our mail server so both my workstation and the server have to connect. I am using the following:

  • 主机:smtp.gmail.com
  • 端口:587
  • EnableSsl:是
  • 凭据:可在调试中使用的有效用户名和密码

结合您的一些建议.
fromAddress的设置是使用db中的值进行的,如下所示:

EDIT 2: To incorporate some of the suggestions from you.
The fromAddress is set earlier using values from the db like this:

DataTable ts = DAL.Notification.GetNotificationSettings();
var fromEmail = ts.Rows[0]["fromadr"].ToString().Trim();
var fromName = ts.Rows[0]["fromname"].ToString().Trim();
var host = ts.Rows[0]["server"].ToString().Trim();
var port = Convert.ToInt32(ts.Rows[0]["smtpport"]);
var ssl = Convert.ToBoolean(ts.Rows[0]["is_ssl"]);
var ishtml = Convert.ToBoolean(ts.Rows[0]["is_html"]);
var bodyTemplate = ts.Rows[0]["bodyTemplate"].ToString();
body = bodyTemplate.Replace("{CONTENT}", body).Replace("{emailFooter}","");// Needs to use the Global emailFooter resource string

var fromAddress = new MailAddress(fromEmail, fromName);

我什至尝试对发件人地址进行这样的硬编码:

I have even tried hard coding the from address like this:

message.From = new MailAddress("websystem@mydomain.com");

我仍然收到错误消息,并且在定义消息时仍然失败.

I still get the error and it still fails when defining the message.

关于如何查找和解决问题的其他建议?

Any other suggestions on how to find and fix the problem?

答案

我没有像这样在web.config中定义默认的发件人地址:

I did not define the default from address in the web.config like this:

    <system.net>
     <mailSettings>
       <smtp from="email@yourdomain.com">
         <network host="smtp.yourdomain.com"/>
       </smtp>
     </mailSettings>   </system.net>

因此,在我定义正确的from地址之前,它在var message = new MailMessage()处失败.

So it failed at var message = new MailMessage() before I could define the correct from address.

我需要实现var message = new MailMessage(From,To)或在web.config中提供默认的from地址(这就是我所做的事情)

I either needed to implement var message = new MailMessage(From,To) or provide a default from address in web.config (which is what I did)

推荐答案

此错误可能由两件事引起:

This error can be caused by two things:

  1. 您使用的其中一个电子邮件地址(用于message.Tomessage.CCmessage.Bcc)无效,即未遵循所需的someuser@somedomain.xxx格式.

  1. One of the email addresses your using (for message.To, message.CC or message.Bcc) is invalid, i.e. it doesn't follow the required format of someuser@somedomain.xxx.

Web.Config 中配置的发件人地址无效:

<system.net>
  <mailSettings>
    <smtp from="invalid@@email">
      <network host="smtp.gmail.com"/>
    </smtp>
  </mailSettings>
</system.net>

这篇关于System.Net.Mail没有在生产中发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆