Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MailTo with Attachment?

Currently I am using the below method to open the users outlook email account and populate an email with the relevant content for sending:

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

I want to however, be able to populate the email with an attached file.

something like:

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

However this does not seem to work. Does anyone know of a way which will allow this to work!?

Help greatly appreciate.

Regards.

like image 572
Goober Avatar asked Jul 28 '09 16:07

Goober


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

If you want to access the default email client then you can use MAPI32.dll (works on Windows OS only). Take a look at the following wrapper:

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

Code looks like this:

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"); 
like image 181
Alex Avatar answered Sep 17 '22 18:09

Alex


mailto: doesn't officially support attachments. I've heard Outlook 2003 will work with this syntax:

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

A better way to handle this is to send the mail on the server using 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();     } 
like image 32
Jon Galloway Avatar answered Sep 20 '22 18:09

Jon Galloway