共用方式為


HOW TO:以程式設計方式將檔案附加至 Outlook 電子郵件項目

這個範例會將檔案附加至新的郵件項目並將它傳送給 Armando Pinto。這個範例會假設存在一位名為 Armando Pinto 的收件者。

**適用於:**本主題中的資訊適用於 Outlook 2013 和 Outlook 2010 的應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

範例

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Startup
    AddAttachment()
End Sub

Private Sub AddAttachment()
    Dim mail As Outlook.MailItem = _
    TryCast(Me.Application.CreateItem _
    (Outlook.OlItemType.olMailItem),  _
    Outlook.MailItem)

    mail.Subject = "An attachment for you!"

    Dim attachment As OpenFileDialog = _
        New OpenFileDialog()

    attachment.Title = "Select a file to send"
    attachment.ShowDialog()

    If attachment.FileName.Length > 0 Then
        mail.Attachments.Add( _
            attachment.FileName, _
            Outlook.OlAttachmentType.olByValue, _
            1, _
            attachment.FileName)
        mail.Recipients.Add("Armando Pinto")
        mail.Send()
    End If
End Sub
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();
    }
}

請參閱

工作

HOW TO:以程式設計方式傳送電子郵件

HOW TO:以程式設計方式從 Outlook 電子郵件項目儲存附件

HOW TO:以程式設計方式建立電子郵件項目

概念

使用郵件項目