什么时候MAILMESSAGE空构造工作? [英] When does the empty MailMessage constructor work?
问题描述
我们有一个Asp.Net的解决方案,一直在生产中使用System.Net.Mail.MailMessage与空的构造超过2年:
We have an Asp.Net solution that's been in production for over 2 years using System.Net.Mail.MailMessage with the empty constructor:
using (MailMessage _mailMessage = new MailMessage()) // Exception thrown here
{
_mailMessage.From = new MailAddress(sFrom); // Setting additional properties - never gets here
_mailMessage.Body = sBody;
_mailMessage.Subject = sSubject;
_mailMessage.IsBodyHtml = true;
昨天我们有一个活的网站报告异常:指定的字符串不是一个电子邮件地址,所需表格。我们通过添加所需的节点到web.config文件固定它。
Yesterday we had a live site report the exception: The specified string is not in the required form for an email address. We fixed it by adding the required node to the web.config file.
现在的问题是:为什么这个曾经工作?或者莫非曾经工作过?
The question is: Why did this ever work? or Could it have ever worked?
错误:
指定的字符串不是一个电子邮件地址所要求的形式。
在System.Net.Mail.MailAddressParser.ReadCfwsAndThrowIfIncomplete(字符串数据的Int32指数)
在System.Net.Mail.MailAddressParser.ParseDomain(字符串数据的Int32&安培;指数)
在System.Net.Mail.MailAddressParser.ParseAddress(字符串数据,布尔expectMultipleAddresses,的Int32&安培;指数)
在System.Net.Mail.MailAddressParser.ParseAddress(字符串数据)
在System.Net.Mail.MailAddress..ctor(字符串的地址,字符串显示名,编码displayNameEncoding)
在System.Net.Mail.MailMessage..ctor()
Error: The specified string is not in the form required for an e-mail address. at System.Net.Mail.MailAddressParser.ReadCfwsAndThrowIfIncomplete(String data, Int32 index) at System.Net.Mail.MailAddressParser.ParseDomain(String data, Int32& index) at System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index) at System.Net.Mail.MailAddressParser.ParseAddress(String data) at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) at System.Net.Mail.MailMessage..ctor()
感谢
EDITS
- 我们从.NET 3.5升级到4.0上个月!
- 添加堆栈跟踪
- 7个月后,该错误只发生在某些服务器上。为什么呢?
推荐答案
一个有点晚了答案。但我有同样的问题。你需要,如果你使用的是来自Web.Config中的SMTP设置,从在你的web.config设置。勾选此 MSDN链接了解更多详情
A little bit late for the Answers. But I had the same problem. You need to set the from
in your Web.Config if you are using smtp setting from Web.Config. Check this MSDN Link for more details
<mailSettings>
<smtp from="xxxxx@gmail.com">// This is important when constructing new mail message. Make sure from is an email address.
<network host="smtp.gmail.com" password="yourpassword" userName="xxxxx@gmail.com" port="587" />
</smtp>
</mailSettings>
如果您不使用 SMTPCLIENT
从Web.Config中则完全删除从web.config中的从
属性,构建自己的smtpclient在code这样的
If you are not using SMTPCLIENT
from Web.Config then completely remove the from
attribute from Web.Config and construct your own smtpclient in code like this
var fromAddress = new MailAddress("xxxxxx@gmail.com", "Mr XXXXX");
string fromPassword = "yourpassword";
message.From = fromAddress;
SmtpClient client = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
这篇关于什么时候MAILMESSAGE空构造工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!