-
-
Notifications
You must be signed in to change notification settings - Fork 10
Simplify.Mail
Provides IMailSender interface and MailSender implementation for simple e-mail sending.
Available at NuGet as binary package
{
"MailSenderSettings": {
"SmtpServerAddress": "server name or ip address",
"SmtpUserName": "user name",
"SmtpUserPassword": "user password"
}
}var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
new MailSender(config).Send("mymail@somedomain.com", "mailrecepient@somedomain.com", "Mail subject!", "Mail message, can be full HTML page");Mail sender includes anti-spam message pool, which is ON by default and default pool message life time is 120 min.
Whenever message is sending via MailSender, it's body will be added to anti-spam pool. At the next time, when another message with the same body is sent in next 120 minutes after first message is sent then the message will not be sent. It is useful, for example, if you have some windows service, which is doing some processing every minute and so on, and it is started to throwing same exceptions every minute, then the MailSender will prevent it from spamming.
Anti-spam pool can be turned off via configuration.
You can specify body for anti-spam pool checking separately from mail message body. For example, if you have some "current time" information in your body, which will be changing every time message sends, and anyway your want to use anti-spam pool, then you can provide that message body without time information as separated parameter, for example:
var message = "Some problem was happened at: ";
mailSender.Send("mymail@somedomain.com", "mailrecepient@somedomain.com", "Some problem was happened at: ", message + DateTime.Now, message);mailSender.Send("mymail@somedomain.com", new List<string> { "address1@somedomain.com", "address2@somedomain.com" }, new List<string> { "address3@somedomain.com", "address4@somedomain.com" },
"Mail subject!", "Mail message, can be full HTML page", null,
new Attachment(ms, "Attachement title.xls", "application/vnd.ms-excel"));mailSender.SendSeparately("mymail@somedomain.com", new List<string> { "address1@somedomain.com", "address2@somedomain.com" }, "Mail subject!", "Mail message, can be full HTML page");{
"MailSenderSettings": {
"SmtpServerAddress": "server name or ip address",
"SmtpUserName": "user name",
"SmtpUserPassword": "user password",
"SmtpServerPortNumber": "25",
"AntiSpamMessagesPoolOn": "true",
"AntiSpamPoolMessageLifeTime": "120",
"EnableSsl": "true"
}
}- Attachment[] → MimeEntity[] parameters
- MailMessage → MimeMessage parameters
- Async methods now have optional CancellationToken parameters
Before (System.Net.Mail):
using System.Net.Mail;
var attachment = new Attachment("file.pdf");
sender.Send("from@example.com", "to@example.com", "Subject", "Body", null, attachment);After (MailKit):
using MimeKit;
var attachment = new MimePart("application", "pdf") {
Content = new MimeContent(File.OpenRead("file.pdf")),
FileName = "file.pdf"
};
sender.Send("from@example.com", "to@example.com", "Subject", "Body", null, attachment);