Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30079

Attaching a file to an email C#

$
0
0

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 :(


Viewing all articles
Browse latest Browse all 30079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>