Alas, I have recently worked on a project to implement the mail sending function. You can also look for some information on the Internet and write it on your own. I suggest you review it for yourself. If there is anything wrong, please point it out. Thank you!
First, I create an object model (emailparameterset ):
1 public class emailparameterset 2 {3 /// <summary> 4 // recipient's email address 5 /// </Summary> 6 Public String consigneeaddress {Get; set ;} 7 8 /// <summary> 9 // recipient name 10 /// </Summary> 11 Public String consigneename {Get; set ;} 12 13 /// <summary> 14 // recipient title 15 /// </Summary> 16 Public String consigneehand {Get; set ;} 17 18 /// <summary> 19 // recipient's topic 20 /// </Summary> 21 Public String consigneetheme {Get; set ;} 22 23 /// <summary> 24 /// SMTP setting of the sending mail server 25 /// </Summary> 26 Public String sendsetsmtp {Get; set ;} 27 28 /// <summary> 29 // sender's email 30 /// </Summary> 31 Public String sendemail {Get; set ;} 32 33 // <summary> 34 // the sender's email password 35 /// </Summary> 36 Public String sendpwd {Get; set ;} 37 /// <summary> 38 /// sending content 39 /// </Summary> 40 Public String sendcontent {Get; Set ;}41}
The method is also simple. If the sending succeeds, a bool value is returned.
1 Public bool mailsend (emailparameterset epsmodel) 2 {3 try 4 {5 // determine the address of the SMTP server, and list a client SMTP 6 system. net. mail. smtpclient sendsmtpclient = new system. net. mail. smtpclient (epsmodel. sendsetsmtp); // the sender's email server address 7 // construct the sender's address 8 system. net. mail. mailaddress sendmailaddress = new mailaddress (epsmodel. sendemail, epsmodel. consigneehand, encoding. utf8); // the sender's email address and recipient's title, encoding 9 10 // construct the address of a recipient 11 system. net. mail. mailaddress consigneemailaddress = new mailaddress (epsmodel. consigneeaddress, epsmodel. consigneename, encoding. utf8); // the recipient's email address and recipient's name and encoding 12 13 // construct an email object 14 system. net. mail. mailmessage = new mailmessage (sendmailaddress, consigneemailaddress); // The Sending address and Receiving address 15 mailmessage. subject = epsmodel. consigneetheme; // Email Subject 16 mailmessage. bodyencoding = encoding. utf8; // encode 17 mailmessage. subjectencoding = encoding. utf8; // encode 18 mailmessage. body = epsmodel. sendcontent; // The sender content 19 mailmessage. isbodyhtml = false; // get or set whether the specified email body is html20 21 // set the email information (specify how to handle the emails to be sent) 22 sendsmtpclient. deliverymethod = smtpdeliverymethod. network; // specify how to send an email by sending 23 sendsmtpclient via the network. enablessl = false; // The server supports secure connections, while the security is true24 25 sendsmtpclient. usedefacrecredentials = false; // whether to send 26 27 along with the request // user logon information 28 networkcredential mycredential = new networkcredential (epsmodel. sendemail, epsmodel. sendpwd); 29 sendsmtpclient. credentials = mycredential; // log on to 30 sendsmtpclient. send (mailmessage); // 31 return true; // sent successfully 32} 33 catch (exception) 34 {35 return false; // failed to send 36} 37}
Then you can call it.
1 private void button3_click (Object sender, eventargs E) 2 {3 emailparameterset model = new emailparameterset (); 4 model. sendemail = "[email protected]"; 5 model. sendpwd = "XXX"; // password 6 model. sendsetsmtp = "smtp.163.com"; // the SMTP service address sent. Each email address is different .. Set 7 model based on the sender's email address. consigneeaddress = "[email protected]"; 8 model. consigneetheme = "topic"; 9 model. consigneehand = "title"; 10 model. consigneename = "Lee may"; 11 model. sendcontent = "htpp: // www.baidu.com"; 12 if (mailsend (model) = true) 13 {14 MessageBox. show ("email sent successfully! "); 15} 16 else17 {18 MessageBox. Show (" failed to send email! "); 19} 20}