Solved

System.Net.Mail.MailMessage Inline Attachment

Posted on 2006-10-22
4
1,019 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

16 Experts available now in Live!

Get 1:1 Help Now