2011-03-18 19 views
6

मैं संलग्न एक्सेल फ़ाइल नामों के साथ HTML ई-मेल भेजने की कोशिश कर रहा हूं। यह सब अच्छी तरह से काम जब तक मैं संदेशों जिसका लगाव नाम हर वैकल्पिक हल मैं कोशिश की है :-(पत्र उच्चारण होता है बुरी तरह विफल रहा है भेजने के लिए nededलहजे के साथ मेलमेसेज अटैचमेंट फ़ाइल नाम

मूल कोड:।

var attachment = new Attachment(
     new MemoryStream(excelFileContents), 
     "simplefilename.xls"); 

यह एक ठीक काम करता है लेकिन अगर। मैं "simplefilename.xls" "échec.xls" द्वारा, लगाव garbaged है (नाम और सामग्री) की जगह

मैं इन की कोशिश की, कोई लाभ नहीं हुआ।

var attachment = new Attachment(
     new MemoryStream(excelFileContents), 
     new System.Net.Mime.ContentType("application/vnd.ms-excel")); 
    attachment.Name = "échec.xls"; 

पिछले एक भी बदतर है: SmtpClient.Send() एक अपवाद फेंकता है, फ़ाइल नाम में é के बारे में शिकायत:

var attachment = new Attachment(
     new MemoryStream(excelFileContents), 
     new System.Net.Mime.ContentType("application/vnd.ms-excel")); 
    attachment.ContentDisposition.FileName = "échec.xls"; 

मैं जिस तरह से लंबे समय के लिए इस पर मेरे सिर पीटने गया है। किसी भी रोशनी का गर्मजोशी से स्वागत है!

+0

मुझे लगता है कि आप फ़ाइल नाम के लिए एन्कोडिंग कहीं निर्दिष्ट कर सकते हैं। लेकिन विशेष वर्णों के साथ सामान्य फ़ाइल नामों में विंडोज सिस्टम पर बहुत सी समस्याएं हो सकती हैं जहां एएनएसआई कोडपेज में उन्हें शामिल नहीं किया जाता है। इसलिए जहां भी संभव हो मैं उनसे बचूंगा। – CodesInChaos

+0

@CodeInChaos: मैं वास्तव में NameEncoding की कोशिश की है कोई लाभ नहीं हुआ। फ़ाइल नाम में लहजे के उपयोग के बारे में, मैं एक बहुत अलग राय है: वे अधिक पठनीय हैं और स्पष्ट रूप से जो लोग उन्हें पढ़ सकते हैं द्वारा इस्तेमाल किया जाएगा, इसलिए सही एएनएसआई कोड पृष्ठ पर है। फिर भी धन्यवाद। –

उत्तर

4

आप लगाव नाम के लिए Quoted-Printable प्रारूप का उपयोग करने की जरूरत है:

+0

धन्यवाद जोहान। मैंने वास्तव में देखा है कि Outlook उसी प्रकार के ई-मेल सफलतापूर्वक भेजने के लिए कोट-प्रिंट करने योग्य का उपयोग करता है। मैं आपके पहले लिंक में दिए गए समाधान में खोद दूंगा। –

6

मैं अंत में एक जवाब है कि काम किया बारे में जाना।

http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55

सामग्री मार्सेल रोमा द्वारा पोस्ट के अलावा जर्मन में है। मैंने अपने कोड में अपने कोड में डाल दिया। मैं उच्चारण के साथ पीडीएफ भेजने में सक्षम था और हम कचरे के बजाय लगाव देखना था।

तो यहाँ यह है:

public class AttachmentHelper 
{ 
    public static System.Net.Mail.Attachment CreateAttachment(string attachmentFile, string displayName, TransferEncoding transferEncoding) 
    { 
     System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachmentFile); 
     attachment.TransferEncoding = transferEncoding; 

     string tranferEncodingMarker = String.Empty; 
     string encodingMarker = String.Empty; 
     int maxChunkLength = 0; 

     switch (transferEncoding) 
     { 
      case TransferEncoding.Base64: 
       tranferEncodingMarker = "B"; 
       encodingMarker = "UTF-8"; 
       maxChunkLength = 30; 
       break; 
      case TransferEncoding.QuotedPrintable: 
       tranferEncodingMarker = "Q"; 
       encodingMarker = "ISO-8859-1"; 
       maxChunkLength = 76; 
       break; 
      default: 
       throw (new ArgumentException(String.Format("The specified TransferEncoding is not supported: {0}", transferEncoding, "transferEncoding"))); 
     } 

     attachment.NameEncoding = Encoding.GetEncoding(encodingMarker); 

     string encodingtoken = String.Format("=?{0}?{1}?", encodingMarker, tranferEncodingMarker); 
     string softbreak = "?="; 
     string encodedAttachmentName = encodingtoken; 

     if (attachment.TransferEncoding == TransferEncoding.QuotedPrintable) 
      encodedAttachmentName = HttpUtility.UrlEncode(displayName, Encoding.Default).Replace("+", " ").Replace("%", "="); 
     else 
      encodedAttachmentName = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName)); 

     encodedAttachmentName = SplitEncodedAttachmentName(encodingtoken, softbreak, maxChunkLength, encodedAttachmentName); 
     attachment.Name = encodedAttachmentName; 

     return attachment; 
    } 

    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; 
    } 

    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; 
    } 
} 

निम्नलिखित तरीके से यह प्रयोग करें:

static void Main(string[] args) 
{ 
    string smtpServer = String.Empty; 
    string userName = String.Empty; 
    string password = String.Empty; 
    string attachmentFilePath = String.Empty; 
    string displayName = String.Empty; 

    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpServer); 
    client.Credentials = new System.Net.NetworkCredential(userName, password); 

    var msg = new System.Net.Mail.MailMessage(fromAddress, toAddress, "Subject", "Body"); 
    System.Net.Mail.Attachment attachment = 
     AttachmentHelper.CreateAttachment(attachmentFilePath, displayName, TransferEncoding.Base64); 

    msg.Attachments.Add(attachment); 
    client.Send(msg); 
} 
संबंधित मुद्दे