0

I have been fighting with HTML emails with images in them. I have them working on desktop Thunderbird and Outlook, but Android Email shows a broken image.

I know it can work, because an email created with Outlook works, and has a very similar structure to the ones I am creating. Obviously it's not the same (otherwise mine would work!), but I can't see what I am missing.

I haven't cut down the code much, as I'm hoping someone will see a problem with the LinkedResource or the AlternateResource, but it might be something bigger.

The HTML view has the image tag in it, which works properly on the desktop. It shows the alternate text on Android, with a broken image icon.

Any help would be greatly appreciated.

        bool SendEmail()
        {
            string fullBody = GenerateHeader() + Body + GenerateFooter();
            string textBody = RemoveHTML(Body);

            AlternateView avHtml = AlternateView.CreateAlternateViewFromString(fullBody, Encoding.UTF8, MediaTypeNames.Text.Html);

            avHtml.TransferEncoding = TransferEncoding.QuotedPrintable;

            MailMessage message = new MailMessage();
            message.IsBodyHtml = true;
            message.Subject = BaseSubject + Subject;
            message.Body = textBody;
            message.AlternateViews.Add(avHtml);
            message.AlternateViews.Add(avText);
            message.Headers.Add("Message-Id",
                                    String.Format("<{0}@{1}>",
                                    Guid.NewGuid().ToString(),
                                    "company.com.au"));

            var assembly = Assembly.GetExecutingAssembly();
            string resourceName = "app.UI.EmbeddedItems.logo.png";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                LinkedResource inline = new LinkedResource(stream, "image/png");
                inline.ContentId = LogoId;
                inline.ContentType.Name = "logo.png";
                inline.ContentLink = new Uri("cid:" + inline.ContentId);
                inline.TransferEncoding = TransferEncoding.Base64;
                inline.ContentType.MediaType = "image/png";

                avHtml.LinkedResources.Add(inline);


                foreach (var addr in ToAddresses)
                {
                    try
                    {
                        message.To.Add(addr);
                    }
                    catch (FormatException ex) // invalid email
                    {
                        error = ex.Message;
                    }
                }
                foreach (var att in Attachments)
                {
                    message.Attachments.Add(att.ToAttachment());
                }

                if (message.To.Count > 0) // we actually added something
                {
                    SmtpClient client = new SmtpClient();

                    try
                    {
                        client.Send(message);
                        return true;
                    }
                    catch (Exception exception)
                    {
                        error = exception.Message;
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
        }
    }

    private string GenerateHeader()
    {
        var header = new StringBuilder();

        header.Append("<html xmlns = 'http://www.w3.org/1999/xhtml'>\n");
        header.Append("<head>\n");
        header.Append("<meta content='en-au' http-equiv='Content-Language' />\n");
        header.Append("<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />\n");
        header.Append("<title>Email Notification</title>\n");
        header.Append("</head>\n");
        header.Append("<body>\n");
        header.Append("<img id='").Append(LogoName).Append("' alt='Logo' src='cid:").Append(LogoId).Append("' />\n");

        return header.ToString();
    }
Simon Parker
  • 1,656
  • 15
  • 37
  • What is the image path once the email is sent out? I have a feeling that might answer you question. – Syfer Jul 31 '18 at 05:00
  • The image path is cid:logo_png. This is the name in the headers when I look at the source in Thunderbird, and it is the name in the src field of the img tag. – Simon Parker Jul 31 '18 at 05:40
  • This might help: https://stackoverflow.com/questions/32825779/gmail-not-showing-inline-images-cid-im-sending-with-system-net-mail – gwally Jul 31 '18 at 17:26
  • Thanks, gwally, but I'm doing everything in the linked item. I'm beginning to lose hope... :-) – Simon Parker Aug 01 '18 at 01:39
  • I would suggest a search with C# and stackoverflow. It will give you more resources https://stackoverflow.com/questions/18358534/send-inline-image-in-email – Syfer Aug 01 '18 at 02:10
  • Possible duplicate of [Send inline image in email](https://stackoverflow.com/questions/18358534/send-inline-image-in-email) – Syfer Aug 01 '18 at 02:10
  • I wish it was a duplicate, Syfer. I'm doing everything in the linked item, and I'm still not getting images on Android. – Simon Parker Aug 01 '18 at 02:31

1 Answers1

0

The problem turned out not to be in the construction of the email, but in the HTML itself. The image tag looked like this:

<img id='Logo.png' alt='Logo' src='cid:LogoId' />

This worked fine on desktop mail clients, but wouldn't show the image on the Android mail app, or the Google mail app for Android. To make it work, the single quotes needed to be replaced with double quotes.

This works:

<img id="Logo.png" alt="Logo" src="cid:LogoId" />

Changing GenerateHeader to use double quotes instead of single quotes in the img tag solved the problem. Makes no sense to me, but it works.

Simon Parker
  • 1,656
  • 15
  • 37