12

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 = "xyz@someorg.com";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com"));
message.From = new  System.Net.Mail.MailAddress("fromaddress@fromaddress.com");              
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?

one noa
  • 345
  • 1
  • 3
  • 10
priyanka.bangalore
  • 1,471
  • 7
  • 20
  • 32

3 Answers3

24
    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

Asad
  • 21,468
  • 17
  • 69
  • 94
  • Oh... this approach seems even better. – Arnis Lapsa Feb 23 '10 at 09:37
  • 4
    For anybody looking at this solution in the future, in order to make it work, I had to add the following image tag in my code (My ContendId is "Screenshot"): `message.Body = "\"\"";` – Grungondola Aug 23 '16 at 19:57
  • The `cid` link doesn't seem to work for Outlook on Android, but works for Outlook on Windows and Samsung Email. Got It working by `AlternateView` and `LinkedResource` instead, [see this answer](https://stackoverflow.com/a/30126266/2441442). Not sure what the differnce is. – Christian Gollhardt Aug 14 '23 at 14:49
1

Use so called LinkedResource. Here you can find how-to. Have done that successfully.

If tutorial does not help, don't be shy and ask for clarification. :)

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;


namespace ItsTrulyFree
{
    public partial class demo_mail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    enter code here
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {


                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(txtUsername.Text);
                // Recipient e-mail address.
                Msg.To.Add(txtTo.Text);
                Msg.Subject = txtSubject.Text;
                // File Upload path
                String FileName = fileUpload1.PostedFile.FileName; 


                string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";

            //LinkedResource LinkedImage = new LinkedResource(FileName);
                     //HttpContext.Current.Server.MapPath("/UploadedFiles");
            LinkedResource LinkedImage = new LinkedResource(Server.MapPath("~//" + FileName), "image/jpg");
                LinkedImage.ContentId = "MyPic";
                //Added the patch for Thunderbird as suggested by Jorge
                LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailbody+
                  " <img src=cid:MyPic>",
                  null, "text/html");

                htmlView.LinkedResources.Add(LinkedImage);
                Msg.AlternateViews.Add(htmlView);


                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtpwd.Text);
                smtp.EnableSsl = true;
                smtp.Send(Msg);
                Msg = null;
                Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
            }
            //catch (Exception ex)
            //{
            //    Console.WriteLine("{0} Exception caught.", ex);
            //}
        }

}