ususual problem with system.net.mail attachment - memorystream

I have an ususual problem with my attachment memorystream on a VB.net project. I am calling a shared member (SendMail) passing in a customer object and a memorystream of a file to be sent as an attachment. Within the "SendMail" it calls two separate functions to prepare one regular email and another for a digitally signed email. The problem I have is I recieve the digitally signed email perfect, however, in the regular email, the attachment is blank and the file name has appended (64 B). If I disable the part of the digitally signing the regular mails works fine. It looks like somewhere the memorystream is changed in the digital signing.
Here his how I make the calls
mailHelper.SendMail(cust, attachment)
withing the mailHelper CLASS
Public Shared Sub SendEmail(ByVal cust As Customer, ByVal attachment As MemoryStream)
Dim messages As New List(Of MailMessage)
messages.Add(CreateUnSignedMail(cust,attachment)
messages.Add(CreateSignedMail(cust,attachment)
SendSMTPMail(messages)
End Sub
Private Shared Function CreateUnSignedMail(ByVal cust As Customer, ByVal attachment As MemoryStream) As MailMessage
Dim eMail As New MailMessage()
With eMail
.//Normal properties set (like to, from etc)
.Attachments.Add(New Attachment(attachment, "someFilename.doc")
End With
Return eMail
End Function
Private Shared Function CreateSignedMail(ByVal cust As Customer, ByVal attachment As MemoryStream) As MailMessage
Dim eMail As New SecureMailMessage()
With eMail
.//Normal properties set (like to, from etc)
.Attachments.Add(New SecureAttachment(attachment, "someFilename.doc")
End With
Return eMail
End Function
Private Shared Sub SendSMTPMail(ByVal messages As List(Of System.Net.Mail.MailMessage))
Dim smtp As New SmtpClient("myServer")
Try
With smtp
.//Additional properties set
For Each email In messages
.Send(email)
Next
End With
Catch ex As Exception
//Log error to file.
Logger.Log.Error("SMTP Error", ex)
Throw
End Try
End Sub
If I try the following it works fine, however, is this the proper solution?
messages.Add(SendUnSignedMail(cust, New MemoryStream(attachment.ToArray())))
messages.Add(SendSignedMail(cust, New MemoryStream(attachment.ToArray())))

Assume you already fixed this given the delay in getting an answer but here is my attempt anyhow...
There are quite a few overloads to create an Attachment, none of them match the way you initialised the Attachment.
When creating an Attachment the second parameter needs to be either NULL or identify a valid MIME-type for your attachment.
Hope this helps.

Related

Forwarding OpenPOP Attachments to SMTP (System.Net.Mail)

I have an application I have built in Visual Studio using VB.NET that pulls mail messages from an Outlook mailbox and saves the message information into a database and downloads the attachments (if any) into a folder. Before saving the mail message to my database, if the message is from a certain user, I forward the message to another mailbox (and thus, I don't save the message). This is all working fine, except when I try to forward a message with attachments.
As stated in the title, I am using OpenPOP to pull the mail and SMTP to transfer the mail. When I try to create the SMTP attachment from the OpenPOP message I get the following error:
System.InvalidCastException: Conversion from type 'MessagePart' to type 'String' is not valid.
The message is thrown in the AddAttachments function (below) on the line:
myAttachment = New Attachment(attachment) (in the For Each statement)
Public Sub ForwardMessage(
ByVal msgPOP As OpenPop.Mime.Message,
toAddress As String,
fromAddress As String,
subject As String,
body As String
)
Dim smtpServer As New System.Net.Mail.SmtpClient(Me.serverName)
Dim msgSMTP As New MailMessage()
msgSMTP.Sender = New MailAddress(fromAddress)
msgSMTP.To.Add(New MailAddress(toAddress))
msgSMTP.Subject = subject
msgSMTP.Body = body
msgSMTP.IsBodyHtml = True
Dim attachments As Object
attachments = AddAttachments(msgPOP, msgSMTP)
msgSMTP.Attachments.Add(New Attachment(attachments))
smtpServer.Send(msgSMTP)
End Sub
I finally figured it out with some help from a friend. The AddAttachments function below has been edited to show the fix.
Public Function AddAttachments(
ByVal msgPOP As OpenPop.Mime.Message,
ByVal msgSMTP As MailMessage
) As MailMessage
Dim attachments As Object = msgPOP.FindAllAttachments()
Dim myAttachment As Attachment = Nothing
For Each attachment As OpenPop.Mime.MessagePart In attachments
Dim sName As String = attachment.FileName
Dim sContentType As String = attachment.ContentType.MediaType
Dim stream As MemoryStream = New MemoryStream(attachment.Body)
myAttachment = New Attachment(stream, sName, sContentType)
msgSMTP.Attachments.Add(myAttachment)
Next
Return msgSMTP
End Function
I have spent hours researching this issue and I have not found one solution yet. I tried changing the application data type to String and OpenPOP.MIME.MessagePart to no avail. I tried adding "ToString" to the attachment variable and received the following error:
System.InvalidCaseException: Operator '&' is not defined for type 'MessagePart' and string ".ToString".
I have been reading up on MIME to see if that would offer some ideas, though I have not been able to connect the dots. I am assuming this is possible and hoping someone will be able to share the solution, and I will be happy with either VB.NET or C#.NET.
Thank you very much in advance and I appreciate your time.
The solution is in the edited AddAttachments function in my original post above.

File Being Used By .NET Mail

I wrote a simple program written to collect files, send the files, then delete said files. I am sending them via Email using System.Net.Mail
If Label6.Text = "" Then
mail.Attachments.Add(New Attachment(zipPath))
End If
'Enables SSL, if required
ssl = Provider.ssl
If skipAhead = True Then
ssl = True
End If
If ssl = True Then
SmtpServer.EnableSsl = True
End If
'Sends the email
SmtpServer.Send(mail)
'Allows the user to either keep testing, or quit.
If skipAhead = True Then
My.Computer.FileSystem.DeleteFile(unalteredPath)
My.Computer.FileSystem.DeleteFile(unalteredPath1)
My.Computer.FileSystem.DeleteFile(zipPath)
Else
Dim keepOpen As Integer = MsgBox("The Email was sent. You can keep testing if you would like to. Press ok to close, and cancel to keep testing", MsgBoxStyle.OkCancel)
If keepOpen = 1 Then
Close()
End If
End If
As seen in line 2, the attachment is added to the email, and I do not attempt to delete the attachment until after the email is sent, however when the code is running, it throws an error that the file is being used by another process.
I am also wondering if this could be lingering from the .zip being created itself. Here is the code that does that:
Public Sub Zipping()
'Copies files to the folder where they will be zipped from
My.Computer.FileSystem.CopyFile(unalteredPath, outputs & "\ExIpOutput.txt")
My.Computer.FileSystem.CopyFile(unalteredPath1, outputs & "\IpConfig.txt")
'Deletes the old output files
My.Computer.FileSystem.DeleteFile(unalteredPath)
My.Computer.FileSystem.DeleteFile(unalteredPath1)
'Starts the zip Sub
ZipFile.CreateFromDirectory(outputs, zipPath, CompressionLevel.Fastest, True)
My.Computer.FileSystem.DeleteDirectory(outputs, FileIO.DeleteDirectoryOption.DeleteAllContents)
End Sub
Here is the CreateFromDirectory sub:
Public Shared Sub CreateFromDirectory(sourceDirectoryName As String, destinationArchiveFileName As String, compressionLevel As Compression.CompressionLevel, includeBaseDirectory As Boolean)
End Sub
Is there something I'm missing here, or do I need to have the program sleep for a bit to let the email send, then delete the .zip file?
You can load the file into an array: File.ReadAllBytes(String) Method.
Then get a MemoryStream from that: How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?
And finally, you can use a MemoryStream for an attachment: Attach a file from MemoryStream to a MailMessage in C#.
As the data to be sent is in memory, you should be able to delete the file. Note that if there is a crash, the data is gone.
The examples are in C#, but if you have problems using the methods in VB.NET, please edit your question to show how far you've got and tell us what the problem is.
A better solution to this issue is to dispose the Attachment object that is locking the file. Any object you create that has a Dispose method should have that method called when you're done with the object and an Attachment is no different.
Dim fileAttachment As Attachment
If Label6.Text = "" Then
fileAttachment = New Attachment(zipPath)
mail.Attachments.Add(fileAttachment)
End If
'...
SmtpServer.Send(mail)
If fileAttachment IsNot Nothing Then
fileAttachment.Dispose()
End If

EWS Connection to Office365 fails - 401 Unauthorized

I need to make a VB .net tool to read email and save attachments. My company recently migrated from on-premise Exchange to Office 365. I have been reading EWS tutorials for 2 days now, and searching StackOverflow, but cannot get past the most basic step of authorized access to the O365 mailbox.
I took the original code from this article: htp://www.c-sharpcorner.com/UploadFile/jj12345678910/reading-email-and-attachment-from-microsoft-exchange-server/. I had some trouble converting it to VB using the Telerik converter but I think I have it right. Each time I try using the FindItemsResults method it halts with "(401) Unauthorized."
The instructions for asking a question state that I should include links to what I have already found and why it did not work, but my SO reputation only allows me 2 links. Here is what I have tried:
I have tried every possible usercode and domain combination I can think after reading this page: htps://stackoverflow.com/questions/10107872/ews-connections-issues-401-unauthorized
I am trying to read my own mailbox, so this one does not help: htps://stackoverflow.com/questions/43346498/401-unauthorized-access-when-using-ews-to-connect-to-mailbox
My connection looks identical to the connection on this page, but using it in my project did not get past the same Unauthorized error as before:htps://stackoverflow.com/questions/29009295/ews-managed-api-retrieving-e-mails-from-office365-exchange-server
Here is my code:
Public Class Form1
Public Exchange As ExchangeService
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstMsg.Clear()
lstMsg.View = View.Details
lstMsg.Columns.Add("Date", 150)
lstMsg.Columns.Add("From", 250)
lstMsg.Columns.Add("Subject", 400)
lstMsg.Columns.Add("Has Attachment", 50)
lstMsg.Columns.Add("Id", 100)
lstMsg.FullRowSelect = True
End Sub
Public Sub ConnectToExchangeServer()
Me.lblMsg.Text = "Connecting to Exchange Server"
lblMsg.Refresh()
Try
'Exchange = New ExchangeService(ExchangeVersion.Exchange2013)
Exchange = New ExchangeService()
Exchange.TraceEnabled = True
'Exchange.UseDefaultCredentials = True
Exchange.Credentials = New WebCredentials("DoeJohn", "mypasswd", "mycorp.com")
'Exchange.AutodiscoverUrl("DoeJohn#mycorp.mail.onmicrosoft.com", AddressOf MyRedirectionURLValidationCallback)
'Exchange.AutodiscoverUrl("John.Doe#mycorp.com", AddressOf MyRedirectionURLValidationCallback)
Exchange.Url = New System.Uri("https://outlook.office365.com/ews/exchange.asmx")
lblMsg.Text = "Connected to Exchange Server"
lblMsg.Refresh()
Catch ex As Exception
MsgBox("Fatal Error in Connect: " & ex.Message)
End
End Try
End Sub
Public Function MyRedirectionURLValidationCallback(RedirectionURL As String) As Boolean
Dim Result As Boolean = False
Dim RedirectionURI As Uri = New Uri(RedirectionURL)
If RedirectionURI.Scheme = "https" Then
Return True
End If
Return False
End Function
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Call ConnectToExchangeServer()
Dim ts As TimeSpan = New TimeSpan(0, -1, 0, 0)
Dim MyDate As DateTime = DateTime.Now.Add(ts)
Dim MyFilter As SearchFilter.IsGreaterThanOrEqualTo = New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, MyDate)
If Exchange IsNot Nothing Then
Dim FindResults As FindItemsResults(Of Item) =
Exchange.FindItems(WellKnownFolderName.Inbox, MyFilter, New ItemView(50))
Dim NewRow As ListViewItem
For Each MyItem As Item In FindResults
Dim Message As EmailMessage = EmailMessage.Bind(Exchange, MyItem.Id)
NewRow = New ListViewItem(Message.DateTimeReceived.ToString())
NewRow.SubItems.Add(Message.From.Name.ToString())
NewRow.SubItems.Add(Message.Subject)
NewRow.SubItems.Add(Message.HasAttachments.ToString())
NewRow.SubItems.Add(Message.Id.ToString())
lstMsg.Items.Add(NewRow)
Next
Else
End If
End Sub
I have confirmed that AutoDiscover is correctly finding the server, comparing to Test Email AutoConfiguration in Outlook.
AutoConfig
An interesting sidenote -- after my company moved to Office 365, I noticed that I have two new SMTP mail addresses. If I open Outlook properties on myself, I see this:
Props
This means someone can send mail to me now either at the old address john.doe#mycorp.com, and also now at doejohn#mycorp.mail.onmicrosoft.com. The new address is based on my domain usercode. I tested the microsoft one from a gmail account and it works.
To sum up, here are my questions:
1. Why am I getting the (401) Unauthorized errors when I try to read my Inbox?
2. Does Office 365 expect me to use my domain account or my mailbox name for user credentials?
3. In the domain part of the WebCredentials statement, do I use my company's mycorp.com or instead do I use Office 365's domain outlook.office365.com?
If you have read this far, many thanks!
I found the answer to the problem above, so I'll share here in case anyone needs it. The problem is down to security protocols added by my IT organization that were not documented because of recent phishing attacks on our company.
The first clue was that IT stated if I want to check company email on my personal 4G device like smartphone or ipad, I must also install Microsoft InTune for MFA connection to the O365 mail server. Since I am unaware of how to integrate InTune into my .Net app, I had to look for another answer.
I requested and was approved to move my functional mailbox from O365 to an on-premise Exchange server for this app. That solved the authentication problem.

vb.net: listbox.items.add() throws exception in same class

I'm not even sure I understand this situation enough to come up with a proper title. I come from a modest understanding of VB6 and having to climb a steep learning curve for VB 2010.
I am trying to create a multi-client server program that will communicate with my Enterprise iPhone app. I found a relatively simple example to build upon here: http://www.strokenine.com/blog/?p=218. I have been able to modify the code enough to make it work with my app, but with one glitch: I can't get access to the controls on the form to add items, even though the method is invoked within the form's class. (I tried this on the original code too, and it does the same thing. I don't know how the author managed to get it to work.)
Here's the code segment in question:
Public Class Server 'The form with the controls is on/in this class.
Dim clients As New Hashtable 'new database (hashtable) to hold the clients
Sub recieved(ByVal msg As String, ByVal client As ConnectedClient)
Dim message() As String = msg.Split("|") 'make an array with elements of the message recieved
Select Case message(0) 'process by the first element in the array
Case "CHAT" 'if it's CHAT
TextBox3.Text &= client.name & " says: " & " " & message(1) & vbNewLine 'add the message to the chatbox
sendallbutone(message(1), client.name) 'this will update all clients with the new message
' and it will not send the message to the client it recieved it from :)
Case "LOGIN" 'A client has connected
clients.Add(client, client.name) 'add the client to our database (a hashtable)
ListBox1.Items.Add(client.name) 'add the client to the listbox to display the new user
End Select
End Sub
Under Case "LOGIN" the code tries to add the login ID to the listbox. It throws an exception: "A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll" The listbox (all controls, for that matter) is in the same class, Server.vb and Server.vb [Design].
The data comes in from another class that is created whenever a client logs on, which raises the event that switches back to the Server class:
Public Class ConnectedClient
Public Event gotmessage(ByVal message As String, ByVal client As ConnectedClient) 'this is raised when we get a message from the client
Public Event disconnected(ByVal client As ConnectedClient) 'this is raised when we get the client disconnects
Sub read(ByVal ar As IAsyncResult) 'this will process all messages being recieved
Try
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ex As Exception
Try 'if an error occurs in the reading purpose, we will try to read again to see if we still can read
Dim sr As New StreamReader(cli.GetStream) 'initialize a new streamreader which will read from the client's stream
Dim msg As String = sr.ReadLine() 'create a new variable which will be used to hold the message being read
RaiseEvent gotmessage(msg, Me) 'tell the server a message has been recieved. Me is passed as an argument which represents
' the current client which it has recieved the message from to perform any client specific
' tasks if needed
cli.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf read, Nothing) 'continue reading from the stream
Catch ' IF WE STILL CANNOT READ
RaiseEvent disconnected(Me) 'WE CAN ASSUME THE CLIENT HAS DISCONNECTED
End Try
End Try
End Sub
I hope I am making sense with all this. It all seems to bounce back and forth, it seems so convoluted.
I've tried using Me.listbox1 and Server.listbox1 and several other similar structures, but to no avail.
I'm reading a lot about Invoke and Delegates, but would that be necessary if the method and the control are in the same class? Or do I have a fundamental misperception of what a class is?
Many thanks for any help I can get.
Private Delegate Sub UpdateListDelegate(byval itemName as string)
Private Sub UpdateList(byval itemName as string)
If Me.InvokeRequired Then
Me.Invoke(New UpdateListDelegate(AddressOf UpdateList), itemName)
Else
' UpdateList
' add list add code
ListBox1.Items.Add(itemName)
End If
End Sub
Add above, then replace:
ListBox1.Items.Add(client.name)
to
UpdateList(client.name)
Does it work? check the syntax, may have typo as I type it.

Can I add an Outlook Attachment from a PictureBox image?

I have an image in my VB.NET Picture box. I would like to attach it to the email message I'm sending through Outlook without having to save it to the drive anywhere. Is it possible to do such a thing?
Here's what I have so far (taken from here):
Public Class email
Dim app As Microsoft.Office.Interop.Outlook.Application
Dim appNameSpace As Microsoft.Office.Interop.Outlook._NameSpace
Dim memo As Microsoft.Office.Interop.Outlook.MailItem
Dim outbox As Microsoft.Office.Interop.Outlook.MAPIFolder
Public Sub New(ByVal attachment)
Try
app = New Microsoft.Office.Interop.Outlook.Application
appNameSpace = app.GetNamespace("MAPI")
appNameSpace.Logon(Nothing, Nothing, False, False)
memo = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
memo.To = "notmy#realemailaddress.com"
memo.Subject = "Testing"
memo.Body = "Hello there"
memo.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
memo.Send()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
When I comment out the attachment line it works perfectly fine, otherwise it throws a COMError. I haven't been able to find any real good information about attaching an email that way, or if it's even possible. If I can't do it this way I plan on just saving the file to some random(ish) name in C:\TEMP\, but it would be nicer if I didn't have to worry about that.
Thanks for any help
Here are your options: Attachment Types
The source information here can help also:Attachment Add Function