Add multiple images in the email body (inline)using c# windows application

后端 未结 2 1260
说谎
说谎 2020-12-02 02:21

I searched for this several times and found solutions,but all supports only one image.Finally I used this code. But the problem is,if the html contain more than one image o

2条回答
  •  孤街浪徒
    2020-12-02 03:15

    You can attach images to mail and then put img tag and useContentId of attachment as src this way:

    private void denMailButton_Click(object sender, EventArgs e)
    {
        string subject = "Subject";
        string body = @"Image 1:  
    Image 2:
    Some Other Content"; MailMessage mail = new MailMessage(); mail.From = new MailAddress("from@example.com"); mail.To.Add(new MailAddress("to@example.com")); mail.Subject = subject; mail.Body = body; mail.Priority = MailPriority.Normal; string contentID1 = Guid.NewGuid().ToString().Replace("-", ""); string contentID2 = Guid.NewGuid().ToString().Replace("-", ""); body = body.Replace("$CONTENTID1$", "cid:" + contentID1); body = body.Replace("$CONTENTID2$", "cid:" + contentID2); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); //path of image or stream LinkedResource imagelink1 = new LinkedResource(@"D:\1.png", "image/png"); imagelink1.ContentId = contentID1; imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; htmlView.LinkedResources.Add(imagelink1); LinkedResource imagelink2 = new LinkedResource(@"D:\2.png", "image/png"); imagelink2.ContentId = contentID2; imagelink2.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; htmlView.LinkedResources.Add(imagelink2); mail.AlternateViews.Add(htmlView); SmtpClient client = new SmtpClient(); client.Host = "mail.example.com"; client.Credentials = new NetworkCredential("from@example.com", "password"); client.Send(mail); }

    And here is the screenshot:

提交回复
热议问题