Tuesday, October 18, 2011

Create a Mail Item, Attach a Report, and Send the Mail Item


// namespace (add microsoft.office.interop.outlook.dll
using Outlook = Microsoft.Office.Interop.Outlook;
//code
private void SendMailThroughOutlook()
{
    Outlook.MailItem mail = Application.CreateItem(
        Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = "File sent programatically";
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;
    if (currentUser.Type == "EX")
    {
        Outlook.ExchangeUser manager =
            currentUser.GetExchangeUser().GetExchangeUserManager();
        // Add recipient using display name, alias, or smtp address
        mail.Recipients.Add(manager.PrimarySmtpAddress);
        mail.Recipients.ResolveAll();
        mail.Attachments.Add(@"c:\file.xlsx",
            Outlook.OlAttachmentType.olByValue, Type.Missing,
            Type.Missing);
        mail.Send();
    }
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

The Code 39 error occurs when Windows is unable to load the device driver for a specific hardware device. This typically indicates that the driver is corrupted, missing, or incompatible

The Code 39 error occurs when Windows is unable to load the device driver for a specific hardware device. This typically indicates that the...