c#-通过电子邮件发送带有长名称和重音的附件
作者:互联网
发送包含包含长名和重音的附件的电子邮件时,我们遇到了问题.
大小或类型似乎不影响结果(我尝试使用txt& pdf,300kb和3833kb的大小)
经过一番搜索后,我发现此帖子http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55显示了如何解决该问题.
如果我的名字真的很长(请参阅attachment2变量名),它将起作用.
但是,如果名称略短(请参见附件1变量名称),则该名称显示为-= _ iso-8859-1_Q_Example,_Example_and_other_
我认为这与SplitEncodedAttachmentName中的以下行有关
encodeAttachmentName = encodeAttachmentName.Remove(encodedAttachmentName.Length-encodingtoken.Length,encodingtoken.Length);
这是守望先锋的手表
attach1-“ =?ISO-8859-1?Q?Example = 2c_Example_and_other_Repr = e9senta.txt?=”
attach2-“ =?ISO-8859-1?Q?Example = 2c_Example_and_other_Repr = e9sentant_Example_Examp?==?ISO-8859-1?Q?le_Example_Example_Example_Example_Example2.txt?=”
我似乎无法正确发送附件1.
(我过去确实回答过这个问题-MailMessage Attachment filename with accents,但是我有一个错误,经过重构后遇到了这个问题.)
这是导致错误的最小代码示例.
class Program
{
static void Main(string[] args)
{
Attachment attachment = new Attachment(@"c:\client\temp\Example,_Example_and_other_Représenta.pdf"); //3488kb
Attachment attachment2 = new Attachment(@"c:\client\temp\Example,_Example_and_other_Représentant_Example_Example_Example_Example_Example_Example.pdf"); //3488kb
Console.WriteLine(attachment.Name);
MailMessage mm = new MailMessage();
mm.From = new MailAddress("toemail");
mm.To.Add("toemail");
mm.Subject = "Yo";
mm.Body = "hello";
mm.Attachments.Add(CreateAttachment(attachment, attachment.Name)); // =_iso-8859-1_Q_Example,_Example_and_other_
mm.Attachments.Add(CreateAttachment(attachment2, attachment2.Name));
SmtpClient smtp = new SmtpClient("SmptServer");
smtp.Send(mm);
}
/// <summary>
/// This method fixes the name of the attachment to allow accents
/// </summary>
/// <remarks>Taken from http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55</remarks>
public static Attachment CreateAttachment(Attachment attachmentFile, string displayName)
{
Attachment attachment = attachmentFile;
attachment.TransferEncoding = TransferEncoding.Base64;
string tranferEncodingMarker = "Q";
string encodingMarker = "ISO-8859-1";
int maxChunkLength = 76;
attachment.NameEncoding = Encoding.GetEncoding(encodingMarker);
string encodingtoken = String.Format("=?{0}?{1}?", encodingMarker, tranferEncodingMarker);
string softbreak = "?=";
string encodedAttachmentName = encodingtoken;
encodedAttachmentName = HttpUtility.UrlEncode(displayName, Encoding.Default).Replace("+", " ").Replace("%", "=");
encodedAttachmentName = SplitEncodedAttachmentName(encodingtoken, softbreak, maxChunkLength, encodedAttachmentName);
attachment.Name = encodedAttachmentName;
return attachment;
}
private static IEnumerable<string> SplitByLength(string stringToSplit, int length)
{
while (stringToSplit.Length > length)
{
yield return stringToSplit.Substring(0, length);
stringToSplit = stringToSplit.Substring(length);
}
if (stringToSplit.Length > 0)
{
yield return stringToSplit;
}
}
private static string SplitEncodedAttachmentName(string encodingtoken, string softbreak, int maxChunkLength, string encoded)
{
int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2);
var parts = SplitByLength(encoded, splitLength);
string encodedAttachmentName = encodingtoken;
foreach (var part in parts)
{
encodedAttachmentName += part + softbreak + encodingtoken;
}
encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length);
return encodedAttachmentName;
}
}
解决方法:
以及一篇kb的文章,以解决带有特殊字符的长名称的最初发行.
http://support.microsoft.com/kb/2402064
安装kb后,
我变了
mm.Attachments.Add(CreateAttachment(attachment,attachment.Name));
mm.Attachments.Add(CreateAttachment(attachment2,attachment2.Name));
回到应该是什么
mm.Attachments.Add(attachment);
mm.Attachments.Add(attachment2);
而且一切正常.
标签:email-attachments,encoding,email,diacritics,c 来源: https://codeday.me/bug/20191101/1982701.html