How to embed an Image Stream to MailMessage

后端 未结 5 642
青春惊慌失措
青春惊慌失措 2020-12-09 10:03

I\'m having some difficulty embedding an image from the Properties.Resources to a MailMessage, currently the image does not show in the email i receive.

I have succe

相关标签:
5条回答
  • 2020-12-09 10:41

    You can embed the image and skip working with resources by converting it to base64 instead:

    public static string BitmapToBase64(Bitmap b)
    {
       ImageConverter ic = new ImageConverter();
       byte[] ba = (byte[])ic.ConvertTo(b, typeof(byte[]));
       return Convert.ToBase64String(ba, 0, ba.Length);
    }
    

    and use it as html image src :

    string logoimage="<img src='data:image/png;base64," + BitmapToBase64(logo) + "'>";
    

    Note that converting to Base64 slighly increases the size of the image.

    0 讨论(0)
  • 2020-12-09 10:43

    Ok i have solved the problem.

    Instead of using the BitMap save method I converted the BitMap to Byte[] and gave the memory stream the Byte[]

    Did not work :

     b.Save(logo, ImageFormat.Jpeg);
    

    Did Work:

    Bitmap b = new Bitmap(Properties.Resources.companyLogo);
    ImageConverter ic = new ImageConverter();
    Byte [] ba = (Byte[]) ic.ConvertTo(b,typeof(Byte[]));
    MemoryStream logo = new MemoryStream(ba);
    

    I think it has something to do with the Bitmap.Save method, in the MSDN lib it mentioned that the stream has to have an offset of 0.

    0 讨论(0)
  • 2020-12-09 10:45

    Try look here:

    http://www.eggheadcafe.com/community/aspnet/2/10219822/send-mail-with-atttached-image.aspx

    From the link above:

    static void EmbedImages()
    {
       var mail = new MailMessage();
    
       mail.From = new MailAddress("me@mycompany.com");
       mail.To.Add("you@yourcompany.com");
       mail.Subject = "This is an email";
    
       var plainView = AlternateView.CreateAlternateViewFromString(
          "This is my plain text content, viewable by those clients that don't support html",
          null, "text/plain");
    
       var htmlView = AlternateView.CreateAlternateViewFromString(
          "Here is an embedded image.<img src=cid:companylogo>", 
          null, "text/html");
    
       LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
       logo.ContentId = "companylogo";
    
       htmlView.LinkedResources.Add(logo);
    
       mail.AlternateViews.Add(plainView);
       mail.AlternateViews.Add(htmlView);
    
       var smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
       smtp.Send(mail);
    }
    
    0 讨论(0)
  • 2020-12-09 10:50

    Instead of adding the "logo" to Resources, I added it directly to the project and set it to build as an "Embedded Resource".

    Then using System.Reflection.Assymbly I can get it (I assume directly) as a Stream:

      var _assembly = Assembly.GetExecutingAssembly();
      var _logoStream = _assembly.GetManifestResourceStream("[Project].[Filename].png");
      var logo = new LinkedResource(_logoStream, "image/png");
      logo.ContentId = "Logo";
      html.LinkedResources.Add(logo);
    
    0 讨论(0)
  • 2020-12-09 10:58
    Bitmap b = new Bitmap(Properties.Resources.companyLogo);
    MemoryStream logo = new MemoryStream();
    b.Save(logo, ImageFormat.Jpeg);
    

    After you do the save, you have to "seek" the MemoryStream back to the start.

    logo.Position = 0;
    
    0 讨论(0)
提交回复
热议问题