L. Levine
L. Levine

Reputation: 155

Attaching a file to an email C#

I've tried many of the solutions provided on this site to attach a file to an email, but no matter what I try I always get the "Sorry, something went wrong. You may want to try again" message at the line where I try to attach the file to my outlook mailitem.

  try
        {
            App = new Microsoft.Office.Interop.Outlook.Application();

            MailItem mailItem = App.CreateItem(OlItemType.olMailItem);

            mailItem.Subject = Subject;
            mailItem.To = To;
            mailItem.CC = CC;
            mailItem.BCC = BCC;
            mailItem.Body = Body;

            // make sure a filename was passed
            if (string.IsNullOrEmpty(FileAtachment) == false)
            {
                // need to check to see if file exists before we attach !
                if (!File.Exists(FileAtachment))
                    MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(FileAtachment);
                    mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
                }
            }
            mailItem.Display();     // display the email
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

Can anyone offer any idea how to get this to work ? I can send emails without any problem, but when I try to add an attachment it doesn't work :(

Upvotes: 3

Views: 11363

Answers (3)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

The Attachments.Add method accepts a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment, but not the attachment object:

            App = new Microsoft.Office.Interop.Outlook.Application();

            MailItem mailItem = App.CreateItem(OlItemType.olMailItem);
             
            mailItem.Subject = Subject;
            mailItem.To = To;
            mailItem.CC = CC;
            mailItem.BCC = BCC;
            mailItem.Body = Body;

            // make sure a filename was passed
            if (string.IsNullOrEmpty(FileAtachment) == false)
            {
                // need to check to see if file exists before we attach !
                if (!File.Exists(FileAtachment))
                    MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    Attachment attachment = mailItem.Attachments.Add("D:\\text.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
                }
            }
            mailItem.Display();     // display the email

And don't mix .Net and OOM objects in the code:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(FileAtachment);
                    mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);

The System.Net.Mail.Attachment type belongs to BCL and can't be used for Outlook.

Upvotes: 7

user8538241
user8538241

Reputation: 11

I am using Outlook 2016 and for me, displaying the Mail before adding the Attachment(s) did the trick (as long as the Attachment is a valid file path)

    App = new Microsoft.Office.Interop.Outlook.Application();

    MailItem mailItem = App.CreateItem(OlItemType.olMailItem);

    mailItem.Subject = Subject;
    mailItem.To = To;
    mailItem.CC = CC;
    mailItem.BCC = BCC;
    mailItem.Body = Body;

    // display the item before adding the attachments
    mailItem.Display();     // display the email

    // make sure a filename was passed
    if (string.IsNullOrEmpty(FileAtachment) == false)
    {
        // need to check to see if file exists before we attach !
        if (!File.Exists(FileAtachment))
            MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else
        {
            Attachment attachment = mailItem.Attachments.Add("D:\\text.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
        }
     }

Upvotes: 1

Oleg Levin
Oleg Levin

Reputation: 3621

Attachments.Add needs a string as an argument. That string must be a fully qualified name of the file that you are trying to attach.

Save the stream to a temporary file, pass the file name to Attachments.Add, delete the file.

You can get details from : https://social.msdn.microsoft.com/Forums/vstudio/en-US/17efe46b-18fe-450f-9f6e-d8bb116161d8/attach-stream-data-with-outlook-mail-client?forum=outlookdev

Upvotes: 0

Related Questions