How to configure smtp for email newsletters?

Good day! I'm writing an application to send e-mail addresses, I have database account(totally different e-mail services: mail.ru walla.co.il pandora.be yahoo.co, etc.)
Actually a question: How can you make a simplified smtp setup? I do so(I know that's horribly wrong, but I don't know how else):
Smtp setup
switch (host) // host is a string variable which is obtained as a result of parsing the account email.
{
 case "@mail.ru":
 smtp = new SmtpClient("smtp.mail.ru", 587);
break;
 case "@yahoo.co":
 smtp = new SmtpClient("smtp.mail.yahoo.com", 465);
 _smtp = new SmtpClient("smtp.mail.yahoo.com", 587); // _smtp use because the sites I looked at where the settings for smtp, there were several ports. First sent to the 1st port, if the error in the second.
break;
 case "@gmx.at":
 smtp = new SmtpClient("smtp.gmx.com", 587);
 _smtp = new SmtpClient("smtp.gmx.com", 465);
break;
 case "@aol.com":
 smtp = new SmtpClient("smtp.aol.com", 465);
 _smtp = new SmtpClient("smtp.aol.com", 587);
break;
 case "@hotmail.com":
 smtp = new SmtpClient("smtp.live.com", 465);
 _smtp = new SmtpClient("smtp.live.com", 587);
break;
 case "@live.com":
 smtp = new SmtpClient("smtp.live.com", 465);
 _smtp = new SmtpClient("smtp.live.com", 587);
break;
 case "@twc.com":
 smtp = new SmtpClient("mail.twc.com", 465);
 _smtp = new SmtpClient("mail.twc.com", 587);
break;
 case "@roadrunner.com":
 smtp = new SmtpClient("mail.twc.com", 465);
 _smtp = new SmtpClient("mail.twc.com", 587);
break;
// etc.
 }
Sending letters in the mail
MailAddress from = new MailAddress(login, name);
 MailAddress to = new MailAddress(listTo[index]);
 MailMessage m = new MailMessage(from, to);
 m.Subject = TBSubject.Text;
 m.Body = message;

try
{
 smtp.Credentials = new NetworkCredential(login, password);
 smtp.EnableSsl = true;
smtp.Send(m);
}
 catch (Exception ex)
{
MessageBox.Show(ex.Message);
 _smtp.Credentials = new NetworkCredential(login, password);
 _smtp.EnableSsl = true;
_smtp.Send(m);
 }



And 2nd question:
I need to send messages with html tags, namely <"a href='URL'>link">, and that the letter is a message displayed link(as link), how to implement it?

P. S. Thank You!
June 10th 19 at 14:44
1 answer
June 10th 19 at 14:46
Solution
Specifically for Your code, then
1. If You are so rigidly prescribed all the settings in code, just a list of settings. First, the class itself
class SmtpSetting
{
 public string Host {get; set;}
 public string Server {get; set;}
 public string Port {get; set;}
}

Then the list itself
var smptSettings = new List<smptsetting>
{
 new SmptSetting
{
 Host = "mail.ru",
 Server = "smtp.mail.ru",
 Port = 587
},
 new SmptSetting
{
 Host = "yahoo.co",
 Server = "smtp.yahoo.co",
 Port = 465
},
...
}</smptsetting>

And then You don't have to use a switch
var currentSmtp = smptSettings.FirstOrDefault(x => x.Host == host);
if(currentSmtp != null)
{
 var smtp = new SmtpClient(currentSmtp.Server currentSmtp.Port);
}

You said that You have a database where you keep the accounts. Keep in the same place and configure the smtp settings in the same form (Id,Host,Server,Port), then from dB You will get the entire server list and when list of smtp settings, you do not have to go into the code and rebuild the project.

2. And in order to allow You to insert html markup in a letter from MailMessage set IsBodyHtml, set it to true.
About storing the smtp settings in the database, this method does not get implemented, because database data can be bought. And for everything else, thank you so much! - kirk22 commented on June 10th 19 at 14:49

Find more questions by tags SMTP.NETC#