C# 使用sendgrid邮件帮助程序时附加到sendgrid api不起作用
我已经浏览了这里关于将文件附加到sendgrid电子邮件的问题,但似乎没有一个问题是我遇到的 我的问题是。如何使用api在sendgrid中发送带有附件的电子邮件C# 使用sendgrid邮件帮助程序时附加到sendgrid api不起作用,c#,sendgrid,C#,Sendgrid,我已经浏览了这里关于将文件附加到sendgrid电子邮件的问题,但似乎没有一个问题是我遇到的 我的问题是。如何使用api在sendgrid中发送带有附件的电子邮件 dynamic sg = new SendGridAPIClient(apiKey); var from = new SendGrid.Helpers.Mail.Email("jkennedy@domain.com"); var subject = "Hello World from the Send
dynamic sg = new SendGridAPIClient(apiKey);
var from = new SendGrid.Helpers.Mail.Email("jkennedy@domain.com");
var subject = "Hello World from the SendGrid C# Library!";
var to = new SendGrid.Helpers.Mail.Email(toAddress);
var content = new Content("multipart/form-data", "Textual content");
var attachment = new Attachment {Filename = attachmentPath };
var mail = new Mail(from, subject, to, content);
var ret = mail.Get();
mail.AddAttachment(attachment);
dynamic response = await sg.client.mail.send.post(requestBody: ret);
如果我把mail.attachment放在get之后,邮件会发送,但是没有附件。如果我把addattachment行放在get之前,我会收到一条“bad request”消息
我还没有找到一个具体如何做到这一点的例子
另外,文件的路径是c:\tblaccudatacounts.csv。我使用的是第三方编写的助手。我同意SendGrid的建议。请参阅下面正在运行的代码
var myMessage = new SendGridMessage {From = new MailAddress("info@email.com")};
myMessage.AddTo("Jeff Kennedy <info@info.com>");
myMessage.Subject = "test email";
myMessage.Html = "<p>See Attachedment for Lead</p>";
myMessage.Text = "Hello World plain text!";
myMessage.AddAttachment("C:\\tblaccudatacounts.csv");
var apiKey = "apikey given by sendgrid";
var transportWeb = new Web(apiKey);
await transportWeb.DeliverAsync(myMessage);
var myMessage=new SendGridMessage{From=new MailAddress(“info@email.com")};
myMessage.AddTo(“杰夫·肯尼迪”);
myMessage.Subject=“测试电子邮件”;
myMessage.Html=“请参见潜在客户的附件””;
myMessage.Text=“你好,世界纯文本!”;
myMessage.AddAttachment(“C:\\tblaccudatacounts.csv”);
var apiKey=“sendgrid提供的apiKey”;
var transportWeb=新站点(apiKey);
等待transportWeb.DeliverAsync(myMessage);
我想明白了。我使用的是第三方编写的助手。我同意SendGrid的建议。请参阅下面正在运行的代码
var myMessage = new SendGridMessage {From = new MailAddress("info@email.com")};
myMessage.AddTo("Jeff Kennedy <info@info.com>");
myMessage.Subject = "test email";
myMessage.Html = "<p>See Attachedment for Lead</p>";
myMessage.Text = "Hello World plain text!";
myMessage.AddAttachment("C:\\tblaccudatacounts.csv");
var apiKey = "apikey given by sendgrid";
var transportWeb = new Web(apiKey);
await transportWeb.DeliverAsync(myMessage);
var myMessage=new SendGridMessage{From=new MailAddress(“info@email.com")};
myMessage.AddTo(“杰夫·肯尼迪”);
myMessage.Subject=“测试电子邮件”;
myMessage.Html=“请参见潜在客户的附件””;
myMessage.Text=“你好,世界纯文本!”;
myMessage.AddAttachment(“C:\\tblaccudatacounts.csv”);
var apiKey=“sendgrid提供的apiKey”;
var transportWeb=新站点(apiKey);
等待transportWeb.DeliverAsync(myMessage);
经过几个小时的努力,我使用sendgrid的V3API找到了答案。这是我学到的
在您的示例中,调用var ret=mail.Get()代码>添加附件之前。由于mail.Get()
本质上是将邮件对象序列化为SendGrid所期望的Json格式,因此在mail.Get()
调用之后添加附件实际上不会将其添加到邮件对象中
您应该知道的另一件事是,API没有简单地将文件路径作为输入的方法(至少我可以找到,我希望有人能纠正我)。您至少需要手动设置内容(作为基本64字符串)和文件名。你可以找到更多的信息
以下是我的工作解决方案:
string apiKey = "your API Key";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("your@domain.com");
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("destination@there.com");
Content body = new Content("text/plain", "Hello, Email!");
Mail mail = new Mail(from, subject, to, body);
byte[] bytes = File.ReadAllBytes("C:/dev/datafiles/testData.txt");
string fileContentsAsBase64 = Convert.ToBase64String(bytes);
var attachment = new Attachment
{
Filename = "YourFile.txt",
Type = "txt/plain",
Content = fileContentsAsBase64
};
mail.AddAttachment(attachment);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
经过几个小时的努力,我使用sendgrid的V3API找到了答案。这是我学到的
在您的示例中,调用var ret=mail.Get()代码>添加附件之前。由于mail.Get()
本质上是将邮件对象序列化为SendGrid所期望的Json格式,因此在mail.Get()
调用之后添加附件实际上不会将其添加到邮件对象中
您应该知道的另一件事是,API没有简单地将文件路径作为输入的方法(至少我可以找到,我希望有人能纠正我)。您至少需要手动设置内容(作为基本64字符串)和文件名。你可以找到更多的信息
以下是我的工作解决方案:
string apiKey = "your API Key";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("your@domain.com");
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("destination@there.com");
Content body = new Content("text/plain", "Hello, Email!");
Mail mail = new Mail(from, subject, to, body);
byte[] bytes = File.ReadAllBytes("C:/dev/datafiles/testData.txt");
string fileContentsAsBase64 = Convert.ToBase64String(bytes);
var attachment = new Attachment
{
Filename = "YourFile.txt",
Type = "txt/plain",
Content = fileContentsAsBase64
};
mail.AddAttachment(attachment);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
是的,从V3开始,他们引入了用于附件的Base64编码数据。在这之前,它还可以正常工作。是的,从V3开始,他们已经为附件引入了Base64编码数据。在此之前,它工作得很好。你从哪里得到这个代码示例的?官方API URL显示了上面@DStage31所建议的内容。SendGrid.Web.WebWhere获得此代码示例?官方API URL显示了@DStage31所建议的内容。SendGrid.Web.Web