9
string email ="sample@gmail.com";
attachment = path + "/" + filename;
Application.OpenURL ("mailto:" + 
                      email+"
                      ?subject=EmailSubject&body=EmailBody"+"&attachment="+attachment);

In the above code, attachment isn't working. Is there any other alternative to add attachments using a mailto: link in C#?

vicky
  • 382
  • 3
  • 14
  • 1
    why shouldn't you give a look to System.net.mail , you can achieve the things in a much easier way, It has All required class implemented like mailaddress, and attachment class for attachment purpose – coder3521 Dec 24 '15 at 07:09
  • 1
    Possible duplicate: [C# MailTo with Attachment?](http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment) – Rahul Tripathi Dec 24 '15 at 07:21

3 Answers3

2

You can use the System.Net.Mail which has the MailMessage.Attachments Property. Something like:

message.Attachments.Add(new Attachment(yourAttachmentPath));

OR

You can try like this:

using SendFileTo;

namespace TestSendTo
{
    public partial class Form1 : Form
    {
        private void btnSend_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();

            mapi.AddAttachment("c:\\temp\\file1.txt");
            mapi.AddAttachment("c:\\temp\\file2.txt");
            mapi.AddRecipientTo("person1@somewhere.com");
            mapi.AddRecipientTo("person2@somewhere.com");
            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");
        }
    }
}

The above code uses the MAPI32.dll.

Source

It probably won't attach the document because you are at the liberty of the email client to implement the mailto protocol and include parsing for the attachment clause. You may not know what mail client is installed on the PC, so it may not always work - Outlook certainly doesn't support attachments using mailto.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    "Outlook certainly doesn't support attachments using mailto." It will work for some versions but your right, you dont know what mail clients is installed so I Suggest using C# System.Net.Mail.Attachment. – Max Sassen Dec 24 '15 at 07:19
  • 1
    @MaxSassen:- Thats the reason after posting the answer, I added the option to use `System.Net.Mail` – Rahul Tripathi Dec 24 '15 at 07:22
2

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

<a href='mailto:name@domain.com?Subject=SubjTxt&Body=Bod_Txt&Attachment=""C:\file.txt"" '>

Your problem has already been answered: c-sharp-mailto-with-attachment

Community
  • 1
  • 1
Max Sassen
  • 650
  • 5
  • 15
-1

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(
       "jane@contoso.com",
       "ben@contoso.com",
       "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();
}
  • 2
    You just copied the post: http://stackoverflow.com/questions/1195111/c-sharp-mailto-with-attachment#answer-1195153 just link to it – Max Sassen Dec 24 '15 at 11:04