12

I need to file an email when requested.

My code below works:

  • Sends email
  • Files the email when requested
  • But does not allow me to specify the file name (uses a guid as file name)
  • Example: c:\Archive\email\1003d05d-11ca-45e2-a5f4-cf2da29c39d9.eml

Potential Solutions:

  1. Save the file to a temporary folder, rename file, and then copy to final destination
  2. Save the file using another method, better performance

Pros and Cons

  • Solution 1: is ugly and has bad performance

Question

Does anyone know how to file an email to "MySpecifiedFileName.eml", without having to rename and then copy?

Existing Code:

Public Shared Sub Send(ByVal EmailFrom As String, ByVal EmailTo As String, ByVal Subject As String, ByVal HTMLBody As String, Optional SaveToFile As Boolean = False, Optional SaveFilepath As String = "")
    Dim MyMsg As MailMessage = New MailMessage

    Dim Recipients() As String
    Recipients = Split(EmailTo, ";")

    With MyMsg
        .From = New System.Net.Mail.MailAddress(EmailFrom)
        For i = 0 To Recipients.Count - 1
            If Recipients(i).ToString <> "" Then
                .To.Add(New System.Net.Mail.MailAddress(Recipients(i)))
            End If
        Next
        .Sender = New System.Net.Mail.MailAddress(EmailFrom)
        .Subject = Subject
        .Body = HTMLBody
        .BodyEncoding = System.Text.Encoding.UTF8
        .IsBodyHtml = True
        .Priority = MailPriority.High            
    End With

    Dim SmtpServer As New SmtpClient(My.Settings("SMTPServer"))
    SmtpServer.Send(MyMsg)

    REM
    REM Save Email when requested
    REM
    If SaveToFile = True Then
        Dim client As New SmtpClient(My.Settings("SMTPServer"))
        client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory
        client.PickupDirectoryLocation = SaveFilepath
        client.Send(MyMsg)
        client = Nothing
    End If
    MyMsg = Nothing
    SmtpServer = Nothing
End Sub
Internet Engineer
  • 2,514
  • 8
  • 41
  • 54
  • 2
    See http://stackoverflow.com/questions/1264672/how-to-save-mailmessage-object-to-disk-as-eml-or-msg-file for one possible solution. You can save it to a stream and then you can save the stream to the filename of your choice. – Tony Hinkle May 01 '15 at 13:56
  • 1
    If this is going to be single-threaded (i.e., only one email being saved at any given time), you can save it to a temporary directory, and then rename and move the file to the permanent final destination. If client.send returns the filename, you can just rename it. – Tony Hinkle May 01 '15 at 13:59
  • You should also be [aware of difference in the `eml` file format](http://stackoverflow.com/a/6603002/231316). IIS saves files with this extension and Outlook reads files with this extension but there are differences. – Chris Haas May 06 '15 at 16:04
  • Good to know. Do you have a link that I could read? – Internet Engineer May 06 '15 at 16:10
  • 3
    Not sure if you've seen [this article](http://www.codeproject.com/Articles/32434/Adding-Save-functionality-to-Microsoft-Net-Mail-Ma) in your research, but it provides a save method (with filename) for the .Net Mail system. Note that there is an update at the bottom of the code for Net 4.5. – ChicagoMike May 07 '15 at 13:01
  • Should @ChicagoMike post the core parts of the CodeProject article as an answer? Do you need extra help or are you satisfied with that solution? – Jeremy Thompson May 10 '15 at 06:20
  • 1
    I think the only way to win a bounty is to post the answer. Yes I am satisfied with the answer. Thank you all. – Internet Engineer May 10 '15 at 17:45

1 Answers1

7

Allan Eagle over at CodeProject.com has created an extension of the System.Net.Mail.MailMessage class that includes the ability to save an email with a specific file name. I believe this will address the issue you raised.

ChicagoMike
  • 618
  • 3
  • 11