且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#Windows应用程序在电子邮件正文中(内联)添加多个图像

更新时间:2023-12-06 10:58:10

您可以将图像附加到邮件中,然后放置 img 标记并使用 ContentId 作为 src 的方式是:

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: <img src=""$CONTENTID1$""/> <br/> Image 2: <img src=""$CONTENTID2$""/> <br/> 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);
}

这是屏幕截图: