public Task SendAsync(IdentityMessage message, GlobalValuesVM g) { var task = new Task(() => { string currDir = @"c:\TestSms\"; string nameOfEmail = Path.Combine(currDir, "sms_" + message.Destination + "_" + DateTime.Now.Ticks.ToString() + ".txt"); string[] strArray = new string[3]; strArray[0] = "TO: " + message.Destination; strArray[1] = "FROM: " + g.FromEmailAddress; strArray[2] = message.Body; try { File.WriteAllLines(nameOfEmail, strArray); } catch { throw; } }); task.Start(); //a blank return Task.FromResult(0); }
public override async Task SendAsync(IdentityMessage message, GlobalValuesVM g) { string sendGridUserName = g.SendGridUserName; string sendGridPassword = g.SendGridPassword; string fromEmailAddress = g.FromEmailAddress; string bccEmailAddress = g.BccEmailAddress; string subject = message.Subject; string body = message.Body; var myMessage = new SendGridMessage(); myMessage.AddTo(message.Destination); myMessage.From = new System.Net.Mail.MailAddress(fromEmailAddress); myMessage.AddBcc(bccEmailAddress); myMessage.Subject = subject; myMessage.Text = body; myMessage.Html = body; var credentials = new NetworkCredential( sendGridUserName, sendGridPassword); //Create the transport var transportWeb = new Web(credentials); //Send Email if (transportWeb != null) { try { //sendGridUserName if (sendGridUserName.ToLower() == string.Format("Enter Your sendgrid.com User Id").ToLower()) throw new Exception("You have not entered your Send Grid USER NAME"); //sendGridPassword if (sendGridPassword.ToLower() == string.Format("Enter Your sendgrid.com Password").ToLower()) throw new Exception("You have not entered your Send Grid PASSWORD"); await transportWeb.DeliverAsync(myMessage); } catch (Exception ex) { throw new HttpException("EMAIL Exception. Unable to resolve the remote server. Please try again later. Error: " + ex.Message); } } else { System.Diagnostics.Trace.TraceError("Failed to create a Web Transport"); await Task.FromResult(0); throw new HttpException("EMAIL Exception. Failed to create a Web Transport"); } }
public async override Task SendAsync(IdentityMessage message, GlobalValuesVM g) { var task = new Task(() => { string currDir = @"c:\TestEmails\"; string nameOfEmail = Path.Combine(currDir, "email_" + message.Destination + "_" + DateTime.Now.Ticks.ToString() + ".txt"); string[] strArray = new string[4]; strArray[0] = "TO: " + message.Destination; strArray[1] = "FROM: " + g.FromEmailAddress; strArray[2] = "SUBJECT:" + message.Subject; strArray[3] = message.Body; File.WriteAllLines(nameOfEmail, strArray); }); task.Start(); await task; }
public UsersController() { db = new ApplicationDbContext(); globalValues = new GlobalValuesVM(db); }
public async override Task SendAsync(IdentityMessage message, GlobalValuesVM g) { //getting values from db string messageFrom = !string.IsNullOrEmpty(g.FromEmailAddress) ? g.FromEmailAddress.ToLower() : string.Empty; string host = !string.IsNullOrEmpty(g.SmtpServer) ? g.SmtpServer.ToLower() : string.Empty; int port = !string.IsNullOrEmpty(g.SmtpServer) ? int.Parse(g.SmtpPort) : 0; string smtpPassword = !string.IsNullOrEmpty(g.SmtpPassword) ? g.SmtpPassword : string.Empty; string smtpUser = !string.IsNullOrEmpty(g.SmtpUser) ? g.SmtpUser.ToLower() : string.Empty; string smtpDomain = !string.IsNullOrEmpty(g.SmtpDomain) ? g.SmtpDomain.ToLower() : string.Empty; string messageBccEmailAddress = !string.IsNullOrEmpty(g.BccEmailAddress) ? g.BccEmailAddress.ToLower() : "false"; string subject = message.Subject; string body = message.Body; string destination = message.Destination; using (SmtpClient smtpClient = new SmtpClient()) { smtpClient.UseDefaultCredentials = false; //smtpClient.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword); smtpClient.EnableSsl = true; smtpClient.Host = host; smtpClient.Port = port; smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtpClient.Timeout = 300000; //send try { bool IsSendBcc = bool.Parse(g.IsSendBcc); if (string.IsNullOrEmpty(smtpDomain)) smtpClient.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword); else smtpClient.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword, smtpDomain); if (!AliKuli.Validators.MyValidators.IsValidEmail(messageFrom)) throw new Exception("FROM email is not valid."); if (!AliKuli.Validators.MyValidators.IsValidEmail(messageBccEmailAddress)) throw new Exception("BCC email is not valid."); if (!AliKuli.Validators.MyValidators.IsValidEmail(destination)) throw new Exception("TO email is not valid."); //error catching //IsSendBcc if (IsSendBcc) messageBccEmailAddress = g.BccEmailAddress; //SmtpPassword if (smtpPassword.ToLower() == (string.Format("SmtpPassword").ToLower())) throw new Exception("SMTP Password has not been set."); //SmtpUser if (smtpUser.ToLower() == (string.Format("SmtpUser").ToLower())) throw new Exception("SMTP User has not been set."); //SMTP Domain if (smtpDomain.ToLower() == string.Format("Your SMTP Domain").ToLower()) throw new Exception("SMTP Domain has not been set."); //MailMessage m = new MailMessage(messageFrom, message.Destination, message.Subject, message.Body); MailMessage m = new MailMessage(); m.From = new MailAddress(messageFrom); m.To.Add(destination); m.Subject = subject; m.Body = body; m.IsBodyHtml = true; await smtpClient.SendMailAsync(m); } catch (Exception) { throw; } } }