?
Solved

System.Net.Mail.MailMessage Inline Attachment

Posted on 2006-10-22
4
Medium Priority
?
1,024 Views
Last Modified: 2007-12-19
How do I add an image inline my mail message.  I am using the built in MailMessage in the .net framework.
0
Comment
Question by:johnywhite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17784807
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
0
 

Author Comment

by:johnywhite
ID: 17785664
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.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1500 total points
ID: 17785707
Hi johnywhite;

To add an attachment to an email you can add the last perameter to the SendEmail subroutine I posted in the last post. With the sample the call would look like this

        SendEmail(txtFrom.Text.Trim(), txtTo.Text.Trim(), _
            txtSubject.Text.Trim(), txtBody.Text, "C:\temp\myimage.gif")

Where "C:\temp\myimage.gif" in the above call is the name and location of the image to be the attachment.

If you want it to show up in the body of the text as well you will need to also add the html tag in the body of the text message as I showed in the last post.

Fernando
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question