Esempio n. 1
0
        /// <summary>
        /// Sends an e-mail using a template as specified in <paramref name="request"/>.
        /// </summary>
        /// <param name="request">The request containing all necessary data for sending an e-mail.</param>
        /// <remarks>
        /// <para>
        /// The parameters that have a default value of <c>null</c> are optional. The rest are required.
        /// </para>
        /// <para>
        /// If the culture is specified in the request, and the configuration contains an alternative template for
        /// the specified template with the specified culture, that template is used instead.
        /// </para>
        /// </remarks>
        public async Task SendEmailWithTemplateAsync(SendEmailWithTemplateRequest request)
        {
            var msg = new SendGridMessage
            {
                Personalizations = new List <Personalization>(),
                TemplateId       = request.TemplateId,
                From             = new EmailAddress(request.From ?? this.Config?.DefaultSender?.Address, request.FromName ?? this.Config?.DefaultSender?.Name)
            };

            msg.Personalizations.Add(new Personalization
            {
                TemplateData = request.TemplateData ?? new Dictionary <string, string>(),
                Tos          = new List <EmailAddress>
                {
                    new EmailAddress(request.To, request.ToName)
                }
            });

            var client   = new SendGridClient(this.Config.ApiKey);
            var response = await client.SendEmailAsync(msg);

            if (response.StatusCode != System.Net.HttpStatusCode.OK && response.StatusCode != System.Net.HttpStatusCode.Accepted)
            {
                // The e-mail was not sent, so we need to report that with an exception.
                var body = await response.Body.ReadAsStringAsync();

                throw new System.Net.Http.HttpRequestException($"The SendGrid API returned status code: {response.StatusCode}. Detailed response body: {body}");
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> SendEmailWithTemplateHttp([HttpTrigger(AuthorizationLevel.Function, "POST")] HttpRequestMessage request, [DurableClient] IDurableClient client)
        {
            Dictionary <string, string> data = null;
            var json = await request.Content.ReadAsStringAsync();

            if (!string.IsNullOrEmpty(json))
            {
                data = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            }

            var query = request.RequestUri.ParseQueryString();

            var sendRequest = new SendEmailWithTemplateRequest
            {
                From         = query.Get("from"),
                FromName     = query.Get("fromname"),
                TemplateData = data,
                TemplateId   = query.Get("templateid"),
                To           = query.Get("to"),
                ToName       = query.Get("toname")
            };

            var instanceId = await client.StartNewAsync(Names.SendEmailWithTemplateOrch, sendRequest);

            return(client.CreateCheckStatusResponse(request, instanceId));
        }