Send Email using Hotmail/Live/Outlook/Office365 in Visual C++

In previous section, I introduced how to send email using Yahoo account. In this section, I will introduce how to use your Hotmail/Outlook.com/Office365 account to send email in Visual C++.

Introduction

Hotmail/Live/Outlook.com SMTP server address is smtp.live.com. It requires explicit SSL (TLS) connection to do user authentication, and you should use your Hotmail/Live/Outlook.com email address as the user name for ESMTP authentication. For example: your email is liveid@hotmail.com, and then the user name should be liveid@hotmail.com.

Server Port SSL/TLS
smtp.live.com 25, 587 TLS

Note

Remarks: All of samples in this section are based on first section: A simple Visual C++ project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[Visual C++ Example - Send email using Hotmail/MSN Live account over TLS connection]

The following example codes demonstrate how to send email using Hotmail/MSN Live account.

Note

To get the full sample projects, please refer to Samples section.

#include "stdafx.h"
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize( NULL );
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = _T("TryIt");

    // Set your hotmail email address
    oSmtp->FromAddr = _T("liveid@hotmail.com");

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 );

    // Set email subject
    oSmtp->Subject = _T("simple email from Visual C++ with hotmail account");

    // Set email body
    oSmtp->BodyText = _T("this is a test email sent from Visual C++ project with hotmail");

    // Hotmail SMTP server address
    oSmtp->ServerAddr = _T("smtp.live.com");

    // set 587 port
    oSmtp->ServerPort = 587;

    // detect SSL/TLS automatically.
    oSmtp->SSL_init();

    // Hotmail user authentication should use your
    // Hotmail email address as the user name.
    oSmtp->UserName = _T("liveid@hotmail.com");
    oSmtp->Password = _T("yourpassword");

    _tprintf(_T("Start to send email via Hotmail account ...\r\n" ));

    if( oSmtp->SendMail() == 0 )
    {
        _tprintf( _T("email was sent successfully!\r\n"));
    }
    else
    {
        _tprintf( _T("failed to send email with the following error: %s\r\n"),
            (const TCHAR*)oSmtp->GetLastErrDescription());
    }
    if( oSmtp != NULL )
        oSmtp.Release();
    return 0;
}

Send Email using Office 365

First of all, you should go to Office 365 “Outlook” -> “Options” -> “See All Options” -> “Account” -> “My Account” -> “Settings for POP, IMAP, and SMTP access”. You will get your Office 365 SMTP server address and port. Then you can use your Office 365 SMTP server, port, user/password in the codes.

By default, Office 365 SMTP server uses 587 port and explicit SSL (TLS) connection.

Server Port SSL/TLS
smtp.office365.com 25, 587 (recommended) TLS

[VC++ - Send email using Office 365 over explicit SSL (TLS) on 587 port - Example]

The following example codes demonstrate how to send email using Office 365 in VC++ over TLS 587 port.

Note

To get the full sample projects, please refer to Samples section.

#include "stdafx.h"
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize( NULL );
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = _T("TryIt");

    //  Your office 365 email address
    oSmtp->FromAddr = _T("myid@mydomain");

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 );

    // Set email subject
    oSmtp->Subject = _T("simple email from Visual C++ with office365 account");

    // Set email body
    oSmtp->BodyText = _T("this is a test email sent from Visual C++ project with office365");

    // SMTP server address
    oSmtp->ServerAddr = _T("smtp.office365.com");

    // Use 587 port.
    oSmtp->ServerPort = 587;

    // detect SSL/TLS automatically.
    oSmtp->SSL_init();

    // Office365 user authentication should use your
    // Office365 email address as the user name.
    oSmtp->UserName = _T("myid@mydomain");
    oSmtp->Password = _T("yourpassword");

    _tprintf(_T("Start to send email via office365 account ...\r\n" ));

    if( oSmtp->SendMail() == 0 )
    {
        _tprintf( _T("email was sent successfully!\r\n"));
    }
    else
    {
        _tprintf( _T("failed to send email with the following error: %s\r\n"),
            (const TCHAR*)oSmtp->GetLastErrDescription());
    }
    if( oSmtp != NULL )
        oSmtp.Release();
    return 0;
}

Next Section

At next section I will introduce how to send email without specified SMTP server.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.