forked from tangxuehua/equeue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendEmailService.cs
More file actions
60 lines (57 loc) · 2.64 KB
/
SendEmailService.cs
File metadata and controls
60 lines (57 loc) · 2.64 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
using System;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using ECommon.Logging;
using EQueue.Protocols.NameServers;
namespace EQueue.AdminWeb
{
public class SendEmailService
{
private SmtpClient _client;
private readonly string _senderMail;
private readonly string[] _targetEmails;
private readonly ILogger _logger;
private const string MailSubject = "EQueue消息堆积报警";
private const string MailBodyFormat = "您好,您订阅的Topic已出现消息堆积,请尽快处理,堆积详情如下:<br/>Topic:{0}<br/>消费者分组:{1}<br/>堆积数:{2}<br/>在线消费者个数:{3}<br/>消费吞吐:{4}<br/>总队列数:{5}";
public SendEmailService(ILoggerFactory loggerFactory)
{
_client = new SmtpClient(ConfigurationManager.AppSettings["mailHost"]);
_client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailUsername"], ConfigurationManager.AppSettings["mailpassword"]);
_senderMail = ConfigurationManager.AppSettings["senderMail"];
_targetEmails = ConfigurationManager.AppSettings["targetMails"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
_logger = loggerFactory.Create(GetType().FullName);
}
public void SendMessageAccumulateNotification(TopicAccumulateInfo topicAccumulateInfo)
{
try
{
var body = string.Format(MailBodyFormat,
topicAccumulateInfo.Topic,
topicAccumulateInfo.ConsumerGroup,
topicAccumulateInfo.AccumulateCount,
topicAccumulateInfo.OnlineConsumerCount,
topicAccumulateInfo.ConsumeThroughput,
topicAccumulateInfo.QueueCount);
var message = new MailMessage();
message.From = new MailAddress(_senderMail);
foreach (var targetMail in _targetEmails)
{
message.To.Add(targetMail);
}
message.Subject = MailSubject;
message.Body = body;
message.SubjectEncoding = Encoding.UTF8;
message.BodyEncoding = Encoding.UTF8;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
_client.Send(message);
}
catch (Exception ex)
{
_logger.Error("SendMessageAccumulateNotification has exception.", ex);
}
}
}
}