链接的资源不是c#的内联图像

本文关键字:图像 资源 链接 | 更新日期: 2023-09-27 17:54:22

我已经写了下面的代码嵌入图像的邮件,这是从我的c#代码发送。但是当我检查邮件时,我得到的图像就像附件一样,而不是内联图像。(Gmail)

AlternateView htmlBodyView = null;
string htmlBody = "<html><body><h1></h1><br><img src='"cid:SampleImage'"></body></html>";
AlternateView plainTextView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
ImageConverter ic = new ImageConverter();
Byte[] ba = (Byte[])ic.ConvertTo(bitmap_obj, typeof(Byte[]));
using (MemoryStream logo = new MemoryStream(ba))
{
    LinkedResource sampleImage = new LinkedResource(logo, "image/jpeg");
    sampleImage.ContentId = "sampleImage";
    htmlBodyView.LinkedResources.Add(sampleImage);
    p.SendEmail(htmlBodyView);
}

链接的资源不是c#的内联图像

作为参考,下面是一个完整的简化示例:

    public void SendMail()
    {
        LinkedResource logo = new LinkedResource(
            "images''image005.png",                 //Path of file
            "image/png");                           //Mime type: Important!
        logo.ContentId = "logo";                    //ID for reference
        //Actual HTML content of the body in an 'AlternateView' object.
        AlternateView vw = AlternateView.CreateAlternateViewFromString(
            "Hello, this is <b>HTML</b> mail with embedded image: <img src='"cid:logo'" />", 
            null, 
            MediaTypeNames.Text.Html);              //Mime type: again important!
        vw.LinkedResources.Add(logo);
        var msg = new MailMessage() { IsBodyHtml = true };
        msg.AlternateViews.Add(vw);
        msg.From = new MailAddress("sender@domain.com");
        msg.To.Add(new MailAddress("reciever@domain.com"));
        msg.Subject = "HTML Mail!";
        using (var client = new SmtpClient("localhost", 25)) 
            client.Send(msg);
    }