Send html email with embedded image and plain text with same image as attachment in C#

后端 未结 2 765
走了就别回头了
走了就别回头了 2020-12-18 17:08

I wish to send an email, with a plain text and html version. The email needs an image to go with it (not one I can host somewhere else), it should be embedded if the client

2条回答
  •  醉梦人生
    2020-12-18 17:31

    Plain text view, is exactly that. It is plain text, it has no images visible. You can attach a picture, but you cant make them view it.

    Take a look at the raw email outlook sends for example on how to show inline attachments. As an example heres some code someone else did: http://blog.devexperience.net/en/12/Send_an_Email_in_CSharp_with_Inline_attachments.aspx

    -- Apparently the above link is no longer valid - a quick google provided the following example to inline a picture

    string htmlBody = "

    Picture


    "; AlternateView avHtml = AlternateView.CreateAlternateViewFromString (htmlBody, null, MediaTypeNames.Text.Html); // Create a LinkedResource object for each embedded image LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg); pic1.ContentId = "Pic1"; avHtml.LinkedResources.Add(pic1); // Add the alternate views instead of using MailMessage.Body MailMessage m = new MailMessage(); m.AlternateViews.Add(avHtml); // Address and send the message m.From = new MailAddress("email1@host.com", "From guy"); m.To.Add(new MailAddress("email2@host.com", "To guy")); m.Subject = "A picture using alternate views"; SmtpClient client = new SmtpClient("mysmtphost.com"); client.Send(m);

提交回复
热议问题