How to add images from resources folder as attachment and embed into outlook mail body in C#

前端 未结 2 929
难免孤独
难免孤独 2021-01-28 22:44

I have a couple of images stored in visual studio project Resources folder, and I have to load them and display on the outlook mail body. Here it is the code:

B         


        
2条回答
  •  孤独总比滥情好
    2021-01-28 23:23

    Maybe you can try this.

    string htmlBody = "

    Picture


    "; AlternateView avHtml = AlternateView.CreateAlternateViewFromString (htmlBody, null, MediaTypeNames.Text.Html); var fileName = Guid.NewGuid.ToString(); var path = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)), fileName); File.WriteAllBytes(path, Properties.Resources.Pic); LinkedResource inline = new LinkedResource(path, MediaTypeNames.Image.Jpeg); //Jpeg or something inline.ContentId = Guid.NewGuid().ToString(); avHtml.LinkedResources.Add(inline); MailMessage mail = new MailMessage(); mail.AlternateViews.Add(avHtml); Attachment att = new Attachment(filePath); att.ContentDisposition.Inline = true; mail.From = from_email; mail.To.Add(data.email); mail.Subject = "Here is your subject; mail.Body = String.Format( "Here is the previous HTML Body" + @"", inline.ContentId); mail.IsBodyHtml = true; mail.Attachments.Add(att);
    1. No need to change your picture to memory stream
    2. For easy use, please do your content of e-mail to be HTML
    3. You can find in the code above

    You can refer to this link too

提交回复
热议问题