简体   繁体   English

在邮件正文VB.NET中发送包含图像的HTML电子邮件

[英]Sending HTML email containing images in mail body VB.NET

I am working on a Mail App in VB.NET. 我正在使用VB.NET中的邮件应用程序。 The mail app sends mail in two steps : 邮件应用程序分两个步骤发送邮件:

1.It converts the Email body(rtf) to HTML 1.它将电子邮件正文(rtf)转换为HTML

2.Then it sends the converted HTML as the email body 2.然后将转换后的HTML作为电子邮件正文发送

For these, i am using EASendMail(to send email),Itenso RTF2HTML converter(to convert RTF to HTML) . 对于这些,我正在使用EASendMail(发送电子邮件),Itenso RTF2HTML转换器(将RTF转换为HTML) Now my email body is basically a RichTextBox(bodytxt.text).The code used are : 现在我的电子邮件正文基本上是RichTextBox(bodytxt.text)。使用的代码是:

  Imports Itenso.Rtf.Converter.Html
  Imports EASendMail
  Imports Itenso.Rtf.Support
  Imports Itenso.Rtf

   Dim rr As String = bodytxt.Rtf.Replace("\0", "")
   Dim rtfDocument As IRtfDocument = RtfInterpreterTool.BuildDoc(rr)
   Dim htmlConverter As New RtfHtmlConverter(rtfDocument)
   Dim html1 As String = htmlConverter.Convert()

    Dim oMail As New SmtpMail("TryIt")
            Dim oSmtp As New EASendMail.SmtpClient()
            oMail.From = fromtxt.Text
            oMail.To = New AddressCollection(totxt.Text)
            oMail.Subject = subjecttxt.Text
            oMail.HtmlBody = html1
            Dim oServer As New SmtpServer(MailConfig.host.Text)
            oServer.Port = MailConfig.port.Text
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
            oServer.User = fromtxt.Text
            oServer.Password = MailConfig.password.Text
            Dim r As Integer
            If ListBox1.Items.Count <= 0 Then
            Else
                oMail.AddAttachment(ListBox1.Items(r))
            End If
            oSmtp.LogFileName = Application.StartupPath & "\maillog.OFPTX"

            oSmtp.SendMail(oServer, oMail)

Now this code works fine and sends the email as HTML-maintaining all text formatting . 现在,此代码可以正常工作,并以HTML格式(保留所有文本格式)发送电子邮件。 The problem is, suppose i add an image in the rich textbox, then the image is sent but it can't be opened/viewed from my mail client's inbox.In Gmail, It shows as a broken image and "Open image in new tab" show "We can't display this image"... 问题是,假设我在富文本框中添加了一个图像,然后发送了该图像,但是无法从我的邮件客户端的收件箱中打开/查看该图像。在Gmail中,该图像显示为损坏的图像,而“在新标签页中打开图像”显示“我们无法显示此图像” ...

1.What am i doing wrong ?How to send the image not as an attachment but a part of the email body ? 1.我在做什么错?如何发送图像而不是附件而是电子邮件正文的一部分?

2.Can my code be improved ? 2.我的代码可以改进吗?

Ok so I've dealt with this issue many times. 好的,所以我已经处理了很多次了。 When you drag an image into the rich text box, it may just kind of "live there" in memory. 当您将图像拖到富文本框中时,它可能只是存在于内存中。

What is the markup look like in the rich text box? 富文本框中的标记外观如何? Typically there are 2 ways I've tackled this with web applications that involve rich text boxes (or any application) where you can drag an image in: 通常,我使用涉及富文本框(或任何应用程序)的Web应用程序来解决此问题的2种方法,您可以在其中拖动图像:

  1. When the image is drag and dropped into the richtextbox, save it to the server, or a file server - and serve up a link. 将图像拖放到RichTextBox中后,将其保存到服务器或文件服务器中-并提供链接。 Now you can add an tag in your email's html 现在,您可以在电子邮件的html中添加标签 图片已放入RTB中->提供了链接 In my vb.net project, it's an asp web project, so I'm utilizing a web service, however the RichTextBox.Rtf property may work - see if the RTF file contains the image in it: https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf(v=vs.110).aspx 在我的vb.net项目中,这是一个ASP Web项目,因此我正在使用Web服务,但是RichTextBox.Rtf属性可能起作用-查看RTF文件是否包含图像: https//msdn.microsoft。 com / zh-CN / library / system.windows.forms.richtextbox.rtf(v = vs.110).aspx
  2. Convert the image to a data URI: https://www.abeautifulsite.net/convert-an-image-to-a-data-uri-with-your-browser 将图像转换为数据URI: https : //www.abeautifulsite.net/convert-an-image-to-a-data-uri-with-your-browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM