Answer the question
In order to leave comments, you need to log in
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):
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;
// и т.д.
}
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);
}
Answer the question
In order to leave comments, you need to log in
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;}
}
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
},
...
}
var currentSmtp = smptSettings.FirstOrDefault(x => x.Host == host);
if(currentSmtp != null)
{
var smtp = new SmtpClient(currentSmtp.Server, currentSmtp.Port);
}
MailMessage
there is a setting IsBodyHtml
, set it to true.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question