Mvc .Net.Mail: How to send email with image (logo)

2019-09-02 02:00发布

问题:

I am using three classes to send email but i cant to combine text email with image, or just to send image. When i get email i see empty image. Help me to change my code so i can to send email with:

  1. text
  2. image
  3. and style

       public class SendService : IDistributionProvider
     {
       public int Send(System.Xml.Linq.XDocument recipientsData, string subject, string fromName, string fromAccount)
    {
        foreach (XElement element in recipientsData.Root.Elements())
        {
            string email = element.Element("email").Value;
            string name = element.Element("name").Value;
            string message = element.Element("message").Value;
    
            bool result = EmailUtils.SendEmail(fromAccount, fromName, email, name, subject, message.Replace("\n", "<br/>"));
        }
    
        return 1;
    }
    
    
    public interface IDistributionProvider
    {
    int Send(XDocument recipientsData, string subject, string fromName, 
     string fromAccount);
    }
    
    
    public static class EmailUtils
    {
     private static string sendHostName;
     private static int sendPort;
     private static string userName;
     private static string password;
     private static string defaultFromEmail;
     private static string defaultFromName;
    
     static EmailUtils()
     {
        sendHostName = ConfigurationManager.AppSettings["sendHostName"];
        sendPort = int.Parse(ConfigurationManager.AppSettings["sendPort"]);
        defaultFromEmail = ConfigurationManager.AppSettings["fromEmail"];
        defaultFromName = ConfigurationManager.AppSettings["fromName"];
    
        string credential = Utils.DecryptString(ConfigurationManager.AppSettings["credential"]);
        if (!string.IsNullOrEmpty(credential) && credential.Split(";".ToCharArray()).Length > 1)
        {
            userName = credential.Split(";".ToCharArray())[0];
            password = credential.Split(";".ToCharArray())[1];
        }
    }
    
    public static bool SendEmail(string toEmail, string toName, string subject, string body)
    {
        return SendEmail(defaultFromEmail, defaultFromName, toEmail, toName, subject, body);
    }
    
    public static bool SendEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string body)
    {
        try
        {
            if (string.IsNullOrEmpty(toEmail))
            {
                return false;
            }
    
            if (string.IsNullOrEmpty(toName))
            {
                toName = toEmail.Substring(0, toEmail.IndexOf("@"));
            }
    
            if (string.IsNullOrEmpty(fromEmail))
            {
                fromEmail = defaultFromEmail;
            }
    
            if (string.IsNullOrEmpty(fromName))
            {
                fromName = defaultFromName;
            }
    
            Message message = new Message();
            message.Charset = "UTF-8";
            message.Subject = Codec.RFC2047Encode(subject, "UTF-8");
            message.From = new Address(fromEmail, fromName);
            message.To.Add(toEmail, toName);
            message.BodyHtml.Format = BodyFormat.Html;
            message.BodyHtml.Charset = "UTF-8";
            message.BodyHtml.Text = body;
    
            return ActiveUp.Net.Mail.SmtpClient.SendSsl(message, sendHostName, sendPort, userName, password, SaslMechanism.Login);
        }
        catch
        {
            return false;
        }
    }
    

    }

In this way I send email- just text:

 string bodyEmail = "<h2>Welcome to website</h2></br><div><p>Thank for using website</p></div>";

        EmailUtils.SendEmail("xxx@gmail.com","xxxx","Contact",bodyEmail);

回答1:

Easiest way to do it is to inline your images using Data URIs.

You essentially inline the image into the HTML of your message. Just follow the format

data:[<MIME-type>][;charset=<encoding>][;base64]

where mime-type may be image/jpeg, charset should be ASCII, and the bytes of the image converted to base64. You can get that by reading the bytes of the image file from disk

byte[] imaeg = File.ReadAllBytes("nekkedladies.jpg");

then convert the byte array to a base 64 string

var base64Imaeg = System.Convert.ToBase64String(imaeg);

slap it together and stick it in your html (stolen from wiki)

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Nekkid Ladies" />

btw, the example image data isn't nekkid ladies. It's this:


sorry



回答2:

I've actually ran into this same problem and what really helped me was this. In my case I had the html and I had to parse it out using HtmlUtilityPack. I would not recommend using the encoded string as It is not fully supported, and it makes your message bloated. The cid way is also how outlook adds images to an email. I'd add code but I think the example was good enough in my case.