Solved

System.Net.Mail.MailMessage Inline Attachment

Posted on 2006-10-22
4
1,017 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
  • 2
4 Comments
 
LVL 62

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 62

Accepted Solution

by:
Fernando Soto earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now