Programmatically attach files to Outlook email items
This example attaches a file to a new mail item and sends it to Armando Pinto. The example assumes a person named Armando Pinto exists as the recipient.
Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.
Example
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddAttachment();
}
private void AddAttachment()
{
Outlook.MailItem mail =
this.Application.CreateItem
(Outlook.OlItemType.olMailItem)
as Outlook.MailItem;
mail.Subject = "An attachment for you!";
OpenFileDialog attachment = new OpenFileDialog();
attachment.Title = "Select a file to send";
attachment.ShowDialog();
if (attachment.FileName.Length > 0)
{
mail.Attachments.Add(
attachment.FileName,
Outlook.OlAttachmentType.olByValue,
1,
attachment.FileName);
mail.Recipients.Add("Armando Pinto ");
((Outlook._MailItem)mail).Send();
}
}
Related content
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for