Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching Image in the body of mail in C#

Tags:

c#

email

image

smtp

How can I attach an image in the body content . I have written the below code

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "[email protected]";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("[email protected]"));
message.From = new  System.Net.Mail.MailAddress("[email protected]");              
message.Subject = "test subject";
message.Body = "<img src=@'C:\\Sunset.jpg'/>";                
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
 smtpClient.Host = "hostname";
 smtpClient.Port = 25;
 smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
 smtpClient.Send(message);

The code is fine as I am receiving the message also but the image is coming as [X] inside the body and not as the image. How to solve this? The path is correct?

like image 204
priyanka.bangalore Avatar asked Feb 23 '10 09:02

priyanka.bangalore


People also ask

How to Add the images in smtp mail body?

You could try this: First use Take Screenshot command to get the screenshot and save in current path… and use Send Outlook message activity in body mention this string name and in properties make sure you have check isBodyHtml Properties … Then image will be added in body of Email… Hope it helps!

How do you embed an image into an email?

Position your cursor where you want the image in your message. Select Insert > Pictures. Browse your computer or online file locations for the picture you want to insert. Select the picture, then select Insert.


1 Answers

    string attachmentPath = Environment.CurrentDirectory + @"\test.png";
    Attachment inline = new Attachment(attachmentPath);
    inline.ContentDisposition.Inline = true;
    inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
    inline.ContentId = contentID;
    inline.ContentType.MediaType = "image/png";
    inline.ContentType.Name = Path.GetFileName(attachmentPath);

    message.Attachments.Add(inline);

reference: Send an Email in C# with Inline attachments

like image 182
Asad Avatar answered Oct 04 '22 21:10

Asad