将图片标题添加到HTML电子邮件中

时间:2014-03-25 12:21:44

标签: c# email html-email

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

附加代码来自之前的实验,它附加了我从第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;  
 }

1 个答案:

答案 0 :(得分:0)

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

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