public static void SendEmail(string[] recipients, string subject, string body, bool async = true, bool isHtml = false) { if (recipients == null || recipients.Length == 0) { return; } AsyncSendMail mailer = new AsyncSendMail( Settings.MailServer, !string.IsNullOrEmpty(Settings.MailServerLogin), Settings.MailServerLogin, Settings.MailServerPassword, Settings.MailServerPort, Settings.MailUseSSL, recipients, Settings.MailFromAddress, null, subject, body, null, isHtml); if (async && !Settings.EmailDebug) { mailer.SendAsynchronously(); } else { mailer.SendSynchronously(); } }
protected void btnTestEmail_Click(object sender, EventArgs e) { divTestEmail.Visible = true; divTestEmail.InnerHtml = "Sending test email...<br>"; try { //mailsender.SendSynchronously(); AsyncSendMail mailer = new AsyncSendMail( Settings.MailServer, !string.IsNullOrEmpty(Settings.MailServerLogin), Settings.MailServerLogin, Settings.MailServerPassword, Settings.MailServerPort, Settings.MailUseSSL, new string[] { "*****@*****.**" }, Settings.MailFromAddress, null, "test", "test", null, false); mailer.SendSynchronously(); divTestEmail.InnerHtml += "Email sent successfully!"; divTestEmail.Style["color"] = "Green"; } catch (Exception ex) { divTestEmail.InnerHtml += "<b>ERROR sending email</b>:<br><br>"; divTestEmail.InnerHtml += ex.ToString(); divTestEmail.Style["color"] = "Red"; } }
public static void SendEmailToUserGroup(int groupId, string subject, string body, bool sendPm = false) { List <string> mailRecipients = new List <string>(); List <int> pmRecipients = new List <int>(); using (var cn = DB.CreateOpenConnection()) { //addin subscribers to the recipients list var usersInGroup = User.GetUserIdsInGroup(groupId); DbDataReader dr = cn.ExecuteReader( @"SELECT UserID, Email FROM ForumUsers WHERE UserID IN (" + usersInGroup.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y) + ") AND Disabled=0"); while (dr.Read()) { if (sendPm) { pmRecipients.Add(Convert.ToInt32(dr["UserID"])); } else { if (AsyncSendMail.MailAddressTryParse(dr["Email"].ToString())) { mailRecipients.Add(dr["Email"].ToString()); } } } dr.Close(); } if (sendPm) { foreach (var userId in pmRecipients) { User.SendPM(userId, subject + "\n\n" + body); } } else { AsyncSendMail mailer = new AsyncSendMail( Settings.MailServer, !string.IsNullOrEmpty(Settings.MailServerLogin), Settings.MailServerLogin, Settings.MailServerPassword, Settings.MailServerPort, Settings.MailUseSSL, mailRecipients.ToArray(), Settings.MailFromAddress, null, subject, body, null, false); mailer.SendSynchronously(); } }