EmailSenderSMTP
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:
Value | SMTP Server | Has it been checked? |
---|---|---|
MailRu | mail.ru | checked |
GoogleCom | google.com | checked |
YandexRu | yandex.ru | not checked |
PochtaRu | pochta.ru | not checked |
RamblerRu | rambler.ru | not 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 BodyCreateMailMessageBodyIsHTML(string emailTo, string name = null, string subject = "", string bodyHTML = "") ;
//for messages with Text BodyCreateMailMessageBodyIsText(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 classSenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//create MailMessage with HTML Bodystring bodyHTML = "<h1>hello, my friend</h1>";MailMessage messageHTML = sender.CreateMailMessageBodyIsHTML("example@email.com", "Your friend", "Letter", bodyHTML);
//create MailMessage with HTML Bodystring 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 classSenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//configure MailMessagestring bodyHTML = "<h1>hello, my friend</h1>";MailMessage message = sender.CreateMailMessageBodyIsHTML("example@email.com", "Your friend", "Letter", bodyHTML);
//send MailMessagesender.SendMail(message);
//async variationawait 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 classSenderSMTP sender = new SenderSMTP(MailService.GoogleCom, "example@gmail.com", "examplepass");
//configure email addressesstring[] addresses = new string[]{"example1@email.com", "example1@email.com", "example1@email.com"}
//configure MailMessagestring bodyHTML = "<h1>hello, my friend</h1>";MailMessage message = sender.CreateMailMessageBodyIsHTML("", "Your friend", "Letter", bodyHTML);
//send one MailMessage to multiple addressessender.SendMailToAddresses(message, addresses);
//async variationawait sender.SendMailToAddressesAsync(message, addresses);
SendMailsToAddresses()
is a method for sending a multiple messages to multiple addresses.
Example:
//configure SenderSMTP classSenderSMTP 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 addressessender.SendMailsToAddresses(messages);
//async variationawait sender.SendMailsToAddressesAsync(messages);
Описание
This nuget package is designed to better interact with SmtpClient
Языки
C#