/// <summary> /// Method to send email /// </summary> /// <param name="mail">mail details</param> /// <param name="setting">smtp server details</param> private void SendEmail(EmailServiceDTO mail, SMTPServer setting) { AspectEnums.EmailStatus mailStatus = AspectEnums.EmailStatus.None; SMTPServerDTO smtpDetail = new SMTPServerDTO(); Mapper mapper = new Mapper(); mapper.CreateMap <SMTPServer, SMTPServerDTO>(); mapper.Map(setting, smtpDetail); string remarks = string.Empty; //call mathod to send email bool isSent = MailingEngine.SendEmail(mail, smtpDetail); if (isSent) { mailStatus = AspectEnums.EmailStatus.Delivered; remarks = "Success"; } else { mailStatus = AspectEnums.EmailStatus.Failed; remarks = "Failure"; } SystemRepository.UpdateEmailServiceStatus(mail.EmailServiceID, (int)mailStatus, remarks); }
/// <summary> /// /// </summary> /// <param name="mail">mail need to be sent</param> /// <param name="smtpSettings">smtp settings</param> /// <returns>TRUE if the email sent successfully, FALSE otherwise</returns> public bool SendEmail(EmailServiceDTO mail, SMTPServerDTO smtpSettings) { bool isSuccess = false; try { // setup email header MailMessage mailMessage = new MailMessage(); // Set the message sender // sets the from address for this e-mail message. mailMessage.From = new MailAddress(smtpSettings.FromEmail, smtpSettings.FromName); // Sets the address collection that contains the recipients of this e-mail message. ManageMailMessageAddress(mail.ToEmail, mail.ToName, mailMessage, MailAddressType.ToAddress); ManageMailMessageAddress(mail.CcEmail, string.Empty, mailMessage, MailAddressType.CcAddress); ManageMailMessageAddress(mail.BccEmail, string.Empty, mailMessage, MailAddressType.BccAddress); // sets the message subject. mailMessage.Subject = mail.Subject; // sets the message body. mailMessage.Body = mail.Body; // sets a value indicating whether the mail message body is in Html. // if this is false then ContentType of the Body content is "text/plain". mailMessage.IsBodyHtml = mail.IsHtml; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; // add all the file attachments if we have any if (mail.IsAttachment && !String.IsNullOrEmpty(mail.AttachmentFileName)) { string[] files = mail.AttachmentFileName.Split(';'); foreach (string attachment in files) { string reportFileLocation = AppDomain.CurrentDomain.BaseDirectory + "\\" + AppUtil.GetAppSettings(AspectEnums.ConfigKeys.ReportFileFolder); ActivityLog.SetLog(reportFileLocation + " - " + AppVariables.AppLogTraceCategoryName.EmailListener, LogLoc.DEBUG); reportFileLocation = String.Format(@"{0}\{1}", reportFileLocation, attachment); if (File.Exists(reportFileLocation)) { mailMessage.Attachments.Add(new Attachment(reportFileLocation)); } } } // SmtpClient Class Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP). SmtpClient smtpClient = new SmtpClient(smtpSettings.ServerName, Convert.ToInt32(smtpSettings.PortNumber)); //Specifies how email messages are delivered. Here Email is sent through the network to an SMTP server. smtpClient.Credentials = new System.Net.NetworkCredential(smtpSettings.UserName, smtpSettings.Password); smtpClient.Port = Convert.ToInt32(smtpSettings.PortNumber); smtpClient.Host = smtpSettings.ServerName; smtpClient.EnableSsl = smtpSettings.IsSSL; //Let's send it smtpClient.Send(mailMessage); isSuccess = true; // Do cleanup mailMessage.Dispose(); smtpClient = null; } catch (Exception ex) { string response = String.Format("Email Failure for {0}, {1}", mail.ToEmail, ex.Message); isSuccess = false; ActivityLog.SetLog(response + " - " + AppVariables.AppLogTraceCategoryName.EmailListener, LogLoc.ERROR); if (ex.InnerException != null) { ActivityLog.SetLog(ex.InnerException.ToString() + " - " + AppVariables.AppLogTraceCategoryName.EmailListener, LogLoc.ERROR); } } return(isSuccess); }