Exemplo n.º 1
0
        public async Task SendMailAsync(string To, string subject, string messageContent = null, Stream stream = null,
                                        string fileName = null)
        {
            var sendGridMessage = new SendGridMessage();

            sendGridMessage.AddPersonalization(To, subject);

            var content = messageContent == null ? subject : messageContent;

            sendGridMessage.AddContent(content);

            if (stream != null && !String.IsNullOrEmpty(fileName))
            {
                await sendGridMessage.AddAttchament(fileName, stream);
            }

            sendGridMessage.From = new EmailAddress()
            {
                Email = "*****@*****.**"
            };

            var jsonRequest = JsonConvert.SerializeObject(sendGridMessage, _serializerSettings);

            var respone = await _client.PostAsync(_settings.SendUrl,
                                                  new StringContent(jsonRequest, Encoding.UTF8, "application/json"));

            if (!respone.IsSuccessStatusCode)
            {
                throw new Exception("Cant send mail.");
            }
        }