I need to file an email when requested.
My code below works:
Potential Solutions:
Pros and Cons
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With