400错误 - x-smtpapi

时间:2016-05-11 15:57:49

标签: c# sendgrid

我正在使用这些API的最新版本: - Sendgrid v6.3.4 - SendGrid.SmtpApi v1.3.1

API的说明如下:https://github.com/sendgrid/sendgrid-csharp

我尝试按照这个示例:https://github.com/sendgrid/smtpapi-csharp但每次尝试都会失败。

我的目标是在没有收件人的情况下使用少量电子邮件进行替换,并在" to"领域。我尝试使用BCC,但它确实可以使用BCC。工作

有我的代码(几乎是示例的副本):

    SendGridMessage mailMsg = new SendGridMessage();

    mailMsg.From = new MailAddress(mail.Sender);

    SendGrid.SmtpApi.Header header = new SendGrid.SmtpApi.Header();
    header.SetTo(mail.Receivers);

    foreach (KeyValuePair<String, List<String>> tag in mail.Tags)
        header.AddSubstitution(tag.Key, tag.Value);

    foreach (String s in mail.BCC)
        mailMsg.AddBcc(s);

    foreach (String s in mail.CC)
        mailMsg.AddCc(s);

    mailMsg.Subject = mail.Subject;

    mailMsg.Html = mail.Content;

    foreach (String s in mail.Attachments)
        mailMsg.AddAttachment(s);

    mailMsg.Headers.Add("X-SMTPAPI", header.JsonString());

    NetworkCredential credential = new NetworkCredential(sLogin, sPassword);
    var transportWeb = new Web(credential);

    try
    {
        transportWeb.DeliverAsync(mailMsg).Wait();
    }

API如何发送电子邮件:

POST https://api.sendgrid.com/api/mail.send.xml HTTP/1.1
User-Agent: sendgrid/6.3.4.0;csharp
Content-Type: multipart/form-data; boundary="2407d110-de4b-4a28-9beb-1da71362d11d"
Host: api.sendgrid.com
Content-Length: 1253
Expect: 100-continue
Connection: Keep-Alive

--2407d110-de4b-4a28-9beb-1da71362d11d
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=headers

{"X-SMTPAPI":"{\"to\" : [\"gregory.wizard@toto.fr\", \"baptiste.butterfly@toto.fr\", \"baptx75@toto.com\", \"tixounet@toto.fr\"],\"sub\" : {\"**coucou**\" : [\"Gregory\", \"Baptiste\", \"BaptX\", \"Tixounet\"],\"**toto**\" : [\"loup\", \"chou\", \"bisou\", \"caillou\"]}}"}
--2407d110-de4b-4a28-9beb-1da71362d11d
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=from

utile@toto.fr
--2407d110-de4b-4a28-9beb-1da71362d11d
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=subject

Test Sendgrid
--2407d110-de4b-4a28-9beb-1da71362d11d
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=html

Salut **coucou**, j'espère que tu vas bien. Je t'aime mon petit **toto** ! Bisous
--2407d110-de4b-4a28-9beb-1da71362d11d
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=api_user

xxxxxxxxxx
--xxxxxxxxxxxxxxxx
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=api_key

xxxxxxxxxx
--xxxxxxxxxxxxxxxx

还有Sendgrid的答案:

HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 11 May 2016 15:35:28 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 136
Connection: keep-alive
X-Frame-Options: DENY

<?xml version='1.0' encoding='UTF-8'?><result><message>error</message><errors><error>Missing destination email</error></errors></result>

我不明白。我遵循API示例,它不起作用。

1 个答案:

答案 0 :(得分:0)

好的,它现在有效!实际上,您必须放两次电子邮件收件人。第一次出现在&#34; To&#34;财产,第二次在feader。

这是我的代码:

private void SendMailToSendGrid(Mail mail)
{
    SendGridMessage mailMsg = new SendGridMessage();

    mailMsg.From = new MailAddress(mail.Sender);

    SendGrid.SmtpApi.Header header = new SendGrid.SmtpApi.Header();
    header.SetTo(mail.Receivers);

    mailMsg.AddTo(mail.Receivers);

    foreach (KeyValuePair<String, List<String>> tag in mail.Tags)
        header.AddSubstitution(tag.Key, tag.Value);

    foreach (String s in mail.BCC)
        mailMsg.AddBcc(s);

    foreach (String s in mail.CC)
        mailMsg.AddCc(s);

    mailMsg.Subject = mail.Subject;

    mailMsg.Html = mail.Content;

    foreach (String s in mail.Attachments)
        mailMsg.AddAttachment(s);

    mailMsg.Headers.Add("x-smtpapi", header.JsonString());

    NetworkCredential credential = new NetworkCredential(sLogin, sPassword);
    var transportWeb = new Web(credential);

    try
    {
        transportWeb.DeliverAsync(mailMsg).Wait();
    }
    catch (InvalidApiRequestException ex)
    {
        var errDetail = new StringBuilder();

        errDetail.Append("ResponseStatusCode: " + ex.ResponseStatusCode + ". ");

        for (int i = 0; i < ex.Errors.Count(); i++)
        {
            errDetail.Append(" -- Error # " + i.ToString() + " : " + ex.Errors[i]);
        }

        throw new ApplicationException(errDetail.ToString(), ex);
    }

}