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

本範例會將電子郵件訊息傳送給電子郵件位址中域名 example.com example.com 的聯繫人

適用於: 本主題中的資訊適用於 Outlook 的 VSTO 載入宏專案。 如需詳細資訊,請參閱 Office 應用程式 lication 和項目類型所提供的功能。

注意

有興趣開發跨多個平台擴充 Office 體驗的解決方案嗎? 查看新的 Office 載入宏模型。 相較於 VSTO 載入宏和解決方案,Office 載入宏的使用量很小,而且您可以使用幾乎任何 Web 程式設計技術來建置它們,例如 HTML5、JavaScript、CSS3 和 XML。

範例

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    SendEmailtoContacts();
}

private void SendEmailtoContacts()
{
    string subjectEmail = "Meeting has been rescheduled.";
    string bodyEmail = "Meeting is one hour later.";
    Outlook.MAPIFolder sentContacts = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderContacts);
    foreach (Outlook.ContactItem contact in sentContacts.Items)
    {
        if (contact.Email1Address.Contains("example.com"))
        {
            this.CreateEmailItem(subjectEmail, contact
                .Email1Address, bodyEmail);
        }
    }
}

private void CreateEmailItem(string subjectEmail,
       string toEmail, string bodyEmail)
{
    Outlook.MailItem eMail = (Outlook.MailItem)
        this.Application.CreateItem(Outlook.OlItemType.olMailItem);
    eMail.Subject = subjectEmail;
    eMail.To = toEmail;
    eMail.Body = bodyEmail;
    eMail.Importance = Outlook.OlImportance.olImportanceLow;
    ((Outlook._MailItem)eMail).Send();
}

編譯程式碼

這個範例需要:

  • 電子郵件位址中有功能變數名稱 的聯繫人 example.com

穩固程式設計

請勿移除搜尋功能變數名稱 的篩選程式代碼 example.com。 如果您移除篩選條件,您的解決方案將會傳送電子郵件訊息給所有聯繫人。