Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have searched web for sending the mail with image embedded in the mail body.
I found the below code and i have integrated with my windows forms application in vb .net and it is working well with my application.

But the problem is it is attaching the image and it is displaying as embedded image in the body.

I don't want the image to be displayed as attachment.

can anyone help me to modify the below code.

What I have tried:

Public Sub SendEmail()
 
        'CREATE MAIL MESSAGE
        Using myMailMessage As New MailMessage
 
            myMailMessage.To.Add("recipent@domain.com")
            myMailMessage.From = New MailAddress("me@mydomain.com")
            myMailMessage.Subject = "This is the email subject"
            myMailMessage.Body = "This is the default text body"
            myMailMessage.IsBodyHtml = True 'THIS WILL MAKE THE MESSAGE USE THE ALT HTML VIEW
 
            'CREATE ALT HTML BODY THAT WILL INCLUDE EMBEDDED IMAGE
            'NOTE THE IMG SRC IS CID:ThePictureID
            Dim myMailHTMLBody = "<html><head></head><body>This is a test and should include a picture: <img src=cid:ThePictureID></body></html>"
 
            'BYTES ARRAY OF IMAGE SO WE CAN PUT IN MEMORY STREAM
            Dim myImageData() As Byte = Nothing
 
            'GRAB IMAGE FROM FILE AND PUT IN MEMORY STREAM
            Using myImage = Image.FromFile("C:\directory\image.jpg")
                Dim IC As New ImageConverter
                myImageData = DirectCast(IC.ConvertTo(myImage, GetType(Byte())), Byte())
            End Using
 
            Using myStream As New MemoryStream(myImageData)
                'CREATE ALT VIEW
                Dim myAltView As AlternateView = AlternateView.CreateAlternateViewFromString(myMailHTMLBody, New System.Net.Mime.ContentType("text/html"))
                'CREATE LINKED RESOURCE FOR ALT VIEW
                Dim myLinkedResouce = New LinkedResource(myStream, "image/jpeg")
                'SET CONTENTID SO HTML CAN REFERENCE CORRECTLY
                myLinkedResouce.ContentId = "ThePictureID" 'this must match in the HTML of the message body
 
                'ADD LINKED RESOURCE TO ALT VIEW, AND ADD ALT VIEW TO MESSAGE
                myAltView.LinkedResources.Add(myLinkedResouce)
                myMailMessage.AlternateViews.Add(myAltView)
 
                'SEND EMAIL
                Using mySMTP As New SmtpClient
                    mySMTP.Host = "smtp.yourdomain.com"
                    mySMTP.Credentials = New System.Net.NetworkCredential("you@domain.com", "password")
                    mySMTP.Send(myMailMessage)
                End Using
 
            End Using
        End Using
    End Sub
Posted
Updated 27-Nov-17 2:55am

1 solution

If you didn't want the image displayed in the HTML, why did you include an img tag in the email body to show it?
<img src=cid:ThePictureID>
 
Share this answer
 
Comments
Member 13142345 27-Nov-17 1:56am    
Sorry. I didn't get you...
Member 13142345 27-Nov-17 1:59am    
I want the image to be displayed within the body but not as an attachment.
Now the above code is doing both scenarios.

i don't want the image as an attachment
Dave Kreskowiak 27-Nov-17 8:59am    
Your original post left some room for interpretation and I missed what you were really trying to say.

You would have to inline the image data into the img tag:
    <img src="data:image/jpg;base64,...base64String..." />


You can convert the image using something like:
    MemoryStream ms = new MemoryStream();
    myBitmap.Save(ms, Imaging.ImageFormat.Jpeg);
    byte[] imgData = ms.ToArray();
    string base64Data = Convert.ToBase64String(imgData);

Keep in mind, that not all email clients and browsers support showing images with inline data!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900