コード例 #1
0
ファイル: EmailSender.cs プロジェクト: HoodDigital/Hood
        public virtual async Task <int> SendEmailAsync(MailObject message, EmailAddress from = null, EmailAddress replyTo = null)
        {
            if (!Engine.Settings.Mail.SendGridKey.IsSet())
            {
                throw new System.Exception("SendGrid is not setup. Create a free account and set up an API key to send emails.");
            }

            SendGridClient client = GetMailClient();

            if (from == null)
            {
                from = GetSiteFromEmail();
            }

            var html = await _renderer.Render(message.Template, message);

            var msg = MailHelper.CreateSingleEmail(from, message.To, message.Subject, message.ToString(), html);

            msg.ReplyTo = replyTo;
            var response = await client.SendEmailAsync(msg);

            var body = await response.DeserializeResponseBodyAsync(response.Body);

            if (response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK)
            {
                return(1);
            }

            throw new System.Exception("The email could not be sent, check your SendGrid settings.");
        }
コード例 #2
0
        public async Task SendEmail <TModel>(string to, string subject, string templateName, TModel model)
        {
            string view = await _viewRenderer.Render(templateName, model);

            await SendEmail(new List <string> {
                to
            }, subject, view);
        }