Skip to content

Instantly share code, notes, and snippets.

@alikrc
Last active February 24, 2017 10:31
Show Gist options
  • Save alikrc/eaeeadafc590f65ba5a6 to your computer and use it in GitHub Desktop.
Save alikrc/eaeeadafc590f65ba5a6 to your computer and use it in GitHub Desktop.
Send mail using hotmail as a service
using System;
using System.Net;
using System.Net.Mail;
namespace ConsoleApplication1
{
/// <summary>
/// sending e-mail using hotmail smtp service
/// </summary>
class Program
{
static void Main(string[] args)
{
EmailSettings myEmailSettings = new EmailSettings()
{
MyEmailAdress = "my@email.com",
MyPassword = "password",
MyDisplayName = "My Display Name",
//must be separated with comma (,)
Recipients = "recipient1@email.com,recipient2@email.com,recipient3@email.com",
MailSubject = "Sending mail from a c# app",
MailBody = "This mail sent using a c# app",
SmtpHost = "smtp.live.com",
SmtpPort = 587,
};
MailMessage message = new MailMessage()
{
From = new MailAddress(myEmailSettings.MyEmailAdress, myEmailSettings.MyDisplayName),
Subject = myEmailSettings.MailSubject,
Body = myEmailSettings.MailBody
};
message.To.Add(myEmailSettings.Recipients);
SmtpClient client = new SmtpClient()
{
Port = myEmailSettings.SmtpPort,
Host = myEmailSettings.SmtpHost,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(myEmailSettings.MyEmailAdress, myEmailSettings.MyPassword)
};
Console.WriteLine("sending..");
client.Send(message);
Console.WriteLine("mail sent. Please check your mailbox in a minute.");
Console.Read();
}
}
class EmailSettings
{
public string MyEmailAdress { get; set; }
public string MyPassword { get; set; }
public string MyDisplayName { get; set; }
public string Recipients { get; set; }
public string MailSubject { get; set; }
public string MailBody { get; set; }
public string SmtpHost { get; set; }
public int SmtpPort { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment