String Mailcontent = " Email content " ;
Mailmessage msg = New System. net. Mail. mailmessage ();
MSG. to. Add ( " * ***** @ Gmail.com " ); // Recipient
// Sender Information
MSG. From = New Mailaddress ( " * ***** @ Gmail.com " , " Sender name " , System. Text. encoding. utf8 );
MSG. Subject = " This is the test email " ; // Email Subject
MSG. subjectencoding = System. Text. encoding. utf8; // Title Encoding
MSG. Body = Mailcontent; // Email Subject
MSG. bodyencoding = System. Text. encoding. utf8;
MSG. isbodyhtml = True ; // HTML?
MSG. Priority = Mailpriority. High; // Priority
Smtpclient Client = New Smtpclient ();
// Set the Gmail mailbox and password
Client. Credentials = New System. net. networkcredential ( " Account @ gmail.com " , " Password " );
Client. Port = 587 ;
Client. Host = " Smtp.gmail.com " ;
Client. enablessl = True ;
Object Userstate = MSG;
Try
{
Client. Send (MSG );
MessageBox. Show ( " Sent successfully " );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message, " An error occurred while sending the email. " );
}
The focus here is on the location of email authentication. You must not write an error. Otherwise, the email cannot be sent. This seems to be a success, but you will find that the sameProgramIs sending successful on some machines, and sending always fails on some machines, which is the cause of the system? It does not seem to be because some of the same systems have successfully sent and some failed to send. That is, why is the framework? Neither does it seem.
Here is a very confusing phenomenon. Some of the same systems and frameworks can still send successfully, while others cannot. The specific cause of the failure is that the mail header encoding is incorrect. So we can remove the subject without writing it. Can we use the default one? The same is true.
Summary:
1. It is very easy to use smtpclient to send emails. As long as the mailmessage object and smtpclient are created correctly, it is easy to send emails.
2. If the email server requires authentication [it seems that authentication is required now, unless configured on its own], pay attention to the following sentence:
Smtpclient. Credentials = new system. net. networkcredential ("account @ gmail.com", "password ");
3. The machine name cannot use Chinese characters.
From: http://www.cnblogs.com/xieqixiang/archive/2009/08/05/1539611.html