发送嵌入了图像的电子邮件 - 图像不可见

时间:2011-03-13 22:58:58

标签: c# asp.net email smtp gmail

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

有没有人对这种差异有所了解?

5 个答案:

答案 0 :(得分:16)

为了使其工作,您需要发送HTML文档,然后使用mime嵌入图像。

自从v2.0起,ASP.NET smtp对象为你完成了大部分的脏工作。

以下是微软网站的示例。 original location

  //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);

答案 1 :(得分:5)

您希望将图像嵌入邮件中。 和MailMessage正文类型应为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);

        }

答案 2 :(得分: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/

答案 3 :(得分: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);

答案 4 :(得分:0)

接收站点或邮件代理正在使用部分基于发件人的规则来阻止图像。您会发现根据您发送到的位置,您的结果会有所不同。你可以做些什么取决于接收者 - 你可以联系他们或查看他们发布的政策,看看你可以跳过哪些箍来避免阻塞。