0

I want to send an e-mail with a logo at the top of the body. This is my method:

public bool SendEmail(string toAddress)
        {
            SmtpClient smtpServer = new SmtpClient("the server");

            try
            {
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<image src=cid:logo  style=\"height: 50px;\"><br>Hello World", null, MediaTypeNames.Text.Html);
                
                LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"));
                imageResource.ContentId = "logo";
                htmlView.LinkedResources.Add(imageResource);

                smtpServer.Port = port;
                smtpServer.Credentials = new NetworkCredential("user", "pass");
                smtpServer.EnableSsl = false;

                MailMessage message = new MailMessage();
                message.To.Add(toAddress);
                message.From = new MailAddress("the address");
                message.Subject = "Some subject";
                message.AlternateViews.Add(htmlView);
                message.IsBodyHtml = true;

                smtpServer.Send(message);
                smtpServer.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

The method is sending the e-mail, but the image is being sent as an attachment. I've tried to do this:

LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"), MediaTypeNames.Image.Jpeg);

But when I add "MediaTypeNames.Image.Jpeg" in this line, the e-mail is sent without the image at all.

Someone can help me on this? Thanks in advance!

Nantharupan
  • 594
  • 3
  • 15
Wanderer
  • 7
  • 3
  • 1
    Don't use a bool as a means to determine whether a method is successful or not. If it's unlikely to fail, just let the exception bubble up to the point where it can be handled. Your catch block here is also hiding the exception details, so if emails do start failing to send, you'll not know what the error is. – mason Jan 20 '21 at 18:28
  • @mason Thanks for the advice! I'll rethink this – Wanderer Jan 20 '21 at 18:38
  • You could also think about base64 image in the email body. Also I am not sure how this Server.MapPath would work? Is the image url publicly accessible? – Nantharupan Jan 20 '21 at 18:39
  • @Nantharupan this image url is a folder inside the project – Wanderer Jan 20 '21 at 18:42
  • @Wanderer Great. If you do think there's a decent chance a method will fail, then rather than returning a boolean instead you might create a class or enum to represent the result. If you call a method and it returns a boolean, how do you know what the boolean means? You'd have to hope it's documented, or read the method itself. But if you return an enum like `EmailSendingResult.FailedToSend` then it's absolutely clear what happened. Of course in that case, make sure you catch the exception and log it so that you can troubleshoot. – mason Jan 20 '21 at 18:50
  • You could check for base64string, https://stackoverflow.com/questions/9110091/base64-encoded-images-in-email-signatures – Nantharupan Jan 20 '21 at 18:50
  • @Nantharupan Thanks man, I converted to base64 and worked! – Wanderer Jan 21 '21 at 13:13

3 Answers3

1

You could convert your image to base64 and add that in src or you could just give the page where is the image is located.

Ajinkya Gadgil
  • 117
  • 1
  • 8
0

You could try the library MailKit. Sending embedded images works well with this library.

var stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
stream.Position = 0;

var resource = builder.LinkedResources.Add(contentId, stream);
resource.ContentId = contentId;

// in the email message, there is img with src="cid:contentId"

Another example of embedding images is here.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

You can use it :

 public bool SendEmail(string toAddress)
    {
        SmtpClient smtpServer = new SmtpClient("the server");

        try
        {
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<image src=cid:logo  style=\"height: 50px;\"><br>Hello World", null, MediaTypeNames.Text.Html);

            LinkedResource imageResource = new LinkedResource(Server.MapPath(@"..\..\images\logo.png"));
            imageResource.ContentId = "logo";
            htmlView.LinkedResources.Add(imageResource);

            smtpServer.Port = 0; //add port here
            smtpServer.Credentials = new NetworkCredential("user", "pass");
            smtpServer.EnableSsl = false;

            MailMessage message = new MailMessage();
            message.To.Add(toAddress);
            message.From = new MailAddress("the address");
            message.Subject = "Some subject";

            //add file here
            Attachment attachment = new Attachment("c://image");
            attachment.ContentDisposition.Inline = true;
            message.Attachments.Add(attachment);

            message.AlternateViews.Add(htmlView);
            message.IsBodyHtml = true;


            smtpServer.Send(message);
            smtpServer.Dispose();

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }