11

I send an email with a C# library. The email body contains a logo. When I send this email via GMail's SMTP server, the image is visible. When I use our domain name [email protected], the image is not visible.

Does anyone have an idea about this difference?

0

5 Answers 5

16

In order for this to work you need to send an HTML document and then embed the image using mime.

The ASP.NET smtp object does most of the dirty work for you since v2.0.

Here is an example from a microsoft site. original location

  //Holds message information.
  System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
  //Add basic information.
  mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
  mailMessage.To.Add(txtTo.Text.Trim());

  mailMessage.Subject = txtSubject.Text.Trim();
  //Create two views, one text, one HTML.
  System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
  System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");
  //Add image to HTML version
  System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName, "image/jpg");
  imageResource.ContentId = "HDIImage";
  htmlView.LinkedResources.Add(imageResource);
  //Add two views to message.
  mailMessage.AlternateViews.Add(plainTextView);
  mailMessage.AlternateViews.Add(htmlView);
  //Send message
  System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
  smtpClient.Send(mailMessage);
6
  • thank you.This is what i 'm using ,but with just one AlternateView :the one embeding image.As i said the image is well embedded when i use gmail smtp server but our domain smtp server.
    – user594166
    Commented Mar 13, 2011 at 23:19
  • @user594166 : OH, I see. Is there a "view images" link maybe. The other option is your SMTP server is filtering the mime attachment... take a look at the original message and see if it actually being delivered.
    – Hogan
    Commented Mar 13, 2011 at 23:22
  • where can i check smtp server mime filtering?
    – user594166
    Commented Mar 13, 2011 at 23:35
  • @user594166 - sadly I have no idea :(
    – Hogan
    Commented Mar 13, 2011 at 23:42
  • I might have missed it in this answer, but the imageResource.ContentType.MediaType needs to be set otherwise things like Office365 or Outlook online won't display it, but normal desktop version of Outlook will. John's answer below uses an overload of the constructor and passes in the media type. So you have a choice whether to set it or use the overload. Commented Apr 8, 2014 at 15:46
5

You want to embed the image in mail message. and MailMessage Body type should be html

try

        {

            MailMessage mail = new MailMessage();

            mail.To.Add("[email protected]");

            mail.From = new MailAddress("[email protected]");

            mail.Subject = "Test with Image";

            string Body = "<b>Welcome</b><br><BR>Online resource for .net articles.<BR><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";



            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");

            LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\codedigest.png", "image/png");

            imagelink.ContentId = "imageId";

            imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            htmlView.LinkedResources.Add(imagelink);

            mail.AlternateViews.Add(htmlView);

            SmtpClient smtp = new SmtpClient();

            smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

            smtp.Send(mail);

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }
4
if (!string.IsNullOrEmpty(BodyImageFileFullName))
        {
            var leftImageLink = new LinkedResource(BodyImageFileFullName, "image/jpg")
            {
                ContentId = "ImageGM_left",
                TransferEncoding = TransferEncoding.Base64
            };
            htmlView.LinkedResources.Add(leftImageLink);
        }

You can look this solution. I solve my problem with this code. detailed code about sending mail with image linked in body.

http://www.softcodearticle.com/2012/11/sending-mail-with-image-using-smtp-in-c/

1

The following code has solved my problem:

//Holds message information.

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

//Add basic information.

mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
mailMessage.To.Add(txtTo.Text.Trim());
mailMessage.Subject = txtSubject.Text.Trim();

//Create two views, one text, one HTML.

System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");

//Add image to HTML version

System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);

//Add two views to message.

mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);

//Send message

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);
0

The receiving site, or mail agent, is using rules, based in part on the sender, to block images. You'll find that your results differ based on where you're sending to. What you can do about this depends on the receiver - you might contact them or review their posted policies to see what hoops you could jump through to avoid the block.

3
  • Thanks Michael.the desitnation email (sendto)is my gmail account.the problme u think is then our smtp server.right?
    – user594166
    Commented Mar 13, 2011 at 23:05
  • 1
    @user: I think the issue is that Google doesn't trust embedded images sent from untrusted SMTP servers. I don't know if you can get resolve this on the sending side. On the receiving side (from your Gmail account), you can enable images on a site-by-site basis, or you can enable all images for your account. Commented Mar 13, 2011 at 23:08
  • with google gmail smtp server the email is sent with image well embedded.with our own domain smtp server:the image isn't embedded(empty image link).the problem is in the sender([email protected]).the destination email(this case my gmail account)receives emails.the problem is just in the embedded image
    – user594166
    Commented Mar 13, 2011 at 23:23