May 28, 2010

Send An Email Using Outlook

See KB Article 310263
Add a reference to Microsoft.Office.Interop.Outlook then use following code:
using Outlook = Microsoft.Office.Interop.Outlook;

public void SendPlainFormatEmail(
  string recipient, 
  string subject, 
  string body, 
  string filePath, 
  bool silently)
{
    try
    {
        //Check file exists before the method is called
        FileInfo fi = new FileInfo(filePath);

        bool exists = fi.Exists;
        if (!exists)
            return;

        string fileName = fi.Name;

        // Create the Outlook application by using inline initialization.
        Outlook.Application outlookApp = new Outlook.Application();

        //Create the new message by using the simplest approach.
        Outlook.MailItem msg = (Outlook.MailItem)outlookApp.CreateItem(
          Outlook.OlItemType.olMailItem);

        Outlook.Recipient outlookRecip = null;
        //Add a recipient.
        if (!string.IsNullOrEmpty(recipient))
        {
          outlookRecip = (Outlook.Recipient)msg.Recipients.Add(recipient);
          outlookRecip.Resolve();
        }

        //Set the basic properties.
        msg.Subject = subject;
        msg.Body = body;
        msg.BodyFormat = Microsoft.Office.Interop.Outlook.
             OlBodyFormat.olFormatPlain;

        //Add an attachment.
        long sizeKB = fi.Length / 1024;
        string sDisplayName = fileName + "(" + 
                     sizeKB.ToString() + " KB)";
        int position = (int)msg.Body.Length + 1;
        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
        Outlook.Attachment attachment = msg.Attachments.Add(
          filePath, iAttachType, position, sDisplayName);

        //msg.Save();
        if (silently)
        {
            //Send the message.
            msg.Send();
        }
        else
        {
            msg.Display(true);  //modal
        }

        //Explicitly release objects.
        outlookRecip = null;
        attachment = null;
        msg = null;
        outlookApp = null;
    }

    // Simple error handler.
    catch (Exception e)
    {
        Console.WriteLine("{0} Exception caught: ", e);
    }
}
Following code uses the above method to email a file:
public void EmailFile(string recipient, string filePath)
{
    FileInfo fi = new FileInfo(filePath);
    bool exists = fi.Exists;
    if (!exists)
        return;

    string fileName = fi.Name;
    string subject = "Emailing: " + fileName;
    string body = "Your message is ready to be sent with the following "
           "file or link attachments:" + Environment.NewLine +
           Environment.NewLine +
           fileName + Environment.NewLine +
           Environment.NewLine +
           "Note: To protect against computer viruses, e-mail programs"
           " may prevent sending or receiving certain types of file "
           "attachments.  Check your e-mail security settings to determine"
           " how attachments are handled."; ;
    SendPlainFormatEmail(recipient, subject, body, filePath, false);
}

No comments: