C # email sending method summary

Source: Internet
Author: User
Tags mailmessage smtpclient

 

The "system. net. Mail" provided in. NET Framework 2.0 can be easily implemented. This article lists three methods for sending:
1. Use localhost;
2. Use normal SMTP;
3. SMTP over SSL;

For example:

1. Use localhost

 

Public void sendmaillocalhost ()
{
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
MSG. to. Add ("a@a.com ");
MSG. to. Add ("B @ B .com ");
/*
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com"); can be sent to multiple people
*/
MSG. CC. Add ("c@c.com ");
/*
* Msg. CC. Add ("c@c.com ");
* Msg. CC. Add ("c@c.com"); can be copied to multiple people
*/
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
MSG. Subject = "this is a test email"; // mail title
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding
MSG. Body = "Mail content"; // mail content
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
MSG. isbodyhtml = false; // whether the email is an HTML email.
MSG. Priority = mailpriority. High; // mail priority
Smtpclient client = new smtpclient ();
Client. Host = "localhost ";
Object userstate = MSG;
Try
{
Client. sendasync (MSG, userstate );
// You can simply use client. Send (MSG );
MessageBox. Show ("sent successfully ");
}
Catch (system. net. Mail. smtpexception ex)
{
MessageBox. Show (ex. message, "email sending error ");
}
}

2. Use normal SMTP

 

Public void sendmailusezj ()
{
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
MSG. to. Add ("a@a.com ");
MSG. to. Add ("B @ B .com ");
/*
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com"); can be sent to multiple people
*/
MSG. CC. Add ("c@c.com ");
/*
* Msg. CC. Add ("c@c.com ");
* Msg. CC. Add ("c@c.com"); can be copied to multiple people
*/
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
MSG. Subject = "this is a test email"; // mail title
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding
MSG. Body = "Mail content"; // mail content
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
MSG. isbodyhtml = false; // whether the email is an HTML email.
MSG. Priority = mailpriority. High; // mail priority
Smtpclient client = new smtpclient ();
Client. Credentials = new system. net. networkcredential ("username@zj.com", "userpass ");
// The email address and password registered at zj.com
Client. Host = "smtp.zj.com ";
Object userstate = MSG;
Try
{
Client. sendasync (MSG, userstate );
// You can simply use client. Send (MSG );
MessageBox. Show ("sent successfully ");
}
Catch (system. net. Mail. smtpexception ex)
{
MessageBox. Show (ex. message, "email sending error ");
}
}

 

The above method is not applicable to all SMTP, and zj.com can be used after testing, but smtp.163.com cannot.

3. SMTP over SSL

 

Public void sendmailusegmail ()
{
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage ();
MSG. to. Add ("a@a.com ");
MSG. to. Add ("B @ B .com ");
/*
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com ");
* Msg. to. Add ("B @ B .com"); can be sent to multiple people
*/
MSG. CC. Add ("c@c.com ");
/*
* Msg. CC. Add ("c@c.com ");
* Msg. CC. Add ("c@c.com"); can be copied to multiple people
*/
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 );
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */
MSG. Subject = "this is a test email"; // mail title
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding
MSG. Body = "Mail content"; // mail content
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding
MSG. isbodyhtml = false; // whether the email is an HTML email.
MSG. Priority = mailpriority. High; // mail priority
Smtpclient client = new smtpclient ();
Client. Credentials = new system. net. networkcredential ("username@gmail.com", "password ");
// Write your Gmail email address and password.
Client. Port = 587; // The port used by Gmail
Client. Host = "smtp.gmail.com ";
Client. enablessl = true; // encrypted by SSL
Object userstate = MSG;
Try
{
Client. sendasync (MSG, userstate );
// You can simply use client. Send (MSG );
MessageBox. Show ("sent successfully ");
}
Catch (system. net. Mail. smtpexception ex)
{
MessageBox. Show (ex. message, "email sending error ");
}
}
Using Gmail to send emails, the success rate is extremely high, and almost all emails can be sent. It is recommended.

Http://www.codeproject.com/useritems/SendMailUsingGmailAccount.asp

 

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.