/// <summary> /// send email /// </summary> /// <param name="email"></param> public static string SendMessage(Emails email) { try { List <EmailAttachmentModel> attachmentModels = new List <EmailAttachmentModel>(); XmlSerializer serializer = new XmlSerializer(typeof(List <EmailAttachmentModel>)); if (!string.IsNullOrWhiteSpace(email.Attachments)) { using (var reader = new StringReader(email.Attachments)) { attachmentModels = (List <EmailAttachmentModel>)serializer.Deserialize(reader); } } Users user = UsersHelper.GetToUserInfo(email.ReceiverUserId); if (user == null) { return("ERROR"); } var settings = Program.Agent.ServiceSettings; MailMessage mail = new MailMessage(email.FromEmail, user.UserEmail); SmtpClient client = new SmtpClient(); client.Port = settings.SMTPSettings.Port; client.DeliveryMethod = SmtpDeliveryMethod.Network; //client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(settings.Credentials.UserName, settings.Credentials.Password); client.Host = settings.SMTPSettings.Server; mail.Subject = email.Subject; mail.IsBodyHtml = true; mail.Body = email.Body; if (!string.IsNullOrWhiteSpace(email.CcResponseTo)) { mail.CC.Add(email.CcResponseTo); } foreach (var attachment in attachmentModels) { var attach = EmailGeneratorHelper.GenerateAttachments(attachment); if (attach != null) { mail.Attachments.Add(attach); } } client.Send(mail); LogHelper.AddLog("Email to: " + mail.To + " succesfully sent"); return("OK"); } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return("ERROR"); } }
/// <summary> /// add email to database /// </summary> /// <returns></returns> public static int AddEmailToDbByEmailModel(int taskId, EmailModel emailModel) { try { using (var db = new FCCEmailAgentEntities()) { EmailContentModel content = new EmailContentModel(emailModel.EmailContentLine); var attachmentsContentModel = content.Elements.Find(x => x.Name == "#attachmentslist#"); Emails email = new Emails(); email.Subject = emailModel.Subject; email.FromEmail = emailModel.FromEmail; email.ReceiverUserId = UsersHelper.CheckExistsUserByEmail(emailModel.ToEmails); email.StateId = 1; email.TaskId = taskId; email.CcResponseTo = string.IsNullOrWhiteSpace(emailModel.CcResponseTo)?"": emailModel.CcResponseTo; email.TypeId = emailModel.TypeId; email.Host = Program.Agent.ServiceSettings.SMTPSettings.Server; email.Port = Program.Agent.ServiceSettings.SMTPSettings.Port; email.Body = EmailGeneratorHelper.GenerateEmailBody(emailModel.TypeId, content.Elements); if (attachmentsContentModel != null) { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(attachmentsContentModel.Value); } else { email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(""); } db.Emails.Add(email); db.SaveChanges(); EmailTransactionsHelper.AddEmailTransaction(email.Id); return(email.Id); } } catch (Exception exception) { string innerException = exception.InnerException == null ? "" : exception.InnerException.Message; string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name; LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " + innerException); return(-1); } }