发送带有嵌入式图片的电子邮件——图片不可见。

11

我使用C#库发送一封带有标志的电子邮件。当我通过GMail的SMTP服务器发送此电子邮件时,图像是可见的。当我使用我们的域名admin@domaine.net发送时,图像不可见。

有人对这种差异有什么想法吗?

5个回答

16
为了使其正常工作,您需要发送一个HTML文档,然后使用mime嵌入图像。
自v2.0以来,ASP.NET smtp对象已经为您完成了大部分繁重的工作。
这是来自Microsoft网站的一个示例。原始位置
  //Holds message information.
  System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
  //Add basic information.
  mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
  mailMessage.To.Add(txtTo.Text.Trim());

  mailMessage.Subject = txtSubject.Text.Trim();
  //Create two views, one text, one HTML.
  System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
  System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");
  //Add image to HTML version
  System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName, "image/jpg");
  imageResource.ContentId = "HDIImage";
  htmlView.LinkedResources.Add(imageResource);
  //Add two views to message.
  mailMessage.AlternateViews.Add(plainTextView);
  mailMessage.AlternateViews.Add(htmlView);
  //Send message
  System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
  smtpClient.Send(mailMessage);

谢谢。这是我正在使用的,但只有一个AlternateView:嵌入图像的那个。正如我所说,当我使用Gmail SMTP服务器时,图像被嵌入得很好,但是我们的域SMTP服务器却不行。 - user594166
@user594166:哦,我明白了。也许有一个“查看图片”链接。另一个选项是您的SMTP服务器正在过滤mime附件...查看原始消息,看看它是否实际被传送。 - Hogan
我在哪里可以检查SMTP服务器的MIME过滤? - user594166
@user594166 - 很遗憾,我不知道 :( - Hogan
在这个答案中,我可能错过了它,但是需要设置imageResource.ContentType.MediaType,否则像Office365或Outlook在线这样的东西就无法显示它,但是Outlook的普通桌面版本可以。下面John的答案使用构造函数的重载并传递媒体类型。因此,您可以选择设置它或使用重载。 - user2000095-tim
@user2000095-tim - 你好Tim - 很棒的评论 - 在这些产品出现之前我就写了这篇文章... 我已经按照你的建议进行了修改。 - Hogan

5
您希望将图片嵌入电子邮件中,且邮件的正文类型应为HTML格式。
try

        {

            MailMessage mail = new MailMessage();

            mail.To.Add("to@gmail.com");

            mail.From = new MailAddress("from@gmail.com");

            mail.Subject = "Test with Image";

            string Body = "<b>Welcome</b><br><BR>Online resource for .net articles.<BR><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >";



            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html");

            LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\codedigest.png", "image/png");

            imagelink.ContentId = "imageId";

            imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

            htmlView.LinkedResources.Add(imagelink);

            mail.AlternateViews.Add(htmlView);

            SmtpClient smtp = new SmtpClient();

            smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

            smtp.Send(mail);

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

4
if (!string.IsNullOrEmpty(BodyImageFileFullName))
        {
            var leftImageLink = new LinkedResource(BodyImageFileFullName, "image/jpg")
            {
                ContentId = "ImageGM_left",
                TransferEncoding = TransferEncoding.Base64
            };
            htmlView.LinkedResources.Add(leftImageLink);
        }

你可以查看这个解决方案。我用这段代码解决了我的问题。关于在正文中添加链接图片的详细代码。 http://www.softcodearticle.com/2012/11/sending-mail-with-image-using-smtp-in-c/

1
以下代码已经解决了我的问题:
//Holds message information.

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

//Add basic information.

mailMessage.From = new System.Net.Mail.MailAddress(txtFrom.Text.Trim());
mailMessage.To.Add(txtTo.Text.Trim());
mailMessage.Subject = txtSubject.Text.Trim();

//Create two views, one text, one HTML.

System.Net.Mail.AlternateView plainTextView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), null, "text/plain");
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", null, "text/html");

//Add image to HTML version

System.Net.Mail.LinkedResource imageResource = new System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName);
imageResource.ContentId = "HDIImage";
htmlView.LinkedResources.Add(imageResource);

//Add two views to message.

mailMessage.AlternateViews.Add(plainTextView);
mailMessage.AlternateViews.Add(htmlView);

//Send message

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Send(mailMessage);

0

接收站点或邮件代理正在使用规则,部分基于发件人,来阻止图像。您会发现,您的结果会因发送到哪里而有所不同。您可以采取的措施取决于接收方 - 您可以联系他们或查看他们发布的政策,以了解您可以跳过哪些障碍以避免被阻止。


谢谢Michael。目标电子邮件(sendto)是我的Gmail帐户。你认为的问题是我们的SMTP服务器,对吗? - user594166
1
@user:我认为问题在于,Google不信任从不受信任的SMTP服务器发送的嵌入式图像。我不知道您是否可以在发送端解决此问题。在接收方面(来自您的Gmail帐户),您可以逐个网站启用图像,或者您可以为您的帐户启用所有图像。 - Michael Petrotta
使用Google Gmail SMTP服务器发送的电子邮件可以嵌入图像。但是,如果使用我们自己的域SMTP服务器,则图像无法嵌入(空图像链接)。问题出在发件人(admin@domain.net)身上。目标电子邮件(在这种情况下是我的Gmail帐户)可以接收电子邮件。问题只出在嵌入式图像上。 - user594166

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接