public static async void MailSendAsync(MailSendInfo info) { if (string.IsNullOrEmpty(smtpClientHost) || smtpClientPort == 0) { return; } if (string.IsNullOrEmpty(mailLogin) || string.IsNullOrEmpty(mailPassword)) { return; } if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) { return; } using (var objMailMessage = new MailMessage()) { using (var objSmtpClient = new SmtpClient(smtpClientHost, smtpClientPort)) { try { objMailMessage.From = new MailAddress(mailLogin); objMailMessage.To.Add(new MailAddress(info.MailAddress)); objMailMessage.Subject = info.Subject; objMailMessage.Body = info.Text; objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; string file = info.File; Attachment attach = new Attachment(file, MediaTypeNames.Application.Octet); ContentDisposition disposition = attach.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); objMailMessage.Attachments.Add(attach); objSmtpClient.UseDefaultCredentials = false; objSmtpClient.EnableSsl = true; objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; objSmtpClient.Credentials = new NetworkCredential(mailLogin, mailPassword); await Task.Run(() => objSmtpClient.Send(objMailMessage)); } catch (Exception) { throw; } } } }
public static void MailSend(MailSendInfo info) { if (string.IsNullOrEmpty(smtpClientHost) || smtpClientPort == 0) { return; } if (string.IsNullOrEmpty(mailLogin) || string.IsNullOrEmpty(mailPassword) || string.IsNullOrEmpty(mailName)) { return; } if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) { return; } using (var objMailMessage = new MailMessage()) { using (var objSmtpClient = new SmtpClient(smtpClientHost, smtpClientPort)) { try { objMailMessage.From = new MailAddress(mailLogin, mailName); objMailMessage.To.Add(new MailAddress(info.MailAddress)); objMailMessage.Subject = info.Subject; objMailMessage.Body = info.Text; objMailMessage.Attachments.Add(new Attachment(info.FileName)); objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; objSmtpClient.UseDefaultCredentials = false; objSmtpClient.EnableSsl = true; objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; objSmtpClient.Credentials = new NetworkCredential(mailLogin, mailPassword); objSmtpClient.Send(objMailMessage); } catch (Exception) { throw; } } } }
public void SendMail(MailSendInfo info) { MailMessage objMailMessage = new MailMessage(); objMailMessage.IsBodyHtml = true; objMailMessage.From = new MailAddress("*****@*****.**", "Цветочная мастерская"); objMailMessage.To.Add(new MailAddress(info.MailAddress)); objMailMessage.Subject = info.Subject; objMailMessage.Body = info.Text; if (info.FileName != null) { objMailMessage.Attachments.Add(new Attachment(info.FileName)); } using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) { client.Credentials = new NetworkCredential("*****@*****.**", "awakeawake397365"); client.EnableSsl = true; client.Send(objMailMessage); } }