2009-07-28 13 views
23

वर्तमान में मैं नीचे दी गई विधि का उपयोग कर रहा उपयोगकर्ताओं दृष्टिकोण ईमेल खाते खोलने के लिए और भेजने के लिए प्रासंगिक सामग्री के साथ एक ईमेल पॉप्युलेट करने के लिए:सी # मेलो अटैचमेंट के साथ?

public void SendSupportEmail(string emailAddress, string subject, string body) 
{ 
    Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
       + body); 
} 

मैं तथापि, एक संलग्न फाइल के साथ ईमेल को भरने के लिए सक्षम हो चाहता हूँ।

कुछ की तरह:

public void SendSupportEmail(string emailAddress, string subject, string body) 
{ 
    Process.Start("mailto:" + emailAddress + "?subject=" + subject + "&body=" 
     + body + "&Attach=" 
     + @"C:\Documents and Settings\Administrator\Desktop\stuff.txt"); 
} 

हालांकि इस काम करने के लिए प्रतीत नहीं होता। क्या किसी को इस तरह से पता है जो इसे काम करने की अनुमति देगा !?

बहुत सराहना करते हैं।

सम्मान।

उत्तर

10

मेलto: आधिकारिक तौर पर अनुलग्नकों का समर्थन नहीं करता है। मैं Outlook 2003 में सुना है इस वाक्य रचना के साथ काम करेंगे:

<a href='mailto:[email protected]?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '> 

इस संभाल करने के लिए एक बेहतर तरीका सर्वर System.Net.Mail.Attachment के प्रयोग पर मेल भेजने के लिए है।

public static void CreateMessageWithAttachment(string server) 
    { 
     // Specify the file to be attached and sent. 
     // This example assumes that a file named Data.xls exists in the 
     // current working directory. 
     string file = "data.xls"; 
     // Create a message and set up the recipients. 
     MailMessage message = new MailMessage(
      "[email protected]", 
      "[email protected]", 
      "Quarterly data report.", 
      "See the attached spreadsheet."); 

     // Create the file attachment for this e-mail message. 
     Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 
     // Add time stamp information for the file. 
     ContentDisposition disposition = data.ContentDisposition; 
     disposition.CreationDate = System.IO.File.GetCreationTime(file); 
     disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
     disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 
     // Add the file attachment to this e-mail message. 
     message.Attachments.Add(data); 

     //Send the message. 
     SmtpClient client = new SmtpClient(server); 
     // Add credentials if the SMTP server requires them. 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 

     try { 
      client.Send(message); 
     } 
     catch (Exception ex) { 
      Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
       ex.ToString());    
     } 
     data.Dispose(); 
    } 
4

क्या इस ऐप को वास्तव में Outlook का उपयोग करने की आवश्यकता है? क्या System.Net.Mail नेमस्पेस का उपयोग न करने का कोई कारण है?

तुम सच में Outlook का उपयोग करने के लिए आप Microsoft.Office नामस्थान पर गौर करने की आवश्यकता होगी (और मैं क्योंकि तब आप 3 पार्टी निर्भरता है कि बदलाव की संभावना है पर आपकी ऐप्लिकेशन आधारित कर रहे हैं इसकी सलाह नहीं होगा) की जरूरत है, तो

मैं यहां शुरू करूंगा: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx

+0

हां यह ....... – Goober

47

यदि आप डिफ़ॉल्ट ईमेल क्लाइंट तक पहुंचना चाहते हैं तो आप MAPI32.dll (केवल विंडोज ओएस पर काम कर सकते हैं) का उपयोग कर सकते हैं। निम्नलिखित आवरण पर एक नज़र डालें:

http://www.codeproject.com/KB/IP/SendFileToNET.aspx

कोड इस तरह दिखता है:

MAPI mapi = new MAPI(); 
mapi.AddAttachment("c:\\temp\\file1.txt"); 
mapi.AddAttachment("c:\\temp\\file2.txt"); 
mapi.AddRecipientTo("[email protected]"); 
mapi.AddRecipientTo("[email protected]"); 
mapi.SendMailPopup("testing", "body text"); 

// Or if you want try and do a direct send without displaying the mail dialog 
// mapi.SendMailDirect("testing", "body text"); 
+0

वह कोडप्रोजेक्ट आलेख बहुत अच्छा है। – Jeremy

+4

यह कोड डिफ़ॉल्ट ईमेल क्लाइंट को अनुलग्नक भेजने के लिए उपयोगी है। हर कोई Outlook का उपयोग नहीं करता है, इसलिए यह कोड बहुत अच्छा है! – Brent

+0

मैंने अभी भी इसका इस्तेमाल किया है, पूरी तरह से काम करता है! :] – Eduardo

2

इस

var proc = new System.Diagnostics.Process(); 
proc.StartInfo.FileName = string.Format("\"{0}\"", Process.GetProcessesByName("OUTLOOK")[0].Modules[0].FileName); 
proc.StartInfo.Arguments = string.Format(" /c ipm.note /m {0} /a \"{1}\"", "[email protected]", @"c:\attachments\file.txt"); 
proc.Start(); 
0

प्रयास करें आधिकारिक तौर पर हाँ mailto प्रोटोकॉल संलग्नक का समर्थन नहीं करता । लेकिन विलीविग ने इसे बहुत अच्छी तरह से समझाया कि Open default mail client along with a attachment

संबंधित मुद्दे