gpt4 book ai didi

java - 如何向通过 SendGrid(Java 中)从 Google App Engine 发送的电子邮件添加附件?

转载 作者:行者123 更新时间:2023-12-02 11:22:33 26 4
gpt4 key购买 nike

我关注了Google's information通过 SendGrid 从 App Engine 发送电子邮件。使用 Java library for SendGrid 可以正常工作。以及提供的示例代码:

import packageName.Sendgrid;

Sendgrid mail = new Sendgrid("<sendgrid_username>","<sendgrid_password>");
mail.setTo("foo@bar.com")
.setFrom("me@bar.com")
.setSubject("Subject goes here")
.setText("Hello World!")
mail.send();

但现在我需要附加一个文件。如何才能做到这一点?我在 sendgrid-google-java library 中找不到 addAttachment 函数或类似的函数.

最佳答案

我只使用SendGrid Java API在 appengine 上,而不是特定的 google 上。这是一个例子:

import com.sendgrid.*;

public class SendGridExample {
public static void main(String[] args) {
SendGrid sendgrid = new SendGrid("SENDGRID_APIKEY");

SendGrid.Email email = new SendGrid.Email();

email.addTo("test@sendgrid.com");
email.setFrom("you@youremail.com");
email.setSubject("Sending with SendGrid is Fun");
email.setHtml("and easy to do anywhere, even with Java");

SendGrid.Response response = sendgrid.send(email);
}
}

https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/java.html

您可以使用以下 3 个函数之一将附件添加到此电子邮件对象:

    public Email addAttachment(String name, File file) throws IOException, FileNotFoundException {
return this.addAttachment(name, new FileInputStream(file));
}

public Email addAttachment(String name, String file) throws IOException {
return this.addAttachment(name, new ByteArrayInputStream(file.getBytes()));
}

public Email addAttachment(String name, InputStream file) throws IOException {
this.attachments.put(name, file);
return this;
}

关于java - 如何向通过 SendGrid(Java 中)从 Google App Engine 发送的电子邮件添加附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39785808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com