-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmailSender.cs
60 lines (54 loc) · 2.01 KB
/
EmailSender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace EmailSenderProgram
{
/// <summary>
/// This is the EmailSender.
/// I'm using my private mail to send and recive.
/// </summary>
public class EmailSender
{
string _sender = "";
string _password = "";
public EmailSender(string sender, string password)
{
_sender = sender;
_password = password;
}
/// <summary>
/// This is the motor for the application
/// Here we create the connection to the mail server (smtp) in this case we are using smtpl.live.com to send mail from hotmail.
/// this method can be called from diffrent types of mail: New customer as old customer...
/// </summary>
/// <param name="recipient"></param>
/// <param name="subject"></param>
/// <param name="message"></param>
public void SendMail(string recipient, string subject, string message)
{
SmtpClient client = new SmtpClient("smtp.live.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;
}
}
}
}