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 );
}
}