SmtpClient()允许您将附件添加到邮件中,但是如果您想在邮件打开时显示图像而不是附加图片,该怎么办?

我记得,可以使用大约4行代码完成,但我不记得我在MSDN网站上找不到它。

编辑:我没有使用网站或任何东西,甚至没有使用IP地址。图像位于硬盘上。发送时,它们应该是邮件的一部分。所以,我想我可能想要使用标签...但我不太确定,因为我的电脑没有广播。

有帮助吗?

解决方案

经常提到的一个解决方案是将图像作为 Attachment 添加到邮件中,然后使用 cid:引用在HTML邮件主体中引用它。

但是,如果您使用 LinkedResources 集合,内联图像仍会显示正常,但不会显示为邮件的附加附件。 这就是我们想要发生的事情,这就是我在这里所做的:

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"), "image/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);
}

注意:此解决方案将 AlternateView 添加到 text / html 类型的 MailMessage 中。为了完整起见,您还应添加 text / plain 类型的 AlternateView ,其中包含非HTML邮件客户端的纯文本版本的电子邮件。

其他提示

HTML电子邮件和图像是附件,因此只是通过内容ID引用图像的情况,即

    Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(txtImagePath.Text)
    Dim RGen As Random = New Random()
    A.ContentId = RGen.Next(100000, 9999999).ToString()
    EM.Body = "<img src='cid:" + A.ContentId +"'>" 

这里似乎有一些全面的例子:发送电子邮件内联图片

当你说4行代码时,你指的是这个吗?

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

如何在Base64字符串中转换图像? AFAIK可以很容易地嵌入到邮件正文中。

查看此处

已经发布的解决方案是我发现的最好的解决方案,例如,如果您有多个图像,我只想完成它。

        string startupPath = AppDomain.CurrentDomain.RelativeSearchPath;
        string path = Path.Combine(startupPath, "HtmlTemplates", "NotifyTemplate.html");
        string body = File.ReadAllText(path);

        //General tags replacement.
        body = body.Replace("[NOMBRE_COMPLETO]", request.ToName);
        body = body.Replace("[ASUNTO_MENSAJE]", request.Subject);

        //Image List Used to replace into the template.
        string[] imagesList = { "h1.gif", "left.gif", "right.gif", "tw.gif", "fb.gif" };

        //Here we create link resources one for each image. 
        //Also the MIME type is obtained from the image name and not hardcoded.
        List<LinkedResource> imgResourceList = new List<LinkedResource>();
        foreach (var img in imagesList)
        {
            string imagePath = Path.Combine(startupPath, "Images", img);
            var image = new LinkedResource(imagePath, "image/" + img.Split('.')[1]);
            image.ContentId = Guid.NewGuid().ToString();
            image.ContentType.Name = img;
            imgResourceList.Add(image);
            body = body.Replace("{" + Array.IndexOf(imagesList, img) + "}", image.ContentId);
        }

        //Altern view for managing images and html text is created.
        var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
        //You need to add one by one each link resource to the created view
        foreach (var imgResorce in imgResourceList)
        {
            view.LinkedResources.Add(imgResorce);
        }

        ThreadPool.QueueUserWorkItem(o =>
        {
            using (SmtpClient smtpClient = new SmtpClient(servidor, Puerto))
            {
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Timeout = 50000;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = UMail,
                    Password = password
                };
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.IsBodyHtml = true;
                    mailMessage.From = new MailAddress(UMail);
                    mailMessage.To.Add(request.ToEmail);
                    mailMessage.Subject = "[NAPNYL] " + request.Subject;
                    mailMessage.AlternateViews.Add(view);
                    smtpClient.Send(mailMessage);
                }
            }
        });

正如您所看到的,您有一系列图像名称,因此图像位于同一文件夹中非常重要,因为它指向同一个输出文件夹。

最后,电子邮件将作为异步发送,因此用户无需等待其发送。

打开邮件时在客户端上显示图像的过程是客户端功能。只要客户知道如何渲染图像&amp;没有阻止任何图像内容,它将立即打开它。只要您正确指定了图像mime附件类型,在发送电子邮件以使其在客户端上打开时,您不必执行任何特殊操作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top