How to embed an Image Stream to MailMessage

后端 未结 5 655
青春惊慌失措
青春惊慌失措 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: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.", 
          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);
    }
    

提交回复
热议问题