HOW TO:以程式設計方式傳送電子郵件
這個範例會將電子郵件訊息傳送至電子郵件地址中含有網域名稱 example.com 的連絡人。
**適用於:**本主題中的資訊適用於 Outlook 2013 和 Outlook 2010 的應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
範例
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
SendEmailtoContacts()
End Sub
Private Sub SendEmailtoContacts()
Dim subjectEmail As String = "Meeting has been rescheduled."
Dim bodyEmail As String = "Meeting is one hour later."
Dim sentContacts As Outlook.MAPIFolder = Me.Application.ActiveExplorer() _
.Session.GetDefaultFolder(Outlook _
.OlDefaultFolders.olFolderContacts)
For Each contact As Outlook.ContactItem In sentContacts.Items()
If contact.Email1Address.Contains("example.com") Then
CreateEmailItem(subjectEmail, contact _
.Email1Address, bodyEmail)
End If
Next
End Sub
Private Sub CreateEmailItem(ByVal subjectEmail As String, _
ByVal toEmail As String, ByVal bodyEmail As String)
Dim eMail As Outlook.MailItem = Me.Application.CreateItem _
(Outlook.OlItemType.olMailItem)
With eMail
.Subject = subjectEmail
.To = toEmail
.Body = bodyEmail
.Importance = Outlook.OlImportance.olImportanceLow
.Send()
End With
End Sub
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 的篩選程式碼。如果您移除該篩選條件,您的方案就會將電子郵件訊息傳送給所有連絡人。