2009-07-28 66 views
23

Hiện nay tôi đang sử dụng phương pháp dưới đây để mở tài khoản email sử dụng Outlook và cư một email với nội dung liên quan để gửi:C# MailTo with Attachment?

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

Tôi muốn tuy nhiên, để có thể cư trú trong email với một tập tin đính kèm.

cái gì đó như:

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

Tuy nhiên điều này dường như không làm việc. Có ai biết cách nào cho phép điều này hoạt động không !?

Giúp đánh giá cao.

Trân trọng.

Trả lời

10

mailto: không chính thức hỗ trợ tệp đính kèm. Tôi đã nghe Outlook 2003 sẽ làm việc với cú pháp này:

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

Cách tốt hơn để xử lý việc này là gửi thư trên máy chủ bằng 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

Ứng dụng này có thực sự cần sử dụng Outlook không? Có lý do nào để không sử dụng không gian tên System.Net.Mail không?

Nếu bạn thực sự cần sử dụng Outlook (và tôi sẽ không khuyên bạn sử dụng Outlook vì bạn đang dựa vào ứng dụng phụ thuộc của bên thứ ba có khả năng thay đổi), bạn sẽ cần phải nhìn vào các không gian tên Microsoft.Office

Tôi muốn bắt đầu tại đây: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx

+0

Có nó ....... – Goober

47

Nếu bạn muốn truy cập ứng dụng email mặc định thì bạn có thể sử dụng MAPI32.dll (chỉ hoạt động trên hệ điều hành Windows). Hãy nhìn vào các wrapper sau:

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

Mã trông như thế này:

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

Bài viết mã hóa đó rất tốt. – Jeremy

+4

Mã này hữu ích cho việc gửi tệp đính kèm đến ứng dụng email mặc định. Không phải ai cũng sử dụng Outlook, vì vậy mã này rất tuyệt! – Brent

+0

Tôi vừa mới sử dụng nó, hoạt động hoàn hảo! :] – Eduardo

2

Hãy thử điều này

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();