Last active
February 24, 2017 10:31
-
-
Save alikrc/eaeeadafc590f65ba5a6 to your computer and use it in GitHub Desktop.
Send mail using hotmail as a service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected]", | |
MyPassword = "password", | |
MyDisplayName = "My Display Name", | |
//must be separated with comma (,) | |
Recipients = "[email protected],[email protected],[email protected]", | |
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