public static bool EnviarEmailGmail(string destinatario, string assunto, string corpo) { MailMessage mail = new MailMessage(); string template = File.ReadAllText(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "app/shared/mail-templates/basic.html"); template = template.Replace("[TITLE]", assunto); template = template.Replace("[DESCRIPTION]", corpo); template = template.Replace("[ROOTURL]", AppExtension.GetURL()); template = template.Replace("[LOGOURL]", AppExtension.GetURL("/assets/img/logo.png")); mail.From = new MailAddress(ConfigurationManager.AppSettings["gmail_remetente"].ToString(), ConfigurationManager.AppSettings["gmail_display_name"].ToString()); mail.To.Add(destinatario); // para mail.Subject = assunto; // assunto mail.Body = template; // mensagem mail.IsBodyHtml = true; using (var smtp = new SmtpClient("smtp.gmail.com")) { smtp.EnableSsl = true; // GMail requer SSL smtp.Port = 587; // porta para SSL smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // modo de envio smtp.UseDefaultCredentials = false; // vamos utilizar credencias especificas // seu usuário e senha para autenticação smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["gmail_remetente"].ToString(), ConfigurationManager.AppSettings["gmail_senha"].ToString()); // envia o e-mail smtp.Send(mail); return(true); } }
public override void OnActionExecuting(HttpActionContext actionContext) { int usuario = AppExtension.IdUsuarioLogado(); if (usuario <= 0) { throw new Exception("NotLoggedIn"); } }
public static bool EnviarEmail(string destinatario, string assunto, string corpo) { string SGApiKey = ConfigurationManager.AppSettings["sendgrid_api_key"].ToString(); string template = File.ReadAllText(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "app/shared/mail-templates/basic.html"); template = template.Replace("[TITLE]", assunto); template = template.Replace("[DESCRIPTION]", corpo); template = template.Replace("[ROOTURL]", AppExtension.GetURL()); template = template.Replace("[LOGOURL]", AppExtension.GetURL("/assets/img/logo.png")); SendGridClient client = new SendGridClient(SGApiKey); var from = new EmailAddress(ConfigurationManager.AppSettings["sendgrid_address"].ToString()); var subject = assunto; var to = new EmailAddress(destinatario); var htmlContent = template; var plainTextContent = string.Empty; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); var response = client.SendEmailAsync(msg).Result; return(true); }