-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmtpEmailService.cs
More file actions
81 lines (63 loc) · 2.56 KB
/
SmtpEmailService.cs
File metadata and controls
81 lines (63 loc) · 2.56 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Net.Mail;
using System.Net;
using RabbitMQ.Client;
using System.Text;
namespace MusicLibraryApp
{
public class RabbitMQService : IEmailService
{
public async Task SendEmailAsyncWithQueue(string email, string subject, string message)
{
// Send email using SMTP
using (var client = new SmtpClient("smtp.gmail.com"))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("emailata8@gmail.com", "hcom fcbt cijz ecxt");
client.Port = 587;
client.EnableSsl = true;
var mailMessage = new MailMessage
{
From = new MailAddress("emailata8@gmail.com"),
Subject = subject,
Body = message,
IsBodyHtml = true
};
mailMessage.To.Add(email);
await client.SendMailAsync(mailMessage);
}
var factory = new ConnectionFactory
{
HostName = "localhost",
UserName = "guest",
Password = "guest",
};
// Send email data to RabbitMQ queue
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("email_queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
var emailData = Encoding.UTF8.GetBytes($"{email};{subject};{message}");
channel.BasicPublish(exchange: "", routingKey: "email_queue", basicProperties: null, body: emailData);
}
}// Send email using ONLY SMTP
public async Task SendEmailAsync(string email, string subject, string message)
{
using (var client = new SmtpClient("smtp.gmail.com"))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("emailata8@gmail.com", "hcom fcbt cijz ecxt");
client.Port = 587;
client.EnableSsl = true;
var mailMessage = new MailMessage
{
From = new MailAddress("emailata8@gmail.com"),
Subject = subject,
Body = message,
IsBodyHtml = true
};
mailMessage.To.Add(email);
await client.SendMailAsync(mailMessage);
}
}
}
}