/* Creator: the blog of caidao jushi
* Creation date: January 1, September 05, 2014
* Function: send an email.
*
*/
Namespace net. String. consoleapplication
{
Using system;
Using system. text;
Using system. net. mail;
Using system. Collections. Generic;
/// <Summary>
/// Send an email
/// </Summary>
Public class sendmailhelper
{
// Your own email address
Public String frommail = "[email protected]";
// Your own name
Public String fromname = "menu ";
// Mail title
Public String mailtext = "";
// If there are multiple people, please match them one by one
// Email receiving mailbox
Public list <string> ltomail = new list <string> ();
// Recipient of the email
Public list <string> ltoname = new list <string> ();
// Email content
Public stringbuilder body = new stringbuilder ();
// The attachment of the email. Note the path of the file on the server.
Public list <string> lfile = new list <string> ();
Public void send ()
{
// Sender
Mailaddress from = new mailaddress (frommail );
// Email
Mailmessage mail = new mailmessage ();
// Set the title
Mail. Subject = mailtext;
Mail. From = from;
Int c = ltomail. count;
// Recipient
For (INT I = 0; I <C; I ++)
{
Mail. to. Add (New mailaddress (ltomail [I], ltoname [I]);
}
// Set the email content
Mail. Body = body. tostring ();
// Set the mail format
Mail. bodyencoding = system. Text. encoding. utf8;
// Whether it is in HTML Format
Mail. isbodyhtml = true;
// Set the mail sending level
Mail. Priority = mailpriority. High;
// Set the attachment
Int F = lfile. count;
For (Int J = 0; j <F; j ++)
{
Mail. attachments. Add (new attachment (lfile [J]);
}
Mail. deliverynotificationoptions = deliverynotificationoptions. onsuccess;
Smtpclient client = new smtpclient ();
// Set the name of the host used for SMTP transaction, and enter the IP address.
Client. Host = "smtp.qq.com ";
// Set the port for SMTP transaction. The default value is 25.
Client. Port = 25;
Client. usedefacrecredentials = false;
// Here is the real email login name and password. For example, my email address is [email protected], my username is hbgx, and my password is xgbh.
Client. Credentials = new system. net. networkcredential ("[email protected]", "123456 ");
Client. deliverymethod = smtpdeliverymethod. Network;
Client. Send (Mail );
System. Console. writeline ("sent successfully! ");
}
Public void sendemail (string strsmtpserver, string strfrom, string strfrompwd, string strto, string strsubject, string strbody)
{
System. net. Mail. smtpclient client = new smtpclient (strsmtpserver );
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential (strfrom, strfrompwd );
Client. deliverymethod = smtpdeliverymethod. Network;
System. net. Mail. mailmessage message = new mailmessage (strfrom, strto, strsubject, strbody );
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;
Client. Send (Message );
}
}
}
C # send an email