Embed the email content in. net

Source: Internet
Author: User

In. net development. We will involve sending emails. When sending emails, the images we usually use in the emails are network images. However, if the recipient cannot directly access the Internet, they will not be able to see the images, for example, an internal email is sent in the company's OA or ERP system.

Under such requirements, we need to solve them in this way;

Solution 1: using external resource (not in the email attachment)

The Code is as follows:

System. net. mail. mailMessage mailMessage = new System. net. mail. mailMessage (); mailMessage. from = "sender's mailbox"; mailMessage. to. add ("recipient email list"); mailMessage. CC. add ("cc Mail List"); mailMessage. subject = subject; AlternateView htmlBody = AlternateView. createAlternateViewFromString (content, null, "text/html"); required resource lrImage = new temporary resource ("test.gif", "image/gif"); lrImage. contentId = "Email_Image_Test"; htmlBody. administrative resources. add (lrImage); mailMessage. alternateViews. add (htmlBody); SmtpClient. send (mailMessage );

Complete case code:

SmtpClient smtp = new SmtpClient (); smtp. deliveryMethod = SmtpDeliveryMethod. network; smtp. host = "smtp.163.com"; smtp. credentials = new NetworkCredential ("evenbing", "**"); MailMessage mm = new MailMessage (); mm. from = new MailAddress ("test@163.com", "test"); mm. to. add ("test2@163.com"); mm. subject = "emails with images"; string plainTextBody = "If your email client does not support HTML format, or you switch to the" common text "view, you will see this content"; mm. alternateViews. add (AlternateView. createAlternateViewFromString (plainTextBody, null, "text/plain"); // string htmlBodyContent = "if you seeThisIn HTML format.

"; HtmlBodyContent + =" "; // note the image resource AlternateView htmlBody = AlternateView embedded here. createAlternateViewFromString (htmlBodyContent, null, "text/html"); required resource lrImage = new Custom Resource (@ "d: \ 1.jpg"," image/gif "); lrImage. contentId = "Email_Image_Test"; // The ContentId corresponds to the cid in htmlBodyContent. If the settings are incorrect, the image htmlBody is not displayed. administrative resources. add (lrImage); mm. alternateViews. add (htmlBody); // indicates the receipt. mm. headers. add ("Disposition-Notification-To", "test@163.com"); // customize the mail header mm. headers. add ("X-Website "," http://blog.csdn.net/evenbing "); // Insert the receipt header mm for the lotus domino server. headers. add ("returnreceept", "1"); mm. priority = MailPriority. normal; // priority mm. replyTo = new MailAddress ("test2@163.com", "Myself"); // if the sending fails, the SMTP server will send the failed email to tell me 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 );}}}

Solution 2: email attachments (images are sent as attachments and displayed in the email content)

String htmlBodyContent = "if you seeThisIn HTML format.

"; HtmlBodyContent + =" "; // pay attention to the image resource System embedded here. net. mail. attachment att = new Attachment (@ "E: \ test.gif"); att. contentId = "attachmentimg ";



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.