-
-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MimeMessage::ToString() throwing System.NullReferenceException #135
Comments
BodyParts can never return null. Can you email me the message that is causing this so I can see for myself what is really going on? |
Here is one of the messages, it is a draft, converted from an MSG file, I have enclosed the code used to create the MimeMessage from the MSG. Public Shared Function ConvertToMimeKit(ByRef message As Independentsoft.Msg.Message) As MimeKit.MimeMessage
Dim mkMessage As MimeKit.MimeMessage = Nothing
If message.TransportMessageHeaders IsNot Nothing Then
Dim transportMessageHeaders As String = message.TransportMessageHeaders
If message.TransportMessageHeaders.StartsWith("Microsoft Mail Internet Headers") Then
transportMessageHeaders = message.TransportMessageHeaders.Substring(message.TransportMessageHeaders.IndexOf(vbCrLf) + 2)
End If
If Not transportMessageHeaders.EndsWith(vbCr & vbLf & vbCr & vbLf) Then
transportMessageHeaders += vbCr & vbLf & vbCr & vbLf
End If
Dim codePageEncoding As System.Text.Encoding = GetEncoding(message.InternetCodePage)
If Not codePageEncoding.IsSingleByte Then
codePageEncoding = Encoding.UTF8
End If
Dim transportMessageHeadersBuffer As Byte() = codePageEncoding.GetBytes(transportMessageHeaders)
Using s As Stream = New MemoryStream(codePageEncoding.GetBytes(transportMessageHeaders))
mkMessage = MimeKit.MimeMessage.Load(s)
End Using
Else
mkMessage = New MimeKit.MimeMessage()
End If
Dim mkMailBox As MimeKit.MailboxAddress
For Each recipient As Independentsoft.Msg.Recipient In message.Recipients
If String.IsNullOrWhiteSpace(recipient.DisplayName) Then
If String.IsNullOrWhiteSpace(recipient.SmtpAddress) Then
mkMailBox = New MimeKit.MailboxAddress(recipient.SmtpAddress, recipient.SmtpAddress)
Else
mkMailBox = New MimeKit.MailboxAddress(recipient.EmailAddress, recipient.EmailAddress)
End If
Else
If String.IsNullOrWhiteSpace(recipient.SmtpAddress) Then
mkMailBox = New MimeKit.MailboxAddress(recipient.DisplayName, recipient.EmailAddress)
Else
mkMailBox = New MimeKit.MailboxAddress(recipient.DisplayName, recipient.SmtpAddress)
End If
End If
If mkMailBox IsNot Nothing AndAlso mkMailBox.Address IsNot Nothing Then
Select Case recipient.RecipientType
Case RecipientType.To
mkMessage.To.Add(mkMailBox)
Case RecipientType.Cc
mkMessage.Cc.Add(mkMailBox)
Case RecipientType.Bcc
mkMessage.Bcc.Add(mkMailBox)
Case RecipientType.P1
Case RecipientType.None
Case Else
End Select
End If
Next
If mkMessage.From Is Nothing Then
If String.IsNullOrWhiteSpace(message.SenderName) Then
If message.InternetAccountName IsNot Nothing AndAlso message.InternetAccountName.Contains("@") Then
mkMailBox = New MimeKit.MailboxAddress(message.SenderEmailAddress, message.InternetAccountName)
Else
mkMailBox = New MimeKit.MailboxAddress(message.SenderEmailAddress, message.SenderEmailAddress)
End If
Else
If message.InternetAccountName IsNot Nothing AndAlso message.InternetAccountName.Contains("@") Then
mkMailBox = New MimeKit.MailboxAddress(message.SenderName, message.InternetAccountName)
Else
mkMailBox = New MimeKit.MailboxAddress(message.SenderName, message.SenderEmailAddress)
End If
End If
mkMessage.From.Add(mkMailBox)
End If
If message.Subject IsNot Nothing Then mkMessage.Subject = message.Subject
mkMessage.[Date] = message.ClientSubmitTime
If mkMessage.MessageId Is Nothing Then
Try ' Some ID's do not comply
mkMessage.MessageId = message.InternetMessageId
Catch
End Try
End If
Dim BodyBuild As New MimeKit.BodyBuilder()
If message.Body IsNot Nothing Then BodyBuild.TextBody = message.Body
If message.BodyHtml IsNot Nothing Then BodyBuild.HtmlBody = message.BodyHtmlText
For Each Attachment As Independentsoft.Msg.Attachment In message.Attachments
Dim mkEntity As New MimeKit.MimePart
If Attachment.Data IsNot Nothing Then
If Attachment.MimeTag IsNot Nothing Then
MimeKit.ContentType.TryParse(Attachment.MimeTag, mkEntity.ContentType)
End If
Try ' Some ID's do not comply
mkEntity.ContentId = Attachment.ContentId
Catch
End Try
Uri.TryCreate(Attachment.ContentLocation, UriKind.RelativeOrAbsolute, mkEntity.ContentLocation)
If Not String.IsNullOrWhiteSpace(Attachment.LongFileName) Then
mkEntity.FileName = Attachment.LongFileName
ElseIf Not String.IsNullOrWhiteSpace(Attachment.DisplayName) Then
mkEntity.FileName = Attachment.DisplayName
ElseIf Not String.IsNullOrWhiteSpace(Attachment.FileName) Then
mkEntity.FileName = Attachment.FileName
End If
mkEntity.ContentObject = New MimeKit.ContentObject(Attachment.GetStream(), MimeKit.ContentEncoding.Default)
mkEntity.ContentTransferEncoding = MimeKit.ContentEncoding.Base64
If mkEntity.IsAttachment Then
BodyBuild.Attachments.Add(mkEntity)
Else
BodyBuild.LinkedResources.Add(mkEntity)
End If
End If
Next
mkMessage.Body = BodyBuild.ToMessageBody
Return mkMessage
End Function From: Jeffrey Stedfast [mailto:[email protected]] BodyParts can never return null. Can you email me the message that is causing this so I can see for myself what is really going on? — |
I do all of my development on a Mac, so don't have access to a VB.NET compiler... could you just send me the results of |
I have attached it and the text is below as well. From: From: Jeffrey Stedfast [mailto:[email protected]] I do all of my development on a Mac, so don't have access to a VB.NET compiler... could you just send me the results of mkMessage.WriteTo ("message.txt") ? — |
It doesn't throw an exception for me. Which version of MimeKit are you using? |
I am running 4.0.30319. I think it might be an issue with one of the internal Try converts getting trapped by the debugger. Microsoft has a way of hooking into and catching errors before the bubble up to the registered error handler making it look like an unhandled exception. Since I am processing a PST file with a few thousand messages I will reprocess it and see if I can get all the details. Oh and here is the VB code as C#. public static MimeKit.MimeMessage ConvertToMimeKit(ref Independentsoft.Msg.Message message)
} From: Jeffrey Stedfast [mailto:[email protected]] It doesn't throw an exception for me. Which version of MimeKit are you using? — |
There is no MimeKit version 4.0.30319. The latest is 1.0.13. |
Sorry I was looking at the runtime version under the properties, the package is reporting 1.0.13.0: [cid:[email protected]] From: Jeffrey Stedfast [mailto:[email protected]] There is no MimeKit version 4.0.30319. The latest is 1.0.13. — |
I'm going to need a test case that I can compile and run locally that reproduces this problem and/or a full stack trace from the exception you are getting because I cannot figure out how it could possibly be throwing a NullReferenceException without more information :-( |
I think I have one…the message has a plain text component, but not HTML, so the BodyBuilder HTMLBody is getting set to String.Empty. The MSG loads all the parts, and then on the call to ToString() a “Buffer cannot be null” is thrown. ? mkMessage.ToString() The WriteTo posts the same error, but handles it, here is the file out, I also used the IndependentSoft library to convert the MSG file to EML. It looks like one of the attachments is having a problem, because the IndependentSoft library generated the email with no attachments. From: Jeffrey Stedfast [mailto:[email protected]] I'm going to need a test case that I can compile and run locally that reproduces this problem and/or a full stack trace from the exception you are getting because I cannot figure out how it could possibly be throwing a NullReferenceException without more information :-( — |
Should be fixed now. |
Great, I will test if first thing in the morning…Hey, just to let you know, we process millions of emails internally to audit, and there is a 300% increase in EML ingestion, and 200% increase on MSG, PST and NSF ingestion. Looks like I need to dig in a little more and fix those other libraries. MSG and PST are both using IndependentSoft. Thank you again. Leland Carpenter ♦ Sr. Software Engineer ♦ PRGX USA, Inc. From: Jeffrey Stedfast [mailto:[email protected]] Should be fixed now. — |
Is there a new nuget release, or would I need to download and build everything? |
There's no nuget release yet, so you'll have to grab the source from github and build it (make sure to follow the README which explains what you'll need to get) |
Just released MimeKit 1.0.14 with this fix |
I have a message that when loaded has a Text part, but the BodyParts Enumeration is null, so the ToString call throws and error.
The text was updated successfully, but these errors were encountered: