vb.netemailsmtpclientopenpop

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.


Solution

  • The solution is in the edited AddAttachments function in my original post above.