C # The email attachment name is blank,
Sample Code:
// 1. create MailMessage mailMsg = new MailMessage (); mailMsg. to. add (new MailAddress ("test@test.com"); // 2. set the mail title, body, and other information mailMsg. headersEncoding = Encoding. getEncoding ("gb2312"); mailMsg. subjectEncoding = Encoding. getEncoding ("gb2312"); mailMsg. bodyEncoding = Encoding. getEncoding ("gb2312"); // email Content Encoding mailMsg. subject = emailTitleStr; // mail title mailMsg. body = emailBodyStr; mailMsg. isBodyHtml = true; // whether the email content supports htmlmailMsg. priority = MailPriority. high; // attachment: In. net 4.0, when the attachment name contains a special name, the recipient will receive the attachment mailMsg whose name is null. attachments. add (new Attachment ("D: \ 6).txt"); // 3. create SMTP send SmtpClient smtp = new SmtpClient (emailSMTP, emailPort); smtp. credentials = new NetworkCredential (emailName. address, emailPwd); smtp. deliveryMethod = SmtpDeliveryMethod. network; smtp. send (mailMsg );
Problem description:When you use SmtpClient to add an attachment, if the attachment name is too long or contains special characters, the name of the attachment received by the email recipient is blank.
Cause:. Net 4.0 Bug.
Solution: Install the. net 4.5 or above class library.
. NET Framework 4.5:
Official Website: Https://www.microsoft.com/zh-cn/download/details.aspx? Id = 30653
Baidu: Http://rj.baidu.com/soft/detail/22370.html
====================================== Series of articles ==============================================
This article: 2.3 C # The Name Of The email attachment to be sent is blank
C # Article navigation