public Task <Blogs> CreateBlogAsync(BlogDTO newBlog, IEnumerable <String> BlogCatagories) { try { Blogs objBlogs = new Blogs(); objBlogs.BlogId = 0; objBlogs.BlogContent = newBlog.BlogContent; objBlogs.BlogDate = newBlog.BlogDate; objBlogs.BlogSummary = newBlog.BlogSummary; objBlogs.BlogTitle = newBlog.BlogTitle; objBlogs.BlogUserName = newBlog.BlogUserName; objBlogs.BlogContent = newBlog.BlogContent; if (BlogCatagories == null) { objBlogs.BlogCategory = null; } else { objBlogs.BlogCategory = GetSelectedBlogCategories(newBlog, BlogCatagories); } _context.Blogs.Add(objBlogs); _context.SaveChanges(); return(Task.FromResult(objBlogs)); } catch (Exception ex) { DetachAllEntities(); throw ex; } }
public Task <bool> UpdateSMTPServerAsync(string SMTPServer) { var resuts = from Settings in _context.Settings where Settings.SettingName == "SMTPServer" select Settings; resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPServer); _context.SaveChanges(); return(Task.FromResult(true)); }
public async Task EmailSendAsync(string email, string subject, string message) { var objGeneralSettings = await _GeneralSettingsService.GetGeneralSettingsAsync(); string strError = await _EmailService.SendMailAsync( false, email, email, "", "", objGeneralSettings.SMTPFromEmail, $"Account Confirmation From: {objGeneralSettings.ApplicationName} {subject}", $"This is an account confirmation email from: {objGeneralSettings.ApplicationName}. {message}"); if (strError != "") { BlazorBlogs.Data.Models.Logs objLog = new Data.Models.Logs(); objLog.LogDate = DateTime.Now; objLog.LogUserName = email; objLog.LogIpaddress = "127.0.0.1"; objLog.LogAction = $"{Constants.EmailError} - Error: {strError} - To: {email} Subject: Account Confirmation From: {objGeneralSettings.ApplicationName} {subject}"; _context.Logs.Add(objLog); _context.SaveChanges(); } }
private async Task <string> SendMailAsync(bool SendAsync, string MailFrom, string MailTo, string MailToDisplayName, string Cc, string Bcc, string ReplyTo, System.Net.Mail.MailPriority Priority, string Subject, Encoding BodyEncoding, string Body, string[] Attachment, string SMTPServer, string SMTPAuthentication, string SMTPUsername, string SMTPPassword, bool SMTPEnableSSL) { string strSendMail = ""; GeneralSettings GeneralSettings = await _generalSettingsService.GetGeneralSettingsAsync(); // SMTP server configuration if (SMTPServer == "") { SMTPServer = GeneralSettings.SMTPServer; if (SMTPServer.Trim().Length == 0) { return("Error: Cannot send email - SMTPServer not set"); } } if (SMTPAuthentication == "") { SMTPAuthentication = GeneralSettings.SMTPAuthendication; } if (SMTPUsername == "") { SMTPUsername = GeneralSettings.SMTPUserName; } if (SMTPPassword == "") { SMTPPassword = GeneralSettings.SMTPPassword; } MailTo = MailTo.Replace(";", ","); Cc = Cc.Replace(";", ","); Bcc = Bcc.Replace(";", ","); System.Net.Mail.MailMessage objMail = null; try { System.Net.Mail.MailAddress SenderMailAddress = new System.Net.Mail.MailAddress(MailFrom, MailFrom); System.Net.Mail.MailAddress RecipientMailAddress = new System.Net.Mail.MailAddress(MailTo, MailToDisplayName); objMail = new System.Net.Mail.MailMessage(SenderMailAddress, RecipientMailAddress); if (Cc != "") { objMail.CC.Add(Cc); } if (Bcc != "") { objMail.Bcc.Add(Bcc); } if (ReplyTo != string.Empty) { objMail.ReplyToList.Add(new System.Net.Mail.MailAddress(ReplyTo)); } objMail.Priority = (System.Net.Mail.MailPriority)Priority; objMail.IsBodyHtml = IsHTMLMail(Body); foreach (string myAtt in Attachment) { if (myAtt != "") { objMail.Attachments.Add(new System.Net.Mail.Attachment(myAtt)); } } // message objMail.SubjectEncoding = BodyEncoding; objMail.Subject = Subject.Trim(); objMail.BodyEncoding = BodyEncoding; System.Net.Mail.AlternateView PlainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Utility.ConvertToText(Body), null, "text/plain"); objMail.AlternateViews.Add(PlainView); //if body contains html, add html part if (IsHTMLMail(Body)) { System.Net.Mail.AlternateView HTMLView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html"); objMail.AlternateViews.Add(HTMLView); } } catch (Exception objException) { // Problem creating Mail Object strSendMail = MailTo + ": " + objException.Message; // Log Error BlazorBlogs.Data.Models.Logs objLog = new Data.Models.Logs(); objLog.LogDate = DateTime.Now; objLog.LogAction = $"{Constants.EmailError} - Error: {strSendMail}"; objLog.LogUserName = MailTo; objLog.LogIpaddress = "127.0.0.1"; } if (objMail != null) { // external SMTP server alternate port int?SmtpPort = null; int portPos = SMTPServer.IndexOf(":"); if (portPos > -1) { SmtpPort = Int32.Parse(SMTPServer.Substring(portPos + 1, SMTPServer.Length - portPos - 1)); SMTPServer = SMTPServer.Substring(0, portPos); } System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); if (SMTPServer != "") { smtpClient.Host = SMTPServer; smtpClient.Port = (SmtpPort == null) ? (int)25 : (Convert.ToInt32(SmtpPort)); switch (SMTPAuthentication) { case "": case "0": // anonymous break; case "1": // basic if (SMTPUsername != "" & SMTPPassword != "") { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new System.Net.NetworkCredential(SMTPUsername, SMTPPassword); } break; case "2": // NTLM smtpClient.UseDefaultCredentials = true; break; } } smtpClient.EnableSsl = SMTPEnableSSL; try { if (SendAsync) // Send Email using SendAsync { // Set the method that is called back when the send operation ends. smtpClient.SendCompleted += SmtpClient_SendCompleted; // Send the email MailMessage objMailMessage = new MailMessage(); objMailMessage = objMail; smtpClient.SendAsync(objMail, objMailMessage); strSendMail = ""; } else // Send email and wait for response { smtpClient.Send(objMail); strSendMail = ""; // Log the Email LogEmail(objMail); objMail.Dispose(); smtpClient.Dispose(); } } catch (Exception objException) { // mail configuration problem if (!(objException.InnerException == null)) { strSendMail = string.Concat(objException.Message, Environment.NewLine, objException.InnerException.Message); } else { strSendMail = objException.Message; } // Log Error BlazorBlogs.Data.Models.Logs objLog = new Data.Models.Logs(); objLog.LogDate = DateTime.Now; objLog.LogAction = $"{Constants.EmailError} - Error: {strSendMail}"; objLog.LogUserName = null; objLog.LogIpaddress = "127.0.0.1"; _context.Logs.Add(objLog); _context.SaveChanges(); } } return(strSendMail); }
public Task <Blogs> CreateBlogAsync(BlogDTO newBlog) { try { Blogs objBlogs = new Blogs(); objBlogs.BlogId = 0; objBlogs.BlogContent = newBlog.BlogContent; objBlogs.BlogDate = newBlog.BlogDate; objBlogs.BlogSummary = newBlog.BlogSummary; objBlogs.BlogTitle = newBlog.BlogTitle; objBlogs.BlogUserName = newBlog.BlogUserName; objBlogs.BlogContent = newBlog.BlogContent; objBlogs.BlogCategory = null; _context.Blogs.Add(objBlogs); _context.SaveChanges(); return(Task.FromResult(objBlogs)); } catch { DetachAllEntities(); throw; } }