使用System.Net.Mail.MailAddress或其他库指定.eml文件名

时间:2015-05-01 13:46:11

标签: c# vb.net

我需要在要求时提交电子邮件。

我的代码有效:

  • 发送电子邮件
  • 请求时提交电子邮件
  • 但不允许我指定文件名(使用guid作为文件名)
  • 示例:c:\ Archive \ email \ 1003d05d-11ca-45e2-a5f4-cf2da29c39d9.eml

潜在解决方案:

  1. 将文件保存到临时文件夹,重命名文件,然后复制到最终目的地
  2. 使用其他方法保存文件,提高性能
  3. 优点和缺点

    • 解决方案1:丑陋且性能不佳

    问题

    是否有人知道如何向" MySpecifiedFileName.eml"提交电子邮件,而无需重命名然后复制?

    现有代码:

    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
    

1 个答案:

答案 0 :(得分:7)

CodeProject.com上的Allan Eagle创建了一个extension of the System.Net.Mail.MailMessage class,其中包含使用特定文件名保存电子邮件的功能。我相信这会解决你提出的问题。