Следите за новостями GitVerse в нашем телеграм-канале

EmailSenderSMTP

Форк
0

README.md

EmailSender.SMTP

Introduction

This project is a Nuget-package.

It is needed in order to make it easier to work with the standard SmtpClient class in .NET CORE 7/8.

Content:

  • Introduction
  • Content
  • Supported MailServices
  • SenderSMTP & its constructor
  • Create Text/HTML MailMessage
  • Send MailMessage
  • Other Methods

Supported MailServices

MailService - enum, which contains the value of SMTP-servers that are supported by this library. Some SMTP-servers will be checked in the future, but have already been added for selection. MailServic enum values:

ValueSMTP ServerHas it been checked?
MailRumail.ruchecked
GoogleComgoogle.comchecked
YandexRuyandex.runot checked
PochtaRupochta.runot checked
RamblerRurambler.runot checked

SenderSMTP & its constructor

SenderSMTP is a class that configures the SmtpClient class itself when its constructor is called.NET CORE.

In the constructor, this class accepts:

  • (required) MailService - one of the MailService enum values (enum MailService)
  • (required) emailFrom - email address from which emails will be sent (string)
  • (required) pass - password for applications from the email address. from which emails will be sent (string)
  • (optional) nameAuthor -the name of the sender to be displayed in the email, it can be specified separately for each message (string)
  • (optional) emailsPerMinutes - number of emails that can be sent in 1 minute, it is recommended to leave the default value (int)
SenderSMTP(MailService mailService, string emailFrom, string pass, string nameAuthor = "", int emailsPerMinutes = 10)

Create Text/HTML MailMessage

There are two methods for creating messages in the library:

//for messages with HTML Body
CreateMailMessageBodyIsHTML(string emailTo, string name = null, string subject = "", string bodyHTML = "") ;
//for messages with Text Body
CreateMailMessageBodyIsText(string emailTo, string name = null, string subject = "", string bodyText = "");

Both methods take the same arguments:

  • (required) emailTo - the email address of the recipient of the help (string)
  • (optional) name - the name of the sender, which will be displayed in the email message. (string)
  • (optional) subject - title email message (string)
  • (optional) bodyHtml/bodyText - The content of the email message (string)

Below is an example of creating messages with an HTML or Text body.

Example:

//configure SenderSMTP class
SenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//create MailMessage with HTML Body
string bodyHTML = "<h1>hello, my friend</h1>";
MailMessage messageHTML = sender.CreateMailMessageBodyIsHTML("example@email.com", "Your friend", "Letter", bodyHTML);
//create MailMessage with HTML Body
string bodyText = "hello, my friend";
MailMessage messageText = sender.CreateMailMessageBodyIsHTML("example@email.com", "Your friend", "Letter", bodyText);

Send MailMessage

Each method for sending messages has its asynchronous version, which differs only in that the postfix 'Async' is added to the end of its name. It is quite simple to work with all the methods, so next there will be only examples with signatures.

//configure SenderSMTP class
SenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//configure MailMessage
string bodyHTML = "<h1>hello, my friend</h1>";
MailMessage message = sender.CreateMailMessageBodyIsHTML("example@email.com", "Your friend", "Letter", bodyHTML);
//send MailMessage
sender.SendMail(message);
//async variation
await sender.SendMailAsync(message);

Other Methods

This library contains not only methods for sending a single message, but also methods for sending a single message to multiple addresses, or multiple messages.

SendMailToAddresses()

is a method for sending a single message to multiple addresses.

Example:

//configure SenderSMTP class
SenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//configure email addresses
string[] addresses = new string[]{"example1@email.com", "example1@email.com", "example1@email.com"}
//configure MailMessage
string bodyHTML = "<h1>hello, my friend</h1>";
MailMessage message = sender.CreateMailMessageBodyIsHTML("", "Your friend", "Letter", bodyHTML);
//send one MailMessage to multiple addresses
sender.SendMailToAddresses(message, addresses);
//async variation
await sender.SendMailToAddressesAsync(message, addresses);

SendMailsToAddresses()

is a method for sending a multiple messages to multiple addresses.

Example:

//configure SenderSMTP class
SenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//configure IEnumerable<MailMessage>
string bodyHTML = "<h1>hello, my friend</h1>";
MailMessage message1 = sender.CreateMailMessageBodyIsHTML("example1@email.com", "Your friend", "Letter", bodyHTML);
MailMessage message2 = sender.CreateMailMessageBodyIsHTML("example2@email.com", "Your friend", "Letter", bodyHTML);
MailMessage message3 = sender.CreateMailMessageBodyIsHTML("example3@email.com", "Your friend", "Letter", bodyHTML);
List<MailMessage> messages = new List<MailMessage>();
messages.Add(message1);
messages.Add(message2);
messages.Add(message3);
//send multiple MailMessage to multiple addresses
sender.SendMailsToAddresses(messages);
//async variation
await sender.SendMailsToAddressesAsync(messages);

Описание

This nuget package is designed to better interact with SmtpClient

Языки

C#

Сообщить о нарушении

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.