开发者

Removing file path from attachment name when using MailMessage

开发者 https://www.devze.com 2023-02-19 00:55 出处:网络
I am trying to send an attachment using MailMessage class. var attachment = new Attachment(ConfigurationManager.AppSettings[\"pathToPDFs\"] + pdfItem.Value);

I am trying to send an attachment using MailMessage class.

var attachment = new Attachment(ConfigurationManager.AppSettings["pathToPDFs"] + pdfItem.Value);
mailMessage.Attachments.Add(attachment);

This adds the file as an attachment but when I receive the file on my email th开发者_开发问答e name is

//Inetpub//Path//To//pdf//name.pdf it should be just name.pdf.

Where am I going wrong?

ConfigurationManager.AppSettings["pathToPDFs"] is set as C://Inetpub//Path//To//pdf// pdfItem.Value is name.pdf


When you receive the e-mail attachment, is it the correct attachment?

Meaning, do you actually get the name.pdf?

Also, your location should be: C:\\inetpub\\path\\to\\pdf\name.pdf

Have you tried hard-coding the file location and seeing if that fixes your problem?


I have to confess first, I am guessing.

The constructor may be using the full path as the name. If you set the name property after creating the object if might work.

var attachment = new Attachment(ConfigurationManager.AppSettings["pathToPDFs"] + pdfItem.Value); 

attachment.Name = pdfItem.Value;

mailMessage.Attachments.Add(attachment);


I ran into the same problem, and later on I found that it was because I used the relative path of the file, i.e. ../../name.pdf. If I get the full path to create the attachment, then the attachment file name in the email doesn't contain the path.

string full_file_name = Path.GetFullPath("../../name.pdf");
Attachment attachment = new Attachment(full_file_name);
mailMessage.Attachments.Add(attachment);
0

精彩评论

暂无评论...
验证码 换一张
取 消