【问题标题】:sending image in mail in asp用asp在邮件中发送图像
【发布时间】:2011-05-20 21:33:29
【问题描述】:

我正在使用 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);

但它没有将图像作为消息发送..

请帮帮我..

【问题讨论】:

    标签: c# image


    【解决方案1】:

    这应该适合您构建附件

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

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

    【讨论】:

    • @V4Vendetta:它将我在 m.body 中给出的文本发送为“

      图片


      "
    • 那么你应该为MailMessage实例设置IsBodyHtml为true
    【解决方案2】:

    请试试这个:找到here

    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
    

    HTH

    【讨论】:

      【解决方案3】:

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

      m.BodyFormat = MailFormat.Html;
      

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

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

      【讨论】:

      • MailMessage.BodyFormat 已弃用,是 System.Web.Mail.MailMessage 的一部分。看来作者正在使用 System.Net.Mail...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2013-01-05
      • 2012-07-25
      相关资源
      最近更新 更多