public static void SendVerificationEmail(string toAddress, string subject, string body) { /*MailMessage mail = new MailMessage(); * * mail.From = new MailAddress(ConfigurationSettings.AppSettings[SiteMailAddress]); * mail.To.Add(new MailAddress(to)); * mail.Subject = subject; * mail.Body = body; * mail.IsBodyHtml = true; * mail.Priority = MailPriority.Normal; * * SmtpClient smtpClient = new SmtpClient(); * smtpClient.Send(mail);*/ try { GmailMessage.SendFromGmail("ubctextbooksca", "ww610ww715", toAddress, subject, body); } catch (Exception ex) { Log.LogError(MethodInfo.GetCurrentMethod().Name, ex.Message + ex.StackTrace); } }
/// <summary> /// Send a <c>System.Web.Mail.MailMessage</c> through the specified gmail account /// </summary> /// <param name="gmailUserName">The username of the gmail account that the message will be sent through</param> /// <param name="gmailPassword">The password of the gmail account that the message will be sent through</param> /// <param name="message"><c>System.Web.Mail.MailMessage</c> object to send</param> /*public static void SendMailMessageFromGmail(string gmailUserName, string gmailPassword, MailMessage message) * { * try * { * message.Fields[SMTP_SERVER] = GmailMessage.GmailServer; * message.Fields[SMTP_SERVER_PORT] = GmailMessage.GmailServerPort; * message.Fields[SEND_USING] = 2; * message.Fields[SMTP_USE_SSL] = true; * message.Fields[SMTP_AUTHENTICATE] = 1; * message.Fields[SEND_USERNAME] = gmailUserName; * message.Fields[SEND_PASSWORD] = gmailPassword; * * System.Web.Mail.SmtpMail.Send(message); * } * catch (Exception ex) * { * //TODO: Add error handling * throw ex; * } * }*/ /// <summary> /// Sends an email through the specified gmail account /// </summary> /// <param name="gmailUserName">The username of the gmail account that the message will be sent through</param> /// <param name="gmailPassword">The password of the gmail account that the message will be sent through</param> /// <param name="toAddress">Recipients email address</param> /// <param name="subject">Message subject</param> /// <param name="messageBody">Message body</param> public static void SendFromGmail(string gmailUserName, string gmailPassword, string toAddress, string subject, string messageBody) { try { GmailMessage gMessage = new GmailMessage(gmailUserName, gmailPassword); gMessage.To = toAddress; gMessage.Subject = subject; gMessage.Body = messageBody; gMessage.From = gmailUserName; if (gmailUserName.IndexOf('@') == -1) { gMessage.From += "@gmail.com"; } /*gMessage.To = toAddress; * gMessage.Subject = subject; * gMessage.Body = messageBody; * gMessage.From = gmailUserName; * if (gmailUserName.IndexOf('@') == -1) gMessage.From += "@Gmail.com"; * //gMessage.BodyFormat = System.Net.Mail.MailMessage.IsBodyHtml;//System.Web.Mail.MailFormat.Html;//System.Net.Mail.MailMessage.IsBodyHTML * * System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); * mail.To.Add(gMessage.To); * mail.From = new MailAddress(gMessage.From, "Email head", System.Text.Encoding.UTF8); * mail.Subject = gMessage.Subject; * mail.SubjectEncoding = System.Text.Encoding.UTF8; * mail.Body = gMessage.Body; * mail.BodyEncoding = System.Text.Encoding.UTF8; * mail.IsBodyHtml = true; * mail.Priority = MailPriority.High; * SmtpClient client = new SmtpClient(); * client.Credentials = new System.Net.NetworkCredential(gmailUserName, gmailPassword); * client.Port = 587; * client.Host = "smtp.gmail.com"; * client.EnableSsl = true; * client.UseDefaultCredentials = false;*/ MailMessage mm = new MailMessage(gMessage.From, gMessage.To); mm.Subject = gMessage.Subject; mm.Body = gMessage.Body; mm.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(gMessage.From, gMessage.GmailPassword); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); //System.Web.Mail.SmtpMail.Send(gMessage); //client.Send(mail); } catch (Exception ex) { //TODO: Add error handling throw ex; } }