Send mht file in body email - vb.net

Hello I am using SendGridMessage() object with VB.net to send emails through SendGrid SMTP server.
I have a .mht file that i want to send in the mail body...
I know that is possible to send pure html in a mail body but when i read the MHT file and put it on the mail body, it appears all messed up like this:
And i wanted to look it like this:
This is my code:
Dim myMsg As New SendGridMessage()
myMsg.AddTo("email#email.com")
myMsg.From = New MailAddress(ApiEmail, ApiUserName)
myMsg.Subject = "Test with MHT file"
myMsg.Html = ""
Dim fso As New FileSystemObject
Dim ts As TextStream
'Open file.
ts = fso.OpenTextFile(sPath)
'Loop while not at the end of the file.
Do While Not ts.AtEndOfStream
myMsg.Html += ts.ReadLine
Loop
'Close the file.
ts.Close()
Dim credentials = New NetworkCredential(ApiUser, ApiKey)
Dim transportWeb = New Web(credentials)
transportWeb.DeliverAsync(myMsg)

You need to convert the .MHT file to regular HTML first to use it in this way. MHT contains metadata and is structured differently than HTML, so you can't use it in a parameter that expects HTML. MHT is more like a MIME message. If you want to deal with MIME via MHT, then sending over SMTP will be easier.

Related

Writeline in VBA breaks with too long text

I am automating a web page extraction and writting the contents to a text (HTML) file.
For that I set up a File System Object like this
Dim myHTMLfilepath As String
myHTMLfilepath = "C:\temp\MyFile.html"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim myHTMLFile As Object
Set myHTMLFile = fso.createtextfile(myHTMLfilepath)
When I try to write the extracted content to the file sometimes I get an error 5 (invalid parameter). Here is th code:
myHTMLFile.writeline objIE.document.getElementsByClassName("cool-box")(0).innerHTML
It breaks when the length of the innerHTML is somewhere between 25800 and 28000 (I haven't yet figured the exact limit).
Does anyone know if the WriteLine limit can be increased or advise on a different way to do this?
Assuming the .innerHTML can successfully be read into a string (split up reading/writing to find out), you should be able to use an ADODB.Stream to write it to the file. WriteLine is intended to write a single line of text to a file, not a whole entire document.
Dim contents As String
contents = objIE.document.getElementsByClassName("cool-box")(0).innerHTML
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.Write contents
.SaveToFile myHTMLfilepath, 2
.Close
End With

Email doesn't maintain formatting after sending VB.NET

I am using EASendmail to send emails.The email body is a rich textbox.If i do formatting e.g. One line below another and send the email,the email loses it's formatting. For example if type this :
Hello,
How are you ?
in the rich text box and send it,then it becomes :
Hello,How are you
How do i maintain the text formatting ? And one more thing, if i add any image to my rich textbox, the email body doesn't keep the image ..I mean , the receiver only receives the texts of the email body, not the image. How to fix these issues ?
My code to send email is :
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 = bodytxt.Text
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
oSmtp.SendMail(oServer, oMail)
EDIT
I even tried :
oMail.HtmlBody = "<html><body>" + bodytxt.Text + "</body></html>"
But no results
This is an easy fix .
All i had to do is convert my RTF to HTML and then send it..RTF-TO-HTML-CONVERTER
After downloading the project files, reference must be added to the .dll files in the bin folder.Then :
Imports Itenso.Rtf.Converter.Html
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()
' my other codes in-between(read my post)
oMail.HtmlBody = html1

Resave .MSG files when sending email

I'm using vb.net to send email via .MSG file, I've tried with .OFT, both using createitemfromtemplate.
The emails will send, no problem. Works great. The only issue I have is anytime I restart, I have to resave the .msg or .oft files as the same file names in order for them to send again, otherwise it won't work anymore.
Any ideas as to why this is or how to fix this?
Example:
Dim omsg As Object
omsg = Outl.CreateItemfromtemplate("Custom Two.msg")
omsg.To = (TextBox1.Text)
omsg.Subject = (TextBox2.Text)
omsg.Display(False) 'will display message to user
Someone suggested adding the files into memory before the application loads to correct this.. but I'm not 100% sure how to do this, other than it goes in the load events.. Any ideas?
The answer I found to be easiest for this is:
Dim filelist() As String = Directory.GetFiles(Application.StartupPath)
For Each File In filelist
If File.Contains(".oft") Or File.Contains(".msg") Then
Dim temp1 As String = File.Replace(Application.StartupPath & "\", String.Empty)
If File.Contains(".oft") Then
ComboBox1.Items.Add(temp1)
ElseIf File.Contains(".msg") Then
ComboBox1.Items.Add(temp1)
End If
End If
Instead of trying to read them into a list one by one with names, linking them dynamically seems to fit better and execute without an issue.

how can I convert pdf file to word file using vb.net

I'm trying to develop a program which allows the user to convert a pdf file to a word file using vb.net.
Is there any good API for this ?
And, is it as easy as it looks like?
try this,
' Path of input PDF document
Dim filePath As String = "d:\\Source.pdf"
' Instantiate the Document object
Dim document As Aspose.Pdf.Document = New Aspose.Pdf.Document(filePath)
' Create DocSaveOptions object
Dim saveOptions As DocSaveOptions = New DocSaveOptions()
' Set the recognition mode as Flow
saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow
' Set the Horizontal proximity as 2.5
saveOptions.RelativeHorizontalProximity = 2.5F
' Enable the value to recognize bullets during conversion process
saveOptions.RecognizeBullets = True
' save the resultnat DOC file
document.Save("d:\\Resultant.doc", saveOptions)

Writing GMAIL inbox html to file fails (VBA, Textstream)

I retrieved a HTML file that I want to save using a textstream object from FileSystemObjects and it produces an empty file.
When I use MsgBox to display the stream right before the write command it shows all the HTML code I want, but it doesn't write the code to the file. Any suggestions?
Dim FSO As FileSystemObject
Dim FSOFile As TextStream
Dim FilePath As String
FilePath = "C:\myhtml.html"
Set FSO = New FileSystemObject
Set FSOFile = FSO.OpenTextFile(FilePath, 2, True)
FSOFile.Write myContent ' String object holding the HTML textstring
FSOFile.Close
I basically am trying to export my gmail inbox folder which I want to use as an event listener (so I can tell another machine what it is supposed to do.) .
On your comments so far:
- riteLine does not make any difference
- OpenTextFile creates non-existing files and it also creates the file and writes any other string that I manually type in, s.a. FSOFile.WriteLine "Nothing works"
- For completeness: CreateTextFile also did not help.
Any likely problem of writing the HTML code using the FSO stream?
Try WriteLine instead of just Write.
See Example: http://msdn.microsoft.com/en-us/library/aa242706%28v=vs.60%29.aspx