C # send an email,
The first step is to write a mail-sending class sendNoticeEmail:
REFERENCE The namespace using System. Net. Mail;
Public class sendNoticeEmail {public string sendNoticeEmailMethod (ef_dm_oop_request oop) // because there are many parameters, pass in the required parameters through an object {// read the mail template page content string emailContent = File. readAllText (System. web. httpContext. current. server. mapPath ("~ /Upload/templates/promotion/NoticeEmail.txt "), System. Text. Encoding. UTF8 );
// Replace the variable emailContent = emailContent on the template page. replace ("author", oop. creation_user ). replace ("state", oop. status ). replace ("themename", oop. theme_en_desc ). replace ("keycode", oop. key_code ). replace ("familyname", oop. family ). replace ("reason", oop. reason_id ). replace ("tmc", oop. tmc); // read the mail configuration parameters configured in webconfig
String mailConfig = System. Configuration. ConfigurationManager. receivettings ["mailConfig"]. ToString (); mailConfig = string. IsNullOrEmpty (mailConfig )? String. Empty: mailConfig; string [] mailConfigArray = mailConfig. Split (';');/* mailConfigArry content: smtp.163.com; 25; account @ 163.com; pwd Password
Smtp.163.com is the email server address of 163, 25 is the port, followed by the account and password, which are different from those of major email service providers, please refer to the http://www.360doc.com/content/15/0411/20/20545288_462481521.shtml * // instantiate an email message object MailMessage email = new MailMessage (); // email. to. add ("123456777@qq.com ");
// Cyclically send messages and use the Add method to write emails to recipient addresses. to. add (oop. email_address); email. from = new MailAddress (mailConfigArray [2], "this is the alias for display", System. text. encoding. UTF8 );
// Content email. Body = emailContent;
// Subject email. subject = "[NSA ++ DM Key in_OOP" + oop. dm_theme_id + "] (" + oop. status + ") Out of Process:" "+ oop. theme_en_desc + "+ oop. key_code + "+ oop. family + "+ oop. reason_id + "" by "" + oop. tmc + ""; email. subjectEncoding = System. text. encoding. UTF8; // email Subject code. bodyEncoding = System. text. encoding. UTF8; // email content encoding. isBodyHtml = false; email. priority = MailPriority. high; // mail priority // instantiate an smtp client object to send an email System. net. mail. smtpClient stmp = new SmtpClient (); stmp. deliveryMethod = SmtpDeliveryMethod. network; // specify the email sending method stmp. host = mailConfigArray [0]; // sets whether authentication is required for sending. If the following credentials is not required, stmp is not required. useDefaultCredentials = true; stmp. credentials = new System. net. networkCredential (mailConfigArray [2], mailConfigArray [3]); stmp. enableSsl = true; stmp. port = System. int32.Parse (mailConfigArray [1]); string returnMess = ""; try {// send the mail stmp. send (email); returnMess = master. success; // return returnMess;} catch (Exception ex) {log4.Error ("sendEmail", ex); returnMess = master. failure; // throw ex; // return "failed";} return returnMess ;}}
Step 2: Create an email template and create a text document, for example:
The name of the variable to be replaced must be consistent with that in the code!
Step 3: Call it. First, we need to get the variables to be replaced in our email.
1 result_email_notice = promotion. _ dm_item.Query_email_notice (ef_dm_oop_request );
// Receive the notification email information to be sent. Here, the information found in the ibaits database is used to return to an entity class. You can change it to 2 if (! String. isNullOrEmpty (result_email_notice.email_address) 3 {4 sendNoticeEmail email = new sendNoticeEmail (); // reference the namespace where this class is located and a new object is given 5 string message = email. sendNoticeEmailMethod (result_email_notice); 6}
Step 4: Go to the sender's mailbox and set it to enable SMTP. Take 163 as an example:
Here you need to set a client password. Remember, you need to configure this password in the program, not your mailbox password.
Different mailboxes have different sending restrictions. 163 seems to be the most 50 messages a day. QQ one hundred, please refer
http://www.360doc.com/content/15/0411/20/20545288_462481521.shtml
You can test it by yourself.
Final Result: If you call this method normally, you will receive an email notification. For example:
Possible problems: If you report what security verification is required by the client, it may be that your email account and password are incorrect. Try logging on to the client first. If you enter the recipient's recycle bin, you can set a whitelist.