C # mail sending

Source: Internet
Author: User
  • Core knowledge points used to send emails
  • Microsoft encapsulated mailmessage class: mainly processes the content of the sent mail (such as the recipient's address, title, subject, and image)
  • Microsoft encapsulated smtpclient class: Mainly used to handle configuration information (such as the mail server, sending port number, and verification method) for sending this mail in SMTP mode)
  • Smtpclient is encapsulated in three layers: socket --> tcpclient --> smtpclient
  • The Code is as follows:
Public class myemail {private mailmessage mmailmessage; // mainly processes the content of the sent mail (such as the recipient's address, title, subject, and image) Private smtpclient msmtpclient; // mainly process the configuration information (such as the mail server, sending port number, and verification method) of sending this mail in SMTP mode. Private int msenderport; // The port number used to send the email (htmp protocol defaults to 25) Private string msenderserverhost; // the mail server address of the sender (either in IP or string format) Private string msenderpassword; // The password for sending the token private string msenderusername; // the user name for sending the token (that is, the string before the @ symbol, for example, [email protected]. The user name is: Hello) Private bool menablessl; // whether to perform socket-layer encrypted transmission of mail content private bool menablepwdauthentication; // Do You Want To verify the password of the sender's email address? // <summary> // constructor // </Summary> /// <Param name = "server"> email server address </param> /// <Param name = "tomail"> recipient address (which can be multiple recipients, programs are differentiated) </param> /// <Param name = "frommail"> sender address </param> /// <Param name = "subject"> email title </param> // /<Param name = "emailbody"> email content (can be designed in HTML format) </param> /// <Param Nam E = "username"> User name of the sender (that is, the string before the @ symbol, for example, [email protected], Username: Hello) </param> /// <Param name = "password"> sender's email password </param> /// <Param name = "Port"> the port number used to send the email (htmp the default protocol is 25) </param> /// <Param name = "sslenable"> "true" indicates encrypted transmission of mail content at the socket layer, false indicates that the email address is not encrypted </param> /// <Param name = "pwdcheckenable"> true indicates that the sender's email address is verified as a password, false indicates that password verification is not performed on the sender's email address </param> Public myemail (string server, string tomail, string frommail, string subject, St Ring emailbody, string username, string password, string port, bool sslenable, bool pwdcheckenable) {try {mmailmessage = new mailmessage (); mmailmessage. to. add (tomail); mmailmessage. from = new mailaddress (frommail); mmailmessage. subject = subject; mmailmessage. body = emailbody; mmailmessage. isbodyhtml = true; mmailmessage. bodyencoding = system. text. encoding. utf8; mmailmessage. priority = mailprio Rity. normal; this. msenderserverhost = server; this. msenderusername = username; this. msenderpassword = password; this. msenderport = convert. toint32 (port); this. menablessl = sslenable; this. menablepwdauthentication = pwdcheckenable;} catch (exception ex) {console. writeline (ex. tostring () ;}} public void send () {try {If (mmailmessage! = NULL) {msmtpclient = new smtpclient (); // msmtpclient. host = "SMTP. "+ mmailmessage. from. host; msmtpclient. host = This. msenderserverhost; msmtpclient. port = This. msenderport; msmtpclient. usedefacrecredentials = false; msmtpclient. enablessl = This. menablessl; If (this. menablepwdauthentication) {system. net. networkcredential NC = new system. net. networkcredential (this. msenderusername, this. msenderpassword); // msmtpclient. credentials = new system. net. networkcredential (this. msenderusername, this. msenderpassword); // NTLM: Secure Password Authentication in Microsoft Outlook Express msmtpclient. credentials = NC. getcredential (msmtpclient. host, msmtpclient. port, "NTLM");} else {msmtpclient. credentials = new system. net. networkcredential (this. msenderusername, this. msenderpassword);} msmtpclient. deliverymethod = system. net. mail. smtpdeliverymethod. network; msmtpclient. send (mmailmessage) ;}} catch (exception ex) {// m_createerrorlogtxt ("program pool (email) notification", "email failed", Ex. message );}}}

From: http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html
/// <Summary> /// send email notification /// </Summary> /// <Param name = "subjectinfo"> title </param> /// <Param name = "bodyinfo"> content </param> Public static void email (string subjectinfo, string bodyinfo) {try {var appsettings = system. configuration. configurationmanager. deleetask; // get config configuration information string senderserverip = "mail.zbian.cn"; string tomailaddress = deleetask[ "tomailaddress"]; // recipient address string frommailaddress = deleetask[ "frommailaddress"]; // sender address string mailusername = receivetask[ "mailusername"]; // sender account string mailpassword = receivetask[ "mailpassword"]; // send the email password string mailport = "25"; myemail email = new myemail (senderserverip, tomailaddress, frommailaddress, subjectinfo, bodyinfo, mailusername, mailpassword, mailport, false, false ); email. send () ;}catch (exception ex ){}}

 

/// Add the config configuration information configuration Config = configurationmanager. openexeconfiguration (configurationuserlevel. none); config. appsettings. settings. add ("isfirstinstall", "true"); config. save (configurationsavemode. modified); configurationmanager. refreshsection ("receivettings"); // obtain the config configuration information var receivettings = system. configuration. configurationmanager. appsettings;

 

 

 

 

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.