Example of Sending email with attachments in C#

本文详细解释了如何使用ContentDisposition类为邮件附件设置元数据,包括创建、修改和获取邮件消息中附件的元信息,如创建日期、修改日期、最后访问日期、文件名、大小和推荐文件名等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//file path
string file_ddr_service_agreement = HttpContext.Current.Server.MapPath(@"~\service_solutions\freedom_to_learn\FTL_online\myorder\schoolpages\Direct_Debit_Agreement-2013\") + "Computelec DDR Agreement.pdf";

//attachment object
Attachment ddr_service_agreement = new Attachment(file_ddr_service_agreement,MediaTypeNames.Application.Pdf);
ContentDisposition disposition = ddr_service_agreement.ContentDisposition;

//setting attachment info for each attachment
disposition.CreationDate = File.GetCreationTime(file_ddr_service_agreement);
disposition.ModificationDate = File.GetLastWriteTime(file_ddr_service_agreement);
disposition.ReadDate = File.GetLastAccessTime(file_ddr_service_agreement);
disposition.FileName = Path.GetFileName(file_ddr_service_agreement);
disposition.Size = new FileInfo(file_ddr_service_agreement).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;

if (ddr_service_agreement != null)
{
   //add the attachment
   mailMessage.Attachments.Add(ddr_service_agreement);
}


Explanation of ContentDisposition class from MSDN:


The information in the ContentDisposition class accompanies an e-mail message that contains attachments when the e-mail message is sent to its destination. The information inContentDisposition can be used by software that displays e-mail to present the e-mail attachments in the manner intended by the sender.

E-mail messages are created using instances of the MailMessage class. Instances of the Attachment class are used to add attachments to e-mail messages. To modify theContentDisposition for an attachment, get the instance from the Attachment.ContentDisposition property.

Content to be displayed as part of the message body has the disposition type of Inline. Content that is not displayed but is attached in a separate file has the disposition type ofAttachment. Use the Inline property to control the disposition type for the attachment associated with an instance of ContentDisposition.

For file attachments, you can use the properties of the ContentDisposition to set the file size, as well as the date the file was created, last read, and last modified. For all attachments, you can set a recommended file name in the event that the attachment is stored on the receiving computer.

The ToString method returns the Content-Disposition header. The Content-Disposition header is described in RFC 2183 available at http://www.ietf.org.

Reference:

http://msdn.microsoft.com/library/vstudio/system.net.mime.contentdisposition


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值