添加附件到.NET电子邮件附件、电子邮件、NET

2023-09-03 16:13:11 作者:单身闯天下

我如何将文件附加一个非常不友好的名称(如在它的会话ID号文件),但它附加的另一个名字吗?

有问题的文件名具有会话ID中,以避免冲突的名字在Web服务器上,但是当我将其附加到文件中,一个更加友好的名字是preferable。

有没有办法与不友好的名称附加的文件为其他名称,这样,当用户得到的电子邮件,他可以告诉什么文件的内容是什么名字?我讨厌不得不创造一个独特的文件夹,只是把一个非唯一的文件名,在它简单地将其连接到电子邮件的目的。

 昏暗mailMessage作为System.Net.Mail.MailMessage =新System.Net.Mail.MailMessage()
mailMessage.Attachments.Add(C:\我\ code \ BESI \ BESI \文件\发票djopyynrgek4p4qadn31dxxs.pdf,????)
 

解决方案

是的 - 你可以做你想做通过使用不同的构造附件是什么()。可悲的是有没有一个需要一个文件名和一个独立的名字,但有一个需要和一个独立的名字。并且存在有System.IO.File 辅助方法,可以很容易得到从文件名的文件流。

 进口System.Net.Mail
进口System.IO

昏暗mailMessage为MailMessage =新MailMessage()
昏暗的流作为的FileStream = File.OpenRead(C:\我\ code \ BESI \ BESI \文件\发票djopyynrgek4p4qadn31dxxs.pdf)
昏暗的附件作为附件=新的附件(流FriendlyName.pdf)
mailMessage.Attachments.Add(附件)

一定要处置的流你发送的邮件后。
 

(我的VB语法较弱,所以有可能在有一些愚蠢的错误,但我希望我的意思是明确的,那些是绝对正确的类/方法使用。)

苹果6手机自带邮箱为什么看不到添加附件的邮件

How do I attach a file with a very unfriendly name (like a file with a session Id number in it) but have it attached as another name?

The file name in question has the session ID in it to avoid clashes name on the web server but when I attach it to the file, a friendlier name is preferable.

Is there a way to attach the file with the unfriendly name as another name so that when the user gets the email he can tell from the name what the content of the file is? I'd hate to have to create a unique folder just to put a non unique file name in it for the purpose of simply attaching it to an email.

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.Attachments.Add("C:\My\Code\BESI\BESI\Files\Invoice-djopyynrgek4p4qadn31dxxs.pdf", ????)

解决方案

Yeah--you can do what you're trying to do by using a different constructor for Attachment(). Sadly there's not one that takes a filename and a separate name, but there is one that takes a Stream and a separate name. And there are helper methods on System.IO.File that make it easy to get a file stream from a file name.

Imports System.Net.Mail
Imports System.IO

Dim mailMessage as MailMessage = New MailMessage()
Dim stream as FileStream = File.OpenRead("C:\My\Code\BESI\BESI\Files\Invoice-djopyynrgek4p4qadn31dxxs.pdf")
Dim attachment as Attachment = New Attachment(stream, "FriendlyName.pdf")
mailMessage.Attachments.Add(attachment)

'Be sure to Dispose stream after you've sent the mail.

(My VB syntax is weak, so there may be some silly mistakes in there, but hopefully my meaning is clear, and those are definitely the right classes/methods to use.)