c#.如何避免在发送邮件时附加图像



我正在尝试从my.net应用程序发送电子邮件。我在里面放了一张图片。我收到了邮件里的图片。问题是图像也是作为附件来的。我只需要内联图像。没有附件。有删除附件的选项吗?我包含了下面的代码

 body = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">";
                    body += "<HTML><HEAD><META http-equiv=Content-Type content="text/html; charset=iso-8859-1">";
                    body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text";
                    body += "</FONT></DIV><DIV><img width=600 height=100 id="_x0000_i1028" src="cid:cid1" alt="KPMG LINK"></DIV></BODY></HTML>";
                    AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, "text/plain");
                    AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    LinkedResource resource = null;
                    resource = new LinkedResource(ImagePath, new ContentType("image/png"));
                    resource.ContentId = "cid";
                    alternate.LinkedResources.Add(resource);
                    message.AlternateViews.Add(alternate);
                    message.AlternateViews.Add(alternateHtml);                        
                    smtp.Send(message);

从以下两个选项中的任意一个尝试:(参考)

选项1:-

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepathfilename.png");
inline.ContentDisposition.Inline = true;

选项2:-

using (var client = new SmtpClient())
{
    MailMessage newMail = new MailMessage();
    newMail.To.Add(new MailAddress("you@your.address"));
    newMail.Subject = "Test Subject";
    newMail.IsBodyHtml = true;
    var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
    inlineLogo.ContentId = Guid.NewGuid().ToString();
    string body = string.Format(@"
            <p>Lorum Ipsum Blah Blah</p>
            <img src=""cid:{0}"" />
            <p>Lorum Ipsum Blah Blah</p>
        ", inlineLogo.ContentId);
    var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    view.LinkedResources.Add(inlineLogo);
    newMail.AlternateViews.Add(view);
    client.Send(newMail);
}

相关内容

  • 没有找到相关文章

最新更新