How to: Programmatically Send E-Mail Programmatically

This example sends an e-mail message to contacts that have the domain name example.com in their e-mail addresses.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

Example

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();
}

Compiling the Code

This example requires:

  • Contacts that have the domain name example.com in their e-mail addresses.

Robust Programming

Do not remove the filter code that searches for the domain name example.com. Your solution will send e-mail messages to all of your contacts if you remove the filter.

See Also

Tasks

How to: Programmatically Create an E-Mail Item

How to: Programmatically Access Outlook Contacts

How to: Programmatically Perform Actions When an E-Mail Message Is Received

Concepts

Working with Mail Items