指定使用System.Net.Mail.MailAddress或其他图书馆.eml文件名 [英] Specify .eml file name using System.Net.Mail.MailAddress or other library
本文介绍了指定使用System.Net.Mail.MailAddress或其他图书馆.eml文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要请求时文件的电子邮件
我下面的代码工作:
- 发送请求时,电子邮件
- 文件的电子邮件
- 但是不允许我指定的文件名(使用GUID作为文件名)
- 例如:C:\Archive\email\1003d05d-11ca-45e2-a5f4-cf2da29c39d9.eml
可能的解决方案:
- 将文件保存到临时文件夹,重命名文件,然后复制到最终目标
- 使用另一种方法,更好的性能
保存文件
优点和缺点
- 解决方案1:是丑陋,有坏的表现
问
有谁知道如何提交电子邮件至MySpecifiedFileName.eml,而无需重新命名,然后复制
现有代码:
公共共享子发(BYVAL EmailFrom作为字符串,BYVAL EmailTo作为字符串,BYVAL主题作为字符串,BYVAL HTMLBody作为字符串,可选的SaveToFile由于布尔=假,可选SAVEFILEPATH的String =)
昏暗MyMsg作为MAILMESSAGE =新MAILMESSAGE
昏暗的接收者()作为字符串
收件人=斯普利特(EmailTo; )
随着MyMsg
。从=新System.Net.Mail.MailAddress(EmailFrom)
。对于i = 0到Recipients.Count - 1
。如果收件人(I)的ToString<> 那
.To.Add(新System.Net.Mail.MailAddress(收件人(I)))
端如果
下一步
.Sender =新System.Net。 Mail.MailAddress(EmailFrom)
.Subject =主题
=。体HTMLBody
.BodyEncoding = System.Text.Encoding.UTF8
.IsBodyHtml = TRUE
.Priority = MailPriority.High
结束与
昏暗SMTPSERVER作为新SmtpClient(My.Settings(SMTPSERVER))
SmtpServer.Send(MyMsg)
REM
REM保存电子邮件时,请
REM
。如果的SaveToFile = true,那么
昏暗的客户端作为新的SmtpClient(My.Settings(SMTPSERVER))
客户端。 DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory
client.PickupDirectoryLocation = SAVEFILEPATH
client.Send(MyMsg)
客户端=什么
端如果
MyMsg =什么
SMTPSERVER =没有什么
端子
解决方案
阿伦鹰在在CodeProject.com创造的一个扩展System.Net.Mail.MailMessage类,其中包括保存电子邮件与特定文件名的能力。我相信这将解决您提出的问题。
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:
- Save the file to a temporary folder, rename file, and then copy to final destination
- 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
解决方案
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.
这篇关于指定使用System.Net.Mail.MailAddress或其他图书馆.eml文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文