C # Summary of three email sending Methods

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 ()
{< br> 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 Code
MSG. body = "email content"; // email 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 ()
{< br> 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 Code
MSG. body = "email content"; // email 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.

C # send mail http://www.legalsoft.com.cn/docs/docs/17/577.html requiring SMTP authentication. Net C # send mail content embedded picture http://hi.baidu.com/lovevc2008/blog/item/831f036cf8c5c8f34216945e.html http://space.itpub.net/12639172/viewspace-490574 http://blog.csdn.net/donny_zhang/archive/2009/01/13/3769454.aspx http://www.codesoso.com/code/smtp-email-sender-net.aspx instance source code http://hi.baidu.com/huqing7002/blog/item/1d6aa2d6c8f6ce2806088b17.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.