M
M
Michael Vasyukov2017-12-16 00:00:42
.NET
Michael Vasyukov, 2017-12-16 00:00:42

How to properly configure smtp for email newsletters?

Good day! I am writing an application for sending letters to email addresses, I have a database of accounts (completely different email services: mail.ru, walla.co.il, pandora.be, yahoo.co, etc.)
Actually the question is: How can to make a simplified smtp setup? I do this (I know it's terribly wrong, but I don't know how else):

smtp setup
switch (host) // host - это string переменная которая получается в результате парсинга аккаунта почты.
                    {
                        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 использую потому что на сайтах где я смотрел настройки для smtp, было несколько портов. Сначала отправляю на 1-ый порт, если ошибка - на второй.
                            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;
// и т.д.
                    }
Sending emails
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 the 2nd question:
I need to send messages with html tags, namely <"a href='URL'>link">, and so that in the letter this message is displayed link (as a link), how to implement this?
PS Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2017-12-16
@programmer_developer

If specifically according to your code, then
1. If you are so rigid in writing all the settings in the code, then make 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
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
  },
  ...
}

And then you don't have to use 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 store accounts. Store the smtp settings in the same place, in the same form (Id, Host, Server, Port), then from the database you will receive the entire list of servers and when you change the list of smtp settings, you do not have to go into the code and rebuild the project.
2. And in order for you to be able to embed html markup in the letter, MailMessagethere is a setting IsBodyHtml, set it to true.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question