If you want to send Email programmatically using your Outlook.com or Gmail account as the SMTP host, there are a few things to pay attention to in order to get it all working.
Using the basic System.Net.Mail
library, sending email is generally fairly straightforward. However, if you want to send using your Outlook.Com or Gmail account as the SMTP host, you will most likely need to take a few extra steps, if you have two-stage authorization enabled (and you DO have two-stage auth enabled, RIGHT??!!).
Image by JASE Group LLC | Some Rights Reserved
Here is a basic class with SMPT configuration for sending mail using Outlook.Com SMTP:
Basic Mail Configuration Settings for Outlook.Com SMTP:
using System;
using System.Net.Mail;
namespace SmtpMailConnections
{
public class OutlookDotComMail
{
string _sender = "";
string _password = "";
public OutlookDotComMail(string sender, string password)
{
_sender = sender;
_password = password;
}
public void SendMail(string recipient, string subject, string message)
{
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(_sender, _password);
client.EnableSsl = true;
client.Credentials = credentials;
try
{
var mail = new MailMessage(_sender.Trim(), recipient.Trim());
mail.Subject = subject;
mail.Body = message;
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
}
}
As you can see, we have kept this pretty minimal for the purpose of clarity. We initialize our simple class with a user name (in this case, our Outlook.Com email address) and a password.
Note that the above, and all of the following examples can be modified to work with Gmail by changing:
SmtpClient client = new SmtpClient("smtp-mail.outlook.com");
to:
SmtpClient client = new SmtpClient("smtp.gmail.com");
Example Usage
We can call into this class to send mail like this (this example is a simple console application):
Sending Mail Using the Mail Sender Class
class Program
{
static void Main(string[] args)
{
string mailUser = "YourAccount@outlook.com";
string mailUserPwd = "YourPassword";
var sender = new OutlookDotComMail(mailUser, mailUserPwd);
sender.SendMail("recipient@example.com", "Test Mail", "Hello!");
}
}
If you run the code above, using your own Outlook.Com Email address and password, all should work fine.
Unless you have enabled two-stage authorization on your Outlook.Com account. If you have, you will need to create an application specific password, or the code above will throw an exception when your credentials are refused by the Outlook.Com SMTP server.
To create an App-Specific Password, log in to your Outlook.com account, and go to Account Settings –> Security Info –> App Passwords:
Click on the Create a new app password link, and voila – you now have a new password for use within your application:
Use this as the password in your code, and all should be well:
Use the App Password Instead of Your Outlook.Com Account Password:
class Program
{
static void Main(string[] args)
{
string mailUser = "YourAccount@outlook.com";
string mailUserPwd = "bnppnnenfmpiixty";
var sender = new OutlookDotComMail(mailUser, mailUserPwd);
sender.SendMail("recipient@example.com", "Test Mail", "Hello!");
}
}
The above applies to Gmail two-step auth as well. If you have two-step auth enabled, you will need to create an app-specific password.
You can find the code samples above at my Github repo.
John on GoogleCodeProject