OpenPop.net not find .eml attachment in .eml file - vb.net

I need to save attachments from .eml files from a vb.net project
OpenPop.net work fine with attachments as .zip, .pdf ...
but not find .eml attachment.
After method FindAllAttachments the list is empty and list.count = 0
My .eml files have only one .eml attachment.
I have tested version 2.0.6.1120, 2.0.6.1119 and 2.0.4.369
Tanks
enter code here
Dim oFile As FileInfo = New FileInfo(strfilename)
Dim loadMessage As OpenPop.Mime.Message = Nothing
loadMessage = OpenPop.Mime.Message.Load(oFile)
Dim att As New List(Of OpenPop.Mime.MessagePart)
att = loadMessage.FindAllAttachments()
If att.Count > 0 Then 'HERE att.count = 0, but i have 1 .eml attachment
'...

Related

Send mht file in body email

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.

vb.net zip overwrite files in zipped directory

I got the code to zip the files in the directory. Here it is
Private Sub ZipFiles()
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip"
'Open the zip file if it exists, else create a new one
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)
'Add as many files as you like:
AddToArchive(zip, "C:\TEMP\Compression\Compress Me1.txt")
AddToArchive(zip, "C:\TEMP\Compression\Compress Me2.txt")
AddToArchive(zip, "C:\TEMP\Compression\Compress Me3.txt")
zip.Close() 'Close the zip file
End Sub
Private Sub AddToArchive(ByVal zip As Package, _
ByVal fileToAdd As String)
'Replace spaces with an underscore (_)
Dim uriFileName As String = fileToAdd.Replace(" ", "_")
'A Uri always starts with a forward slash "/"
Dim zipUri As String = String.Concat("/", _
IO.Path.GetFileName(uriFileName))
Dim partUri As New Uri(zipUri, UriKind.Relative)
Dim contentType As String = _
Net.Mime.MediaTypeNames.Application.Zip
'The PackagePart contains the information:
' Where to extract the file when it's extracted (partUri)
' The type of content stream (MIME type): (contentType)
' The type of compression: (CompressionOption.Normal)
Dim pkgPart As PackagePart = zip.CreatePart(partUri, _
contentType, CompressionOption.Normal)
'Read all of the bytes from the file to add to the zip file
Dim bites As Byte() = File.ReadAllBytes(fileToAdd)
'Compress and write the bytes to the zip file
pkgPart.GetStream().Write(bites, 0, bites.Length)
End Sub
But this code is creating problem when the file is already present in the zipped folder. It gives an exception. How can i overwrite the files which are already present ?
Also, this code is little slow, is there any fast way to zip the files ?
Try this
'Open the zip file if it exists, else create a new one
Dim zip As Package = ZipPackage.Open(zipPath, _
IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.Read)

Write File with text from resources visual basic

I'm writing some program in VB, and I want to create txt file with text from file in resources. You didn't understood, did you? So, it goes like this.
Dim path As String = "c:\temp\MyTest.txt"
' Create or overwrite the file.
Dim fs As FileStream = File.Create(path)
' Add text to the file.
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
fs.Write(info, 0, info.Length)
fs.Close()
is code for creating txt file with certain text. But, I need the following.
Dim fd As New FolderBrowserDialog
fd.ShowDialog()
is the only function that I have in program, and, when folder is selected, I need to create file in that folder, file's name should be config.cfg, but, text in file which is going to be created in selected folder should be text from mine txt file which is in Recources.
I've tried
Dim path As String = fd.SelectedPath
Dim fs As FileStream = File.Create(path)
' upisuje tekst u fajl
Dim info As Byte() = New UTF8Encoding(True).GetBytes(application.startuppath & "\..\..\Resources\config.cfg")
fs.Write(info, 0, info.Length)
fs.Close()
but the text I got in file is directory from where is my program debugged.
Any ideas to do this? :)
If you added a text file to your resources, then you can try something like this:
Using fbd As New FolderBrowserDialog
If fbd.ShowDialog(Me) = DialogResult.OK Then
File.WriteAllText(Path.Combine(fbd.SelectedPath, "config.cfg"), My.Resources.config)
End If
End Using
The file I added was called config, and it made a config.txt file in my resource library.

Extracting from your resources VB.net

