Solved

System.Net.Mail.MailMessage Inline Attachment

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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