Exception when creating MailMessage

Posted by Julien Poulin on Stack Overflow See other posts from Stack Overflow or by Julien Poulin
Published on 2010-03-22T21:11:52Z Indexed on 2010/03/23 0:21 UTC
Read the original article Hit count: 968

Filed under:
|
|

I'm getting a FormatException telling me the address '[email protected]' is invalid (I'm guessing because of the '-'). Here is the code I'm using:

var md = new MailDefinition();
md.BodyFileName = physicalPath;
md.Subject = subject;
md.From = from;
md.CC = cc;
md.IsBodyHtml = true;

MailMessage mailMessage = md.CreateMailMessage(to, replacements, owner);

Is there a bug in the .Net e-mail parser? I am sure this address is valid since it belongs to a friend and I have no problem sending and receiving his e-mails with Outlook.

Is there a way to fix this?

EDIT: Here is the stacktrace:

[FormatException: La chaîne spécifiée n'est pas de la forme requise pour une adresse de messagerie.]
System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +1141755
System.Net.Mail.MailAddress.ParseValue(String address) +240
System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +85
System.Net.Mail.Message..ctor(String from, String to) +122
System.Net.Mail.MailMessage..ctor(String from, String to) +114
System.Web.UI.WebControls.MailDefinition.CreateMailMessage(String recipients, IDictionary replacements, String body, Control owner) +1366
System.Web.UI.WebControls.MailDefinition.CreateMailMessage(String recipients, IDictionary replacements, Control owner) +290
WebNotificationManager.CreateMessage(Page owner, MessageTemplate messageTemplate, ListDictionary replacements, String to, String from, String cc, String subject) in c:\dev\SoccerMania\SoccerMania\App_Code\WebNotificationManager.cs:41
WebNotificationManager.SendMessage(Page owner, MessageTemplate messageTemplate, ListDictionary replacements, String to, String subject) in c:\dev\SoccerMania\SoccerMania\App_Code\WebNotificationManager.cs:22
inscription.bInscription_Click(Object sender, EventArgs e) in c:\dev\SoccerMania\SoccerMania\Inscription.aspx.cs:54
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

© Stack Overflow or respective owner

Related posts about c#

Related posts about email