I have a .zip folder in the .exe resources and I have to move it out and then extract it to a folder. Currently I am moving the .zip out with System.IO.File.WriteAllByte and unziping it. Is there anyway to unzip straight from the resources to a folder?
Me.Cursor = Cursors.WaitCursor
'Makes the program look like it's loading.
Dim FileName As FileInfo
Dim Dir_ExtractPath As String = Me.tb_Location.Text
'This is where the FTB folders are located on the drive.
If Not System.IO.Directory.Exists("C:\Temp") Then
System.IO.Directory.CreateDirectory("C:\Temp")
End If
'Make sure there is a temp folder.
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
'This is where the .zip file is moved to.
Dim Dir_FTBTemp As String = Dir_ExtractPath & "\updatetemp"
'This is where the .zip is extracted to.
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
'This moves the .zip file from the resorces to the Temp file.
Dim UnleashedZip As ZipEntry
Using Zip As ZipFile = ZipFile.Read(Dir_Temp)
For Each UnleashedZip In Zip
UnleashedZip.Extract(Dir_FTBTemp, ExtractExistingFileAction.DoNotOverwrite)
Next
End Using
'Extracts the .zip to the temp folder.
So if you're using the Ionic library already, you could pull out your zip file resource as a stream, and plug that stream into Ionic to decompress it. Given a resource of My.Resources.Unleashed, you have two options for getting your zip file into a stream. You can load up a new MemoryStream from the bytes of the resource:
Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Unleashed)
...
End Using
Or you can use the string representation of the name of the resource to pull a stream directly from the assembly:
Dim a As Assembly = Assembly.GetExecutingAssembly()
Using zipFileStream As Stream = a.GetManifestResourceStream("My.Resources.Unleashed")
...
End Using
Assuming you want to extract all the files to the current working directory once you have your stream then you'd do something like this:
Using zip As ZipFile = ZipFile.Read(zipFileStream)
ForEach entry As ZipEntry In zip
entry.Extract();
Next
End Using
Taking pieces from here and there, this works with 3.5 Framework on Windows 7:
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim tmpZip As String = My.Application.Info.DirectoryPath & "\tmpzip.zip"
Using zip As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myProject.myfile.zip")
Dim by(zip.Length) As Byte
zip.Read(by, 0, zip.Length)
My.Computer.FileSystem.WriteAllBytes(tmpZip, by, False)
End Using
'Declare the output folder
Dim output As Object = shObj.NameSpace(("C:\destination"))
'Declare the input zip file saved above
Dim input As Object = shObj.NameSpace((tmpZip)) 'I don't know why it needs to have double parentheses, but it fails without them
output.CopyHere((input.Items), 4)
IO.File.Delete(tmpZip)
shObj = Nothing
Sources: answers here and https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
Since we are using the shell to copy the files, it will ask the user to overwrite them if already exist.

how to load a file from folder to memory stream buffer

I am working on vb.net win form. My task is display the file names from a folder onto gridview control. when user clicks process button in my UI, all the file names present in gridview, the corresponding file has to be loaded onto memory stream buffer one after another and append the titles to the content of the file and save it in hard drive with _ed as a suffix to the file name.
I am very basic programmer. I have done the following attempt and succeeded in displaying filenames onto gridview. But no idea of later part. Any suggestions please?
'Displaying files from a folder onto a gridview
Dim inqueuePath As String = "C:\Users\Desktop\INQUEUE"
Dim fileInfo() As String
Dim rowint As Integer = 0
Dim name As String
Dim directoryInfo As New System.IO.DirectoryInfo(inqueuePath)
fileInfo = System.IO.Directory.GetFiles(inqueuePath)
With Gridview1
.Columns.Add("Column 0", "FileName")
.AutoResizeColumns()
End With
For Each name In fileInfo
Gridview1.Rows.Add()
Dim filename As String = System.IO.Path.GetFileName(name)
Gridview1.Item(0, rowint).Value = filename
rowint = rowint + 1
Next
Thank you very much for spending your valuable time to read this post.
to read a file into a memorystream is quite easy, just have a look at the following example and you should be able to convert it to suite your needs:
Dim bData As Byte()
Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(Path))
bData = br.ReadBytes(br.BaseStream.Length)
Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)
ms.Write(bData, 0, bData.Length)
then just use the MemoryStream ms as you please. Just to clearify Path holds the full path and filename you want to read into your memorystream.