С# – Отправка почты smtp

Spread the love

Функция для отправки почты имеет следующий вид:

public bool SendMail(string mailto, string caption, string message, string attachFile = “”, bool isHtml = true)
{
string smtpServer = “”;/* Тут адрес smtp сервера*/
string from = “”;/*Адрес почты, с которой будет идти отправка*/
string password = “”;/* Пароль к почте, указанной выше*/
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(new MailAddress(mailto));
mail.Subject = caption;
mail.Body = message;
mail.IsBodyHtml = isHtml;
if (!string.IsNullOrEmpty(attachFile)){
mail.Attachments.Add(new Attachment(attachFile));
}
SmtpClient client = new SmtpClient();
client.Host = smtpServer;
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential(from, password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
mail.Dispose();
}
catch (Exception e)
{
return false;
}
return true;
}

Leave a Reply