2

I am working on a Mail App in VB.NET. The mail app sends mail in two steps :

1.It converts the Email body(rtf) to HTML

2.Then it sends the converted HTML as the email body

For these, i am using EASendMail(to send email),Itenso RTF2HTML converter(to convert RTF to HTML). Now my email body is basically a RichTextBox(bodytxt.text).The code used are :

  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 . 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"...

1.What am i doing wrong ?How to send the image not as an attachment but a part of the email body ?

2.Can my code be improved ?

Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • What part of the documentation [VB.NET - Send HTML Email with Embedded Images](https://www.emailarchitect.net/easendmail/ex/vb/15.aspx) didn't work? – Andrew Morton Nov 22 '17 at 11:11
  • I can't entirely follow the link's instructions.Because i tried that earlier,the html code is kindda complex.And the line : ` oMail.HtmlBody = "this is a embedded picture."` can't be used in my case as i am using a rich text box rather than just adding text in the code.I could edit this code and use my textbox instead of pre-written text, but then the problem is i can't use line breaks... – Software Dev Nov 22 '17 at 12:52
  • and please read my post carefully, i am using another api to convert rtf to html and then sending it – Software Dev Nov 22 '17 at 12:55
  • You will need to examine the HTML you get from the convertor do determine how you are going to change the `src` values into the "cid:" references. If you included a relevant snippet, we might be able to help. Or are there no references to the images in the RTF, and so no `` tags in the HTML? – Andrew Morton Nov 22 '17 at 13:03
  • the link you provided shows how to send images as attachment where my image is in rich text box itself – Software Dev Nov 22 '17 at 13:09
  • It sounds like you need [Extract Embedded Image Object in RTF](https://stackoverflow.com/questions/14779647/extract-embedded-image-object-in-rtf). The code is in C#, but I expect you will be able to translate it. – Andrew Morton Nov 22 '17 at 13:17

1 Answers1

0

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:

  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. Now you can add an tag in your email's html Image is dropped in RTB -> link served up 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
  2. Convert the image to a data URI: https://www.abeautifulsite.net/convert-an-image-to-a-data-uri-with-your-browser
Poat
  • 327
  • 1
  • 11
  • i wish i could follow you instruction but i can't..Suppose a user types something in the rtb and then adds the picture??what to do then?? – Software Dev Nov 23 '17 at 04:10
  • @zackraiyan try this. put a picture in the text box - run your code put a breakpoint towards the end and see what is contained in "rr" - that will contain the image in some kind of "string" format - so let's see how it's handled in code. also what is the contents of "html1" as well? – Poat Nov 23 '17 at 04:14
  • html1 is just the converted rtf(to html) – Software Dev Nov 23 '17 at 04:24
  • @zackraiyan do you have a sample - so we can see how the image is rendered in html – Poat Nov 23 '17 at 06:50
  • i really can't as i am facing [this](https://stackoverflow.com/questions/47448680/vb-net-out-of-memory-exception) issue now – Software Dev Nov 23 '17 at 06:58