Reputation: 39025
When i write a mail service with C#,the mail send success.But the receiver can not receive the mail.I read the log it like:
Exception :System.Net.Mail.SmtpException: Send mail failed. ---> System.FormatException: An invalid character was found in header value
at System.Net.Mime.HeaderCollection.Set(String name, String value)
at System.Net.Mail.Message.PrepareHeaders(Boolean sendEnvelope)
at System.Net.Mail.Message.BeginSend(BaseWriter writer, Boolean sendEnvelope, AsyncCallback callback, Object state)
at System.Net.Mail.MailMessage.BeginSend(BaseWriter writer, Boolean sendEnvelope, AsyncCallback callback, Object state)
at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result)
How to solve this problem?
This is my code:
public static bool Send(MailAddress Messagefrom, string MessageTo, string MessageSubject, string MessageBody)
{
MailMessage message = new MailMessage();
message.From = Messagefrom;
message.To.Add(MessageTo);
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
MailHelper mh = new MailHelper();
SmtpClient sc = mh.setSmtpClient("smtp.163.com", 25);
sc.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
try
{
sc.SendAsync(message, message);
}
catch (Exception)
{
return false;
}
return true;
}
The messagebody is:
[SysAdmin 发送给您的,〈建模流程〉 建物理表 业务|EcaClient:Cmd=OpenTask&TaskGuid={3866BB12-0DFB-4713-9C89-043C228FDA3A}&UserID=271&Sender=1]
Is this encoding problem?I test it:When the body is very simple,,it work normal.But when become complex like this,it can not received.How to fix it?
Upvotes: 0
Views: 751
Reputation: 1549
try this
System.Net.Mail.MailMessage mail_msg = new System.Net.Mail.MailMessage();
System.Net.Mail.MailAddress fromAdd = new System.Net.Mail.MailAddress("from");
mail_msg.From = fromAdd;
mail_msg.Subject = "subject";
mail_msg.IsBodyHtml = true;
mail_msg.Body = "message";
mail_msg.Bcc.Add("bcc"); //optional
mail_msg.CC.Add("cc"); //optional
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName ="email";
NetworkCred.Password = "password";
smtp.Credentials = NetworkCred;
smtp.Host = "host"; // EX. smtp.gmail.com
smtp.Port = 25;
smtp.Timeout = 500000;
smtp.EnableSsl = true;
smtp.Send(mail_msg);
Upvotes: 0
Reputation: 15951
Try to set the BodyEncoding to utf8 (the default is us-ascii):
message.BodyEncoding = System.Text.Encoding.UTF8;
Upvotes: 3