关于ASP.NET中电子邮件形式不符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于ASP.NET中电子邮件形式不符相关的知识,希望对你有一定的参考价值。

“/Web”应用程序中的服务器错误。
--------------------------------------------------------------------------------

指定字符串与电子邮件地址所要求的形式不符。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.FormatException: 指定字符串与电子邮件地址所要求的形式不符。

源错误:

行 23: SmtpClient mailClient = new SmtpClient(BalloonShopConfiguration.MailServer);
行 24: // 创建email 消息
行 25: MailMessage mailMessage = new MailMessage(from,to, subject, body);
行 26:
行 27: // 发送email 消息

源文件: E:\sunjiasi\繁星\我的新项目\BalloonShopModels\Utilities.cs 行: 25

堆栈跟踪:

[FormatException: 指定字符串与电子邮件地址所要求的形式不符。]
System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +1343
System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset) +21
System.Net.Mail.MailAddressCollection.ParseValue(String addresses) +38
System.Net.Mail.MailAddressCollection.Add(String addresses) +39
System.Net.Mail.Message..ctor(String from, String to) +173
System.Net.Mail.MailMessage..ctor(String from, String to) +111
System.Net.Mail.MailMessage..ctor(String from, String to, String subject, String body) +14
BalloonShop.Models.Utilities.SendMail(String from, String to, String subject, String body) in E:\sunjiasi\繁星\我的新项目\BalloonShopModels\Utilities.cs:25
BalloonShop.Models.Utilities.LogError(Exception ex) in E:\sunjiasi\繁星\我的新项目\BalloonShopModels\Utilities.cs:51
BalloonShop.BLL.GenericDataAccess.ExecuteSelectCommand(DbCommand command) in E:\sunjiasi\繁星\我的新项目\BalloonShopBLL\GenericDataAccess.cs:32
BalloonShop.BLL.CatalogAccess.GetDepartments() in E:\sunjiasi\繁星\我的新项目\BalloonShopBLL\CatalogAccess.cs:57
UserControls_DepartmentsList.Page_Load(Object sender, EventArgs e) in e:\sunjiasi\繁星\我的新项目\Web\UserControls\DepartmentsList.ascx.cs:19
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Control.LoadRecursive() +132
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745

关键是解决问题,别给我贴烂东西
if (BalloonShopConfiguration.EnableErrorLogEmail)

string from = "fanxingwz@126.com";
string to = BalloonShopConfiguration.ErrorLogEmail;
string subject = "错误报告!";//BalloonShopConfiguration.SiteName +
string body = errorMessage;
SendMail(from, to, subject, body);

异常详细信息: System.FormatException: 指定字符串与电子邮件地址所要求的形式不符。

这么明显的提示:格式错误,建议你用正则表达式验证你的email地址
这种错误要跟踪堆栈干嘛?debug下就行了,肯定是你传的参数(from,to)有问题!

给你个表达式:
验证Email地址:“^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$”

还有那最后一句话真的很欠鄙视!
参考技术A MailMessage mailMessage = new MailMessage(from,to, subject, body);

把你的 from, to 的值贴出来看看
参考技术B ...看不懂

C#和ASP.NET通过Gmail账户发送邮件的代码

开发之余,把开发过程中比较常用的代码片段备份一次,下面的代码段是关于C#和ASP.NET通过Gmail账户发送邮件的代码,希望对大家有所用途。

using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
"smtp.gmail.com");
myMail.Fields.Add
"465");
myMail.Fields.Add
"2");

myMail.Fields.Add
myMail.Fields.Add
pGmailEmail);
myMail.Fields.Add
pGmailPassword);
myMail.Fields.Add
"true");
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}

System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}

 

EmailLib.SendEmail.SendEmail("your_gmail_id",
"your_gmail_password",
"[email protected]",
"This is email subject" ,
"This is email body",
Web.Mail.MailFormat.Text,
"Physical path to your Attachment")





以上是关于关于ASP.NET中电子邮件形式不符的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET 的电子邮件地址验证

是否可以在 MVC for Asp.NET 中使用 Outlook 电子邮件组作为角色?

从 C# ASP.NET 中的数据集进行邮件合并

Asp.Net Identity 2 无法重置密码明文?

C#和ASP.NET通过Gmail账户发送邮件的代码

使用 ASP.Net 从 HTML 发送电子邮件