protected EmailDto GetEmailDto(Settings settings, string to, string subject, string body) { EmailDto dto = new EmailDto(); if (settings == null) settings = SettingsDao.LoadFirst(); if (settings == null) { dto.Error = "Отсутствуют настройки в базе данных."; return dto; } dto.SmtpServer = settings.NotificationSmtp; dto.SmtpPort = settings.NotificationPort; dto.UserName = settings.NotificationLogin; dto.Password = settings.NotificationPassword; dto.From = settings.NotificationEmail; dto.To = to ?? settings.NotificationEmail; dto.Subject = subject; dto.Body = body; return dto; }
protected void SetEmailDtoForBilling(IEmailDtoSupport model, string from, string subject, string body) { EmailDto dto = new EmailDto(); Settings settings = SettingsDao.LoadFirst(); if (settings == null) { dto.Error = "Отсутствуют настройки в базе данных."; model.EmailDto = dto; return; } dto.SmtpServer = settings.BillingSmtp; dto.SmtpPort = settings.BillingPort; dto.UserName = settings.BillingLogin; dto.Password = settings.BillingPassword; dto.From = from ?? settings.NotificationEmail; dto.To = settings.BillingEmail; dto.Subject = subject; dto.Body = body; model.EmailDto = dto; }
protected static bool SendEmail(EmailDto dto) { if (!string.IsNullOrEmpty(dto.Error)) return false; MailMessage mailMessage = null; try { Log.Debug("Try to send mail to "+ dto.To+" from "+dto.From+" "+ dto.UserName); mailMessage = new MailMessage { From = new MailAddress(dto.From, dto.From) }; string[] toAddresses = dto.To.Split(';'); foreach (string address in toAddresses) mailMessage.To.Add(new MailAddress(address, address)); mailMessage.Subject = dto.Subject; mailMessage.Body = "<html>" + dto.Body + "</html>"; mailMessage.IsBodyHtml = true; var smtpClient = new SmtpClient { Host = dto.SmtpServer, Port = dto.SmtpPort, UseDefaultCredentials = false, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new BlockGSSAPINTLMCredential(dto.UserName, dto.Password)//new NetworkCredential(dto.UserName, dto.Password), }; smtpClient.Send(mailMessage); //WebMail.SmtpServer = dto.SmtpServer; //WebMail.SmtpPort = dto.SmtpPort; //WebMail.UserName = dto.UserName; //WebMail.Password = dto.Password; //WebMail.SmtpUseDefaultCredentials = false; //WebMail.Send(dto.To, // dto.Subject, // dto.Body, // dto.From); Log.DebugFormat("Отправлено письмо на {0}, тема {1}, текст {2}" , dto.To, dto.Subject, dto.Body); return true; } catch (Exception ex) { Log.Error("Exception:", ex); dto.Error = "Исключение: " + ex.GetBaseException().Message; return false; } finally { if (mailMessage != null) mailMessage.Dispose(); } }