Some time ago using the System.Net.Mail.MailMessage class to write a mail bulk send the program, in debugging and testing encountered some of the problems and share with you, hope to be helpful to everyone.
There are a number of ways to send messages using the MailMessage class, but I recommend sending messages asynchronously, especially bulk mail, because when a program submits multiple messages to a mail server, it synchronizes by waiting until the last message is submitted and sending back, before sending the next one. If the network is slow to connect to the mail server, there is no timely response, then the program has been in the waiting state, will affect the subsequent send, and sometimes will produce duplicate submissions, so that the recipient received more than repeated messages.
The following is an instance of sending asynchronously.
Using System;
Using System. ComponentModel;
Using System. Net;
Using System. Net. Mail;
Namespace Mailsendexample
{
Send asynchronously, get send status
static bool Mailsent = FALSE;
public static void Sendcompletedcallback (object sender, AsyncCompletedEventArgs e)
{
if (E. Cancelled)
{
Mailsent = false;
}
if (E. Error!= null)
{
Mailsent = false;
}
Else
{
Mailsent = true;
}
}
public static bool SendMail (string fromemail, String fromname, String reemail, String toemail, String mailtitle, String mailbody)
{
Format email, recipient supports multiple emails separated by half-width commas
Toemail = Toemail. Replace (";", ",");
Toemail = Toemail. Replace (";", ",");
Toemail = Toemail. Replace (",", ",");
MailMessage mail = new MailMessage ();
Try
{
sender, name of sender
Mail. from = new MailAddress (Fromemail, fromname);
Reply person, reply name
Mail. ReplyTo = new MailAddress (Reemail, fromname);
Recipient
Mail. To. ADD (Toemail);
Message priority
Mail. Priority = mailpriority. Normal;
Set up an HTML message
Mail. Isbodyhtml = true;
Title
Mail. Subject = Mailtitle;
Content
Mail. BODY = Mailbody;
}
catch (Exception ex)
{
Error logging
Logger. WriteLine ("Sendmail_errormail:" + ex.) Message + "|" + Toemail);
return false;
}
Try
{
Mail server
String smtpserver = "mail.aaaa.com";
Send Account
String smtpauthusername = "bbb@aaaa.com";
Password to send account
String Smtpauthpassword = "123456";
Define transport protocol
SmtpClient smtp = new SmtpClient (smtpserver);
Set up certified senders
Smtp. Credentials = new NetworkCredential (Smtpauthusername, Smtpauthpassword);
Asynchronous send completes get send status
Smtp. sendcompleted + = new Sendcompletedeventhandler (sendcompletedcallback);
Debugging
Logger.writeline ("SendMail:" + mail.) to + "|" + Mail. from);
Send asynchronously
Smtp. SendAsync (mail, String. Empty);
return mailsent;
}
catch (Exception ex)
{
Logger. WriteLine ("Sendmail_send:" + ex.) Message + "|" + SmtpServer);
return false;
}
}
}
}
Attention to the following question, pay attention to the debugging procedure
Debugging
Logger.writeline ("SendMail:" + mail.) to + "|" + Mail. from);
Since the addition of this sentence to write the log method of debugging, the message sent has failed, this problem plagued for a long time, and finally intercepted the error message to find the reason, the error is the "message header found invalid characters", feeling very strange, this problem plagued for a long time, Finally found the relevant information on the Internet to know is a Microsoft bug, you can refer to the following links, the above has a very detailed description.
Http://hi.baidu.com/shuwang/blog/item/f463db1bb4c789d2ad6e75dc.html