http://www.mikepope.com/blog/AddComment.aspx?blogid=1264
베리 베리 굿잡...
Finally stumbled upon the way to create email messages with embedded images in ASP.NET 2.0. (Note that this doesn't work for ASP.NET 1.1, sorry to say.)
For review ... you can add an image to an email message in these ways:
So, embedding. The trick, such as it is, is to create an alternative view and to add a linked resource to the alternative view. Alternative views enable you to create different versions of the email message -- typically one in plain text and the other formatted with HTML. These then substitute for the basic msg.Body property. Behind the scenes, the class creates the appropriately formatted multipart email that incorporates the alternative views, which lets the email client choose which one to use.
To do the actual embedding, you need to do a number of things:
Here's code. I can't take credit for it. I played with this for a long time trying to get it to work and came close, but I ultimately relied on an example provided by mharder in an internal email. Note the syntax of the <img> tag and the use of ContentId, ContentType.Name, and filename.
베리 베리 굿잡...
August 17, 2005 | System.Net.Mail and embedded images | 14353 hit(s)
Finally stumbled upon the way to create email messages with embedded images in ASP.NET 2.0. (Note that this doesn't work for ASP.NET 1.1, sorry to say.)
For review ... you can add an image to an email message in these ways:
- Attach it. Works ok, but it's ... attached. Illustrated earlier.
- In an HTML-formatted message, create an <img> tag that points to an absolute URL.
- In an HTML-formatted message, create an <img> tag that points to an embedded image and then (duh) embed the image. In that case, the image shows up inline with the message's text.
So, embedding. The trick, such as it is, is to create an alternative view and to add a linked resource to the alternative view. Alternative views enable you to create different versions of the email message -- typically one in plain text and the other formatted with HTML. These then substitute for the basic msg.Body property. Behind the scenes, the class creates the appropriately formatted multipart email that incorporates the alternative views, which lets the email client choose which one to use.
To do the actual embedding, you need to do a number of things:
- Create an HTML-formatted message.
- Use an <img> tag in the message body.
- For the src attribute of the <img>, point not to a URL, but to a content ID (cid). This points to the portion of the message containing the image stream.
- Create an alternative view.
- Create a linkedResource that slurps up the image you want to embed.
- Assign a content ID to the linked resource -- this should match the cid you used in the <img> tag.
- Assign the image's file name to the linked resource.
Here's code. I can't take credit for it. I played with this for a long time trying to get it to work and came close, but I ultimately relied on an example provided by mharder in an internal email. Note the syntax of the <img> tag and the use of ContentId, ContentType.Name, and filename.
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.IO
[...]
Dim fromAddress As String = "mike@elsewhere.com"
Dim toAddress As String = "mike@elsewhere.com"
Dim subject As String = "Test EmbeddedImage"
Dim contentId As String = "image1"
Dim path As String = Server.MapPath("~") & "\"
Dim filename As String = path & "MyPicture.jpg"
Dim body As String = "Here is a linked resource: <img src=""cid:image1""/>"
Dim mailMessage As New MailMessage(fromAddress, toAddress)
mailMessage.Subject = "Testing embedded image"
Dim av1 As AlternateView
av1 = AlternateView.CreateAlternateViewFromString(body, Nothing, _
MediaTypeNames.Text.Html)
Dim linkedResource As LinkedResource = New LinkedResource(filename)
linkedResource.ContentId = contentId
linkedResource.ContentType.Name = filename
av1.LinkedResources.Add(linkedResource)
mailMessage.AlternateViews.Add(av1)
mailMessage.IsBodyHtml = True
Dim mailSender As New SmtpClient("smtpHost")
Try
mailSender.Send(mailMessage)
labelStatus.Text = "Message sent!"
Catch ex As Exception
labelStatus.Text = ex.Message
End Try
'.NET' 카테고리의 다른 글
Search Query Examples (Search Object (AD)) (0) | 2009.04.27 |
---|---|
HOW TO: Retrieve a Value from the Registry and Then Use the Value in a Setup Project in Visual Studio .NET (0) | 2009.04.16 |
VISUAL STUDIO Theme Gallery (0) | 2008.03.20 |
[펌] 리플렉션을 사용하여 비즈니스 개체를 ASP.NET 폼 컨트롤에 바인딩 (0) | 2006.02.01 |
[펌] HTTP 모듈과 처리기를 사용하여 플러그형 ASP.NET 구성 요소 만.. (0) | 2006.02.01 |