Original post address: http://www.zu14.cn/2008/12/02/net_sendmail2/
In the previous. net mail article, we talked about the basic aspect. This article is a little more advanced.
Knowledge points:
- Embedded image resources in HTML-format emails
- Send a receipt to you after receiving the request
- If the email fails to be sent, send an Error Notification Email to you.
- Supports html/plain text dual-format mails, and the recipient can switch by itself
- Custom mail header
- Asynchronous transmission, supports canceling sending
- Email receipt, supporting Domino server of Lotus Notes
Smtpclient SMTP = new smtpclient (); SMTP. enablessl = false; SMTP. deliverymethod = smtpdeliverymethod. network; SMTP. host = "smtp.163.com"; SMTP. credentials = new networkcredential ("Triangle cat @ 163.com"," this is the password "); mailmessage Mm = new mailmessage (); mm. from = new mailaddress ("Triangle cat @ 163.com"," Triangle cat ", encoding. getencoding (936); mm. to. add ("Triangle cat @ gmail.com"); mm. subjectencoding = encoding. getencoding (1, 936); mm. subject = "Triangle Cat's test email, haha"; mm. bodyencoding = encoding. getencoding (936); // the content of a common text email. If the recipient's receiving client does not support HTML, This is a required string plaintextbody = "If your mail client does not support HTML format, or you can switch to the "plain text" view and you will see this content "; mm. alternateviews. add (alternateview. createalternateviewfromstring (plaintextbody, null, "text/plain"); // the content of the HTML-format email string htmlbodycontent = "if you see <B> This </B>, you can view emails in <span style = \ "color: Red \"> HTML </span> Format <br> "; htmlbodycontent + = "<a href = \" http://www.zu14.cn/\ "> really interesting Network </a> "; // note the image resource alternateview htmlbody = alternateview embedded here. createalternateviewfromstring (htmlbodycontent, null, "text/html"); // process the embedded image watermark resource lrimage = New Custom Resource (@ "D: \ blogo.gif ", "image/GIF"); lrimage. contentid = "weblogo"; // The contentid corresponds to the CID in the htmlbodycontent content. If the settings are incorrect, the htmlbody is not displayed. administrative resources. add (lrimage); mm. alternateviews. add (htmlbody); // indicates the receipt. Mm. headers. add ("disposition-Notification-to", "email address for receiving receipt @ 163.com"); // customize the mail header mm. headers. add ("X-Website", "http://www.zu14.cn/"); // Insert the receipt header mm for the Lotus Domino server. headers. add ("returnreceept", "1"); mm. priority = mailpriority. normal; // priority mm. replyto = new mailaddress ("Reply email Receiving address @ yahoo.com.cn", "Myself", encoding. getencoding (936); // if the message fails to be sent, the SMTP server will send an email to mm. deliverynotificationoptions = deliverynotificationoptions. onfailure; // process event SMTP when asynchronous sending is complete. sendcompleted + = new sendcompletedeventhandler (smtp_sendcompleted); // start sending SMTP asynchronously. sendasync (mm, null );
Void smtp_sendcompleted (Object sender, asynccompletedeventargs e) {If (E. cancelled) {MessageBox. show ("sent canceled");} else {If (E. error = NULL) {MessageBox. show ("sent successfully");} else {MessageBox. show ("failed to send:" + E. error. message );}}}
References: http://msdn.microsoft.com/en-us/vbasic/bb630227.aspx