[C #] Simple email sending

Source: Internet
Author: User
Tags mailmessage smtpclient
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.