在 ASP 中的邮件中发送图像

本文关键字:图像 ASP | 更新日期: 2023-09-27 17:57:10

我正在使用C#代码将邮件发送给其他用户。当我在其中发送文本时,它工作正常。现在我想发送图像,以便我发送的图像应该在他的收件箱中打开。意味着图像应作为邮件正文而不是附件。我正在使用代码将图像发送为:

    System.Net.Mail.Attachment attach =
                new System.Net.Mail.Attachment(
                    "C:''Documents and Settings''All Users''Documents''My Pictures''Sample Pictures''Winter.jpg");
            Random Rgen = new Random();
            attach.ContentId = Rgen.Next(100000, 9999999).ToString();
            attach.ContentDisposition.Inline = true;
  MailMessage m = new MailMessage();
 m.From = new MailAddress("swe@gmail.com");
            m.To.Add(new MailAddress("qwe@gmail.com"));
  m.IsBodyHtml = true;
            m.Body = "<html><body><h1>Picture</h1><br><img src='cid:" + attach.ContentId + "'></body></html>";
            //m.Body = inline.ToString();
            // m.Body = "<img src='cid:" + attach.ContentId + "'>";
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Credentials = new System.Net.NetworkCredential("swe@gmail.com", "swe");
            client.EnableSsl = true;
            client.Send(m);

但它不是将图像作为消息发送。

请帮帮我..

在 ASP 中的邮件中发送图像

这应该适合您构建附件

attach.ContentDisposition.Inline = true;
attach.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
attach.ContentId = contentID;
attach.ContentType.MediaType = "image/png";

从正文中删除 img 标签,然后执行m.Attachments.Add(attach);

请尝试以下操作: 在这里找到

Public Sub EmbeddedImages()
        'create the mail message
        Dim mail As New MailMessage()
        'set the addresses
        mail.From = New MailAddress("from@fromdomain.com", " Display Name")
        mail.To.Add("to@todomain.com")
        'set the content
        mail.Subject = "This is an embedded image mail"
        'first we create the Plain Text part
        Dim palinBody As String = "This is my plain text content, viewable by
        those clients that don't support html"
        Dim plainView As AlternateView =
        AlternateView.CreateAlternateViewFromString(palinBody, Nothing,
        "text/plain")
        'then we create the Html part
        'to embed images, we need to use the prefix 'cid' in the img src value
        Dim htmlBody As String = "<b>This is the embedded image 
        file.</b><DIV>&nbsp;</DIV>"
        htmlBody += "<img alt="""" hspace=0 src=""cid:uniqueId"" align=baseline 
        border=0 >"
        htmlBody += "<DIV>&nbsp;</DIV><b>This is the end of Mail...</b>"
        Dim htmlView As AlternateView =
         AlternateView.CreateAlternateViewFromString(htmlBody, Nothing,
         "text/html")
        'create the AlternateView for embedded image
        Dim imageView As New AlternateView("c:'attachment'image1.jpg",
        MediaTypeNames.Image.Jpeg)
        imageView.ContentId = "uniqueId"
        imageView.TransferEncoding = TransferEncoding.Base64
        'add the views
        mail.AlternateViews.Add(plainView)
        mail.AlternateViews.Add(htmlView)
        mail.AlternateViews.Add(imageView)
        'send mail
        SendMail(mail)
End Sub ' End EmbedImages

我认为您所要做的就是指定邮件的格式。

m.BodyFormat = MailFormat.Html;

也许这会给你一个改变的想法。我有一个类似的任务(为内部网编码)。所以。。。我所做的是 - 将图像上传到网络服务器,这样我就已经有了 URI。在代码隐藏中,我使用这些 URI 作为图像的 src。

mail.Body = "<html><body><img src='" + img_uri + "'></body></html>";