Link to home
Start Free TrialLog in
Avatar of johnywhite
johnywhite

asked on

System.Net.Mail.MailMessage Inline Attachment

How do I add an image inline my mail message.  I am using the built in MailMessage in the .net framework.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi johnywhite;

This shows how to insert an in line image  
<img src="c:\temp\myimage.gif" width=300 height=75>
into the body of an email message

Thanks;
Fernando Soto

The above text information was typed into a multiline text box in this example was called txtBody. The "<img src=" html tage identifies the image to be placed in the body of the text. The MailMessage IsBodyHtml property must be set to True. See test code below.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        SendEmail(txtFrom.Text.Trim(), txtTo.Text.Trim(), _
            txtSubject.Text.Trim(), txtBody.Text)

    End Sub

    Private Sub SendEmail(ByVal sender As String, _
        ByVal recipient As String, ByVal subject As String, _
        ByVal body As String, Optional ByVal attachmentString As String = "")

        Dim fromAddress As New MailAddress(sender)
        Dim toAddress As New MailAddress(recipient)
        Dim message As New MailMessage(fromAddress, toAddress)

        Dim mailSender As SmtpClient

        ' This next line needs to be modified for your Smtp server
        mailSender = New SmtpClient("smtp-server.myServer.com", 25)

        message.Bcc.Add(fromAddress)
        message.Subject = subject
        message.IsBodyHtml = True
        message.Body = body

        If Not attachmentString = "" Then
            Dim msgAttach As New Attachment(attachmentString)
            message.Attachments.Add(msgAttach)
        End If

        Try
            mailSender.Send(message)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Mail Not Sent")
        End Try

    End Sub

Fernando
Avatar of johnywhite
johnywhite

ASKER

I am not sure if this is what I was looking for.  What I want is the image to be an attachment on the email, and in the body of the email have it display that image.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial