示例#1
0
        public async Task <string> SendMail(EmailMessage message, Boolean isHtml)
        {
            msg.AddTo(message.Recipient);
            msg.Subject = message.Subject;
            if (isHtml)
            {
                msg.HtmlContent = message.Body;
            }
            else
            {
                msg.PlainTextContent = message.Body;
            }


            SendGrid.Response response = await client.SendEmailAsync(msg);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return("Message sent successfully");
            }
            else
            {
                return(string.Empty);
            }
        }
示例#2
0
文件: Home.cs 项目: zkhaleqi/bitScry
        public static async Task SendEmail(string fromEmail, string toEmail, string messageText, string subject, string templateId, string apiKey)
        {
            SendGridClient  client           = new SendGridClient(apiKey);
            EmailAddress    from             = new EmailAddress(fromEmail);
            EmailAddress    to               = new EmailAddress(toEmail);
            string          plainTextContent = messageText;
            string          htmlContent      = messageText;
            SendGridMessage message          = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

            message.TemplateId = templateId;
            SendGrid.Response response = await client.SendEmailAsync(message);
        }
示例#3
0
        public async Task SendEmailAsync_ReturnsSuccessResult()
        {
            const string emailFrom   = "*****@*****.**";
            const string emailTo     = "*****@*****.**";
            const string subject     = "test subject";
            const string messageBody = "test body";
            const string guidKey     = "899DE54D-6921-4827-A2A0-DFBA783AC899";

            Mock <SendGridClient> moq = new Mock <SendGridClient>(guidKey, null, null, null, null);

            SendGrid.Response result = await SendGridSender.SendEmailAsync(moq.Object, emailFrom, emailTo, subject, messageBody);

            Assert.True(!Equals(result.Body, null));
        }
示例#4
0
        public async Task <MailSendResult> Send(ISmtpMessage message)
        {
            // TODO: Create Autofac injector for SendGrid.SendGridClient

            var sendGridMessage = MailHelper.CreateSingleEmail(
                from: new SendGrid.Helpers.Mail.EmailAddress(message.From.Address, message.From.Name),
                to: new SendGrid.Helpers.Mail.EmailAddress(message.To.First().Address, message.To.First().Name),
                subject: message.Subject,
                plainTextContent: message.Body,
                htmlContent: message.Body);

            SendGrid.Response response = await _sendGridClient.SendEmailAsync(sendGridMessage);

            return(new MailSendResult());
        }
示例#5
0
        public async Task <bool> Send(string Email, string Name, string toName, string toAddress, string subject, string messageText, string messageHtml)
        {
            var apiKey = SiteConfig.SendGrid;
            var client = new SendGridClient(apiKey);
            var msg    = new SendGridMessage()
            {
                From             = new EmailAddress(Email, Name),
                Subject          = subject,
                PlainTextContent = messageText,
                HtmlContent      = messageHtml
            };

            msg.AddTo(new EmailAddress(toAddress, toName));
            SendGrid.Response response = await client.SendEmailAsync(msg);

            bool success = response.StatusCode == System.Net.HttpStatusCode.Accepted;

            return(success);
        }
示例#6
0
        public async Task <bool> Execute(ConfirmationEmailRequest model)
        {
            var apiKey           = ConfigurationManager.AppSettings["djKey"].ToString(); //Grab Key From Web.Config
            var client           = new SendGridClient(apiKey);
            var from             = new EmailAddress("*****@*****.**");
            var subject          = model.Subject;
            var to               = new EmailAddress(model.To);
            var plainTextContent = model.Body;
            var htmlContent      = model.Body;
            var msg              = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

            SendGrid.Response response = await client.SendEmailAsync(msg);

            if (response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK)
            {
                return(true);
            }
            return(false);
        }
示例#7
0
        private Mock <ISendGridClient> BuildMockSendGridClient()
        {
            var sendGridResponse = new SendGrid.Response(
                new HttpStatusCode(),
                new StringContent(""),
                FormatterServices.GetUninitializedObject(typeof(HttpResponseHeaders)) as HttpResponseHeaders);

            // See www.ronaldrosier.net/Blog/2013/07/23/mocking_a_task_return_method
            TaskCompletionSource <SendGrid.Response> sendGridClientTaskCompletion = new TaskCompletionSource <Response>();

            sendGridClientTaskCompletion.SetResult(sendGridResponse);

            var mockClient = new Mock <ISendGridClient>();

            mockClient.Setup(c => c.SendEmailAsync(It.IsAny <SendGridMessage>(), It.IsAny <CancellationToken>()))
            .Returns(sendGridClientTaskCompletion.Task)
            .Callback(() => { _sendEmailAsyncCallCount++; });

            return(mockClient);
        }
示例#8
0
    static public async Task <string> Send(string toAddress, string subject, string plainBody, string htmlBody)
    {
        if (String.IsNullOrWhiteSpace(toAddress))
        {
            return("");
        }
        if (String.IsNullOrWhiteSpace(subject))
        {
            return("");
        }
        if (String.IsNullOrWhiteSpace(plainBody))
        {
            return("");
        }
        if (String.IsNullOrWhiteSpace(htmlBody))
        {
            return("");
        }
        if (Environment.GetEnvironmentVariable("SENDGRID_API_KEY") == null)
        {
            throw new ArgumentNullException("The environment variable SENDGRID_API_KEY must not be null");
        }
        string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");

        if (apiKey == "")
        {
            throw new ArgumentException("The environment variable SENDGRID_API_KEY must not be empty");
        }

        SendGridClient  client = new SendGridClient(apiKey);
        EmailAddress    from   = new EmailAddress("*****@*****.**", "Kent McKinney");
        EmailAddress    to     = new EmailAddress(toAddress, "File Upload User");
        SendGridMessage msg    = MailHelper.CreateSingleEmail(from, to, subject, plainBody, htmlBody);

        SendGrid.Response response = await client.SendEmailAsync(msg);

        string status = response.StatusCode.ToString();

        return(status);
    }