C# MailTo with Attachment?
Asked Answered
H

4

31

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.

Hedron answered 28/7, 2009 at 16:3 Comment(2)
Duplicate: https://mcmap.net/q/219445/-open-default-mail-client-along-with-a-attachment/5389585Exposition
Duplicate of Open default mail client along with a attachmentAgnosticism
D
11

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();
    }
Dissimilar answered 28/7, 2009 at 16:12 Comment(0)
O
57

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");
Ornamentation answered 29/8, 2009 at 20:14 Comment(5)
This code is useful for sending attachments to the default email client. Not everyone uses Outlook, so this code is great!Og
This throws an AccessViolationException when I attach a file.Vadim
Nice reference! Should be the standard way to do it.Rhonarhonchus
If your mail client is already running, you will need to make sure that your mail client and your program are running with the same privileges. For instance, if Outlook is running as Admin, your application that uses this MAPI class you need to run as Admin as well. Otherwise, you may get MAPISendMail fails and returns error code 2Rebeccarebecka
Does Mapi works with Web Application ?Burkhart
D
11

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();
    }
Dissimilar answered 28/7, 2009 at 16:12 Comment(0)
M
4

Does this app really need to use Outlook? Is there a reason for not using the System.Net.Mail namespace?

If you really do need to use Outlook ( and I would not recommend it because then you're basing your app on 3rd party dependencies that are likely to change) you will need to look into the Microsoft.Office namespaces

I'd start here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx

Meeks answered 28/7, 2009 at 16:9 Comment(0)
H
2

Try this

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();
Heavy answered 27/8, 2014 at 13:22 Comment(1)
This requires running as an Admin. You could also use or check some default outlook exe location, instead of grabbing it from Process list.Cease

© 2022 - 2024 — McMap. All rights reserved.