【问题标题】:Adding a picture header to an HTML email向 HTML 电子邮件添加图片标题
【发布时间】:2014-05-03 06:55:33
【问题描述】:

我正在尝试从应用程序发送一封自动电子邮件。
我遇到的主要问题是图片不显示。 其次,我应该使用的实际资源是我只能通过 URL 访问的 PSD 文件。

附加代码来自之前的实验,它附加了我从第 3 行对话框中选择的文件

那么谁能告诉我哪里出错了?

try{
            OpenFileDialog diag = new OpenFileDialog();
            diag.ShowDialog();

            FileInfo inf = new FileInfo(diag.FileName);


            string htmlBody = "<html><header><h1>TestPic</h1><br><img src=\"cid:filename\"></header></html>";
            AlternateView avHtml = AlternateView.CreateAlternateViewFromString
               (htmlBody, null, MediaTypeNames.Text.Html);

            LinkedResource inline = new LinkedResource(@inf.FullName, MediaTypeNames.Image.Jpeg);
            inline.ContentId = Guid.NewGuid().ToString();
            inline.ContentLink = new Uri("PSD_Url.com");

            avHtml.LinkedResources.Add(inline);

            MailMessage mail = new MailMessage();
            mail.AlternateViews.Add(avHtml);

            Attachment att = new Attachment(inf.FullName);
            att.ContentDisposition.Inline = true;

            mail.From = new MailAddress("from@gmail.com");
            mail.To.Add("to@gmail.com");
            mail.Subject = "Test Email";
            mail.Body = String.Format(
                       "<h3>TEST Has Sent You A Mail</h3>" +
                       @"<img src=""cid:{0}"" />", inline.ContentId);



            mail.IsBodyHtml = true;
            mail.Attachments.Add(att);
            mail.BodyEncoding = Encoding.Unicode;
            mail.HeadersEncoding = Encoding.Unicode;
            mail.SubjectEncoding = Encoding.Unicode;

            SmtpClient smp = new SmtpClient("mailhost.com", 80);
            smp.Credentials = new System.Net.NetworkCredential(@"from@gmail.com", @"password");
            smp.DeliveryFormat = SmtpDeliveryFormat.International;

            smp.Send(mail);
 }
 catch(SmtpException sEx)
 {
  throw sEx;  
 }

【问题讨论】:

  • 如果你使用src="cid:filename",那么你不应该使用inline.ContentId = "filename";来链接两者吗?

标签: c# email html-email


【解决方案1】:

.PSD 文件不会在大多数电子邮件客户端中呈现。它们仅限于 .JPG、.PNG 和 .GIF。在您的情况下,您可能希望首先通过 imagemagick 或某些图像转换器运行您的 .PSD 来进行转换。

除此之外,请确保图片 URL 是绝对的并且是在线托管的,并且应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2014-06-26
    • 2018-03-04
    • 2010-12-20
    • 1970-01-01
    • 2021-11-06
    • 2020-06-26
    • 2021-12-21
    • 2018-01-01
    相关资源
    最近更新 更多