无法使用SendGrid WebApi lib发送电子邮件

纳西尔·保利诺(Nathiel Paulino)

每次尝试发送电子邮件时,都会收到与跨域相关的BadRequest。我试图搜索问题,但似乎是因为我从浏览器(本地主机)调用,因此无法正常工作。基本上,我正在对我的Aspnet mvc进行Ajax调用,并且他们调用了WebApi项目

public async Task<Response> SendEmail(string email, string link, string companyName)
    {
        var apiKey = Environment.GetEnvironmentVariable("SMTP");
        var client = new SendGridClient(apiKey);

        var from = new EmailAddress("d.com", "d");
        var subject = "Sending with SendGrid is Fun";
        var to = new EmailAddress(email, "Caro");
        var plainTextContent = "and easy to do anywhere, even with C#";
        var htmlContent = "<strong>Aqui está seu contrato </strong>";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        return await client.SendEmailAsync(msg);

    }

那就是回应:

{服务器:nginx日期:2019年12月10日,星期二14:17:40 GMT连接:keep-alive Access-Control-Allow-Origin:https ://sendgrid.api-docs.io Access-Control-Allow-Methods:POST访问控制允许标头:授权,内容类型,代表,x-sg-elas-acl访问控制最大年龄:600 X-No-CORS原因:https:// sendgrid。 com / docs / Classroom / Basics / API / cors.html }

不知道该怎么办了。我应该在HttpClientClass的标题中添加一些内容吗?

亚瑟·格里高良
 private async Task<string> SendMail(string to, string text)
        {
            try
            {
                var msg = new SendGridMessage();
                msg.SetFrom(new EmailAddress("[email protected]", "Your Name"));
                msg.AddTo(to);
                msg.SetSubject("Your subject here");
                msg.AddContent(MimeType.Text, text);
                var client = new SendGridClient("???"); // Your sendgrid client private id here
                var response = await client.SendEmailAsync(msg);
                if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
                    return "ok";
                else return "failed"; // not happening ))
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章