1

I am trying to send email from my.net application. I have included an image in it. I get the image in the email. Issue is the image is coming also as an attachment. I need only the inline image. Not attachment. Any option to remove the attachment? I have include the code below

 body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
                    body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
                    body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text";
                    body += "</FONT></DIV><DIV><img width=600 height=100 id=\"_x0000_i1028\" src=\"cid:cid1\" alt=\"KPMG LINK\"></DIV></BODY></HTML>";
                    AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, "text/plain");
                    AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    LinkedResource resource = null;
                    resource = new LinkedResource(ImagePath, new ContentType("image/png"));
                    resource.ContentId = "cid";
                    alternate.LinkedResources.Add(resource);
                    message.AlternateViews.Add(alternate);
                    message.AlternateViews.Add(alternateHtml);                        

                    smtp.Send(message);
Mahesh
  • 13
  • 5

1 Answers1

0

Try from any one of below two options: (Reference)

Option 1:-

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png");
inline.ContentDisposition.Inline = true;

Option 2 :-

using (var client = new SmtpClient())
{
    MailMessage newMail = new MailMessage();
    newMail.To.Add(new MailAddress("you@your.address"));
    newMail.Subject = "Test Subject";
    newMail.IsBodyHtml = true;

    var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
    inlineLogo.ContentId = Guid.NewGuid().ToString();

    string body = string.Format(@"
            <p>Lorum Ipsum Blah Blah</p>
            <img src=""cid:{0}"" />
            <p>Lorum Ipsum Blah Blah</p>
        ", inlineLogo.ContentId);

    var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    view.LinkedResources.Add(inlineLogo);
    newMail.AlternateViews.Add(view);

    client.Send(newMail);
}
Community
  • 1
  • 1
NewDirection
  • 116
  • 10