Use Gmail and 126 SMTP to send emails

Source: Internet
Author: User
Tags mailmessage smtpclient

All of the following tests passed

Private readonly string strsmtpserver = "smtp.gmail.com ";
Private readonly int intport = 587;
Private readonly string strmail = "service@cloud.com ";
Private readonly string strname = "cloud by Yi ";
Private readonly string strusername = "service@cloud.com ";
Private readonly string strpassword = "service ";

// Private readonly string strsmtpserver = "smtp.126.com ";
// Private readonly int intport = 587;
// Private readonly string strmail = "laoyi@126.com ";
// Private readonly string strusername = "laoyi ";
// Private readonly string strpassword = "888888 ";

Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Sendgmail ("laoyi@126.com", "service@cloud.com", "", "mail Title Test", "mail body test ");
}

Private void form1_load (Object sender, eventargs E)
{

}

Private void sendgmail (string sendto, string CC, string BCC, string subject, string body)
{
Mailmessage message = new mailmessage ();
Message. From = new mailaddress (strmail, strname); // the sender's email address and display name.
If (sendto! = "")
Message. to. Add (sendto); // recipient address, which can be set to multiple
If (CC! = "")
Message. CC. Add (CC );
If (bcc! = "")
Message. bcc. Add (BCC );

Message. Subject = subject;
Message. Body = body;
Message. isbodyhtml = true; // you can specify whether the email body is in HTML format.
Message. bodyencoding = system. Text. encoding. utf8; // sets the email body encoding.
Message. Priority = mailpriority. Normal; // you can specify the email priority.

// Contains attachments
// String attachpath = "attachment address ";
// System. net. Mail. Attachment attachment = new system. net. Mail. Attachment (attachpath );
// Attachment. Name = system. Io. Path. getfilename (attachpath); // attachment name
// Attachment. nameencoding = system. Text. encoding. getencoding ("gb2312"); // encode the attachment name
// Attachment. transferencoding = system. net. Mime. transferencoding. base64; // sets the attachment encoding.
// Attachment. contentdisposition. inline = true;
// Attachment. contentdisposition. dispositiontype = system. net. Mime. dispositiontypenames. inline;
// String cid = attachment. contentid; // key point. An ID value is obtained here.
// Message. attachments. Add (Attachment); // you can add multiple attachments.

Smtpclient client = new smtpclient ();
Client. Host = strsmtpserver; // set the host name or IP address of the SMTP transaction
Client. Port = intport; // port number
Client. usedefacrecredentials = true;
Client. deliverymethod = smtpdeliverymethod. Network;
Client. enablessl = true; // After SSL encryption, The Gmail mailbox must be set to ture
Client. Credentials = new system. net. networkcredential (strusername, strpassword); // your mail address & Password
Try
{
Client. Send (Message );
MessageBox. Show ("email sent to" + message. to. tostring ());
}
Catch (exception ee)
{
MessageBox. Show (EE. Message + "<br>" + ee. innerexception. Message );
}

}

Private void send126mail ()
{
Mailmessage message = new mailmessage ();
Message. From = new mailaddress (strmail, "cloud by Yi"); // the sender's email address and display name.
Message. to. Add (New mailaddress ("laoyi@qq.com"); // recipient address, which can be set to multiple
Message. CC. Add ("service@cloud.com, laoyi@126.com ");

Message. Subject = "mail Title Test ";
Message. Body = "mail body test ";
Message. isbodyhtml = false; // you can specify whether the email body is in HTML format.
Message. bodyencoding = system. Text. encoding. utf8; // sets the email body encoding.
Message. Priority = mailpriority. Normal; // you can specify the email priority.

// Contains attachments
// String attachpath = "attachment address ";
// System. net. Mail. Attachment attachment = new system. net. Mail. Attachment (attachpath );
// Attachment. Name = system. Io. Path. getfilename (attachpath); // attachment name
// Attachment. nameencoding = system. Text. encoding. getencoding ("gb2312"); // encode the attachment name
// Attachment. transferencoding = system. net. Mime. transferencoding. base64; // sets the attachment encoding.
// Attachment. contentdisposition. inline = true;
// Attachment. contentdisposition. dispositiontype = system. net. Mime. dispositiontypenames. inline;
// String cid = attachment. contentid; // key point. An ID value is obtained here.
// Message. attachments. Add (Attachment); // you can add multiple attachments.

Smtpclient client = new smtpclient ();
Client. Host = strsmtpserver; // set the host name or IP address of the SMTP transaction
Client. usedefacrecredentials = true;
Client. deliverymethod = smtpdeliverymethod. Network;
Client. Credentials = new system. net. networkcredential (strusername, strpassword); // your mail address & Password
Try
{
Client. Send (Message );
MessageBox. Show ("email sent to" + message. to. tostring ());
}
Catch (exception ee)
{
MessageBox. Show (EE. Message + "<br>" + ee. innerexception. Message );
}

}

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.