在文本框和电子邮件中嵌入图像vb

时间:2017-04-19 08:14:49

标签: vb.net email

我尝试在文本框中显示图像,然后在文本框中我试图通过电子邮件发送它...所有内容都显示并通过电子邮件发送但图像不显示在文本框中也不发送

txtEmailBody.Text = txtEmailBody.Text & "Tel:2345" & vbCrLf
    txtEmailBody.Text = txtEmailBody.Text & "Cell:435360 " & vbCrLf
    txtEmailBody.Text = txtEmailBody.Text & "Fax:3432456" & vbCrLf
    txtEmailBody.Text = txtEmailBody.Text & "Cell:+3456" & vbCrLf
    txtEmailBody.Text = "<html><body><h1>Picture</h1><br><img src=""../images/logo_deg.gif""></body></html>"
    Dim avHtml As AlternateView = AlternateView.CreateAlternateViewFromString(txtEmailBody, Nothing, MediaTypeNames.Text.Html)
    Dim pic1 As New LinkedResource("../images/logo_deg.gif", MediaTypeNames.Image.Gif)
    pic1.ContentId = "Pic1"
    avHtml.LinkedResources.Add(pic1)

但这显示错误

System.IO.DirectoryNotFoundException:找不到路径的一部分&#39; C:\ Program Files \ images \ logo_deg.gif&#39;。 在这一行

Dim pic1 As New LinkedResource("../images/logo_deg.gif", MediaTypeNames.Image.Gif)

当我在c盘中添加文件时,显示错误

从客户端检测到一个潜在危险的Request.Form值(ctl00 $ ContentPlaceHolder1 $ txtEmailBody =&#34; ...

1 个答案:

答案 0 :(得分:0)

首先要做的事情是:

阅读MSDN Textbox您将看到TextBox不支持图像。所以你可以:

  • 创建自定义文本框控件
  • 使用WebBrowser中的图像直接显示HTML Render
  • 在TextBox旁边添加PictureBox

其次,只需使用 Base64 转换

,即可轻松将嵌入式图片添加到电子邮件中

将其添加为HTML:

<img src="data:image/jpg;base64,/*base64-data-string-here*/" />

您可以在应用中轻松实现一项功能,将图片转换为 Base64

Public Function ImageToBase64(image As Image, format As System.Drawing.Imaging.ImageFormat) As String
    Using ms As New MemoryStream()
        ' Convert Image to byte[]
        image.Save(ms, format)
        Dim imageBytes As Byte() = ms.ToArray()

        ' Convert byte[] to Base64 String
        Dim base64String As String = Convert.ToBase64String(imageBytes)
        Return base64String
    End Using
End Function

然后可以像这样调用它,例如,如果有本地jpg文件,或者可以直接从 My.Resources 调用它,如果取消 Image.FromFile < / p>

MyBody = <img src="data:image/jpg;base64,/*" & ImageToBase64(Image.FromFile("Path to Image"), Imaging.ImageFormat.Jpeg) &*/" />