java实现邮件发送功能_电子邮件是啥

java实现邮件发送功能_电子邮件是啥我使用sendgrid发送电子邮件,使用以下代码工作正常但它没有附件.packagesendgrid;importcom.sendgrid.Content;importcom.sendgrid.Email;importcom.sendgrid.Mail;importcom.sendgrid.Method;importcom.sendgrid.Request;importcom.sen…

我使用sendgrid发送电子邮件,使用以下代码工作正常

但它没有附件.

package sendgrid;

import com.sendgrid.Content;

import com.sendgrid.Email;

import com.sendgrid.Mail;

import com.sendgrid.Method;

import com.sendgrid.Request;

import com.sendgrid.Response;

import com.sendgrid.SendGrid;

import java.io.IOException;

public class SendEmail {

public static void main(String[] args) throws IOException {

Email from = new Email(“test@example.com”);

String subject = “Hello World from the SendGrid Java Library!”;

Email to = new Email(“shareef@gmail.com”);

Content content = new Content(“text/plain”, “Hello, Email!”);

Mail mail = new Mail(from, subject, to, content);

SendGrid sg = new SendGrid(“SG.rIEh84OgQBybYEJcOMie1wd.AZqqdWNYXbOqTarUJcG-iSg0UtHJtCto4oe6tVzn6es”);

Request request = new Request();

try {

request.method = Method.POST;

request.endpoint = “mail/send”;

request.body = mail.build();

Response response = sg.api(request);

System.out.println(response.statusCode);

System.out.println(response.body);

System.out.println(response.headers);

} catch (IOException ex) {

throw ex;

}

}

}

但我需要的是发送附件,所以我搜索github源和Web文档API,由于某种原因没有javadocs但有一个例子GitHub sendgrid所以我尝试,直到它工作,我缩小了一些例外和响应代码,起初我是未经授权的禁止,它更好地响应202,意味着有效和排队(check here)这里任何方式是我的代码,发送电子邮件和附件,但当你打开附件的零大小,并说不能打开或预览文件!

package sendgrid;

import com.sendgrid.Attachments;

import com.sendgrid.Content;

import com.sendgrid.Email;

import com.sendgrid.Mail;

import com.sendgrid.MailSettings;

import com.sendgrid.Method;

import com.sendgrid.Request;

import com.sendgrid.SendGrid;

import com.sendgrid.Setting;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

public class SendEmailAttachmentV2 {

public static void main(String[] args) throws IOException {

sendmail();

}

// Fully populated Mail object

public static void sendmail() throws IOException {

com.sendgrid.Response response1;

Email from = new Email(“shareef@gmail.com”);

String subject = “Hello World from the SendGrid Java Library!”;

Email to = new Email(“shareef@gmail.com”);

Content content = new Content(“text/plain”, “Hello, Email!”);

Mail mail = new Mail(from, subject, to, content);

File file = new File(“C:\\x.png”);

byte[] fileData = null;

try {

fileData = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(file));

} catch (IOException ex) {

}

Attachments attachments3 = new Attachments();

attachments3.setContent(new String(fileData, 0, (int) file.length(), “UTF-8”));

attachments3.setType(“image/png”);//”application/pdf”

attachments3.setFilename(“x.png”);

attachments3.setDisposition(“attachment”);

attachments3.setContentId(“Banner”);

mail.addAttachments(attachments3);

MailSettings mailSettings = new MailSettings();

Setting sandBoxMode = new Setting();

sandBoxMode.setEnable(true);

mailSettings.setSandboxMode(sandBoxMode);

SendGrid sg = new SendGrid(“SG.1Hg78VK0TJ6kexUnByZUYg.LAa5A4GufssZ9lpPQdV6PcZCY6SZ9Xq6LvqfMRG0wesKw”);

Request request1 = new Request();

try {

request1.method = Method.POST;

request1.endpoint = “mail/send”;

request1.body = mail.build();

response1 = sg.api(request1);

System.out.println(response1.statusCode);

System.out.println(response1.body);

System.out.println(response1.headers);

} catch (IOException ex) {

System.out.println(ex);

}

}

}

仅供参考:使用从sendgrid控制台生成的生成的API密钥

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/206687.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 常用字符编码及乱码分析[通俗易懂]

    常用字符编码及乱码分析[通俗易懂]常见的编码GBK、GB2312:表示国标编码,GDB包含简体中文和繁体中文,而DB2312只包含简体中文,也就是说,这两种编码都是描述中文的编码UNICODE编码:Java提供的16进制编码,可以描述世界上任意的文字信息,但是有个问题,若现在所有的字母都使用16进制编码,那么这个编码太庞大了,会造成网络传输的负担ISO8859-1:国际通用编码,但是所有的编码都需要进行转换UTF编码:相…

    2022年5月29日
  • java 股票历史数据_获取股票的历史数据

    java 股票历史数据_获取股票的历史数据packagecom.xiaole.stock;importjava.util.ArrayList;importjava.util.List;importorg.jsoup.Jsoup;importorg.jsoup.nodes.Document;importorg.jsoup.nodes.Element;importorg.jsoup.select.Elements;publicclassGe…

    2022年6月24日
  • linux goland21.2.4激活【最新永久激活】

    (linux goland21.2.4激活)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月30日
  • 二叉树的基本性质及证明

    二叉树的基本性质及证明性质1:一棵非空二叉树的第i层上最多有2^(i-1)个结点,(i>=1)。性质2:一棵深度为k的二叉树中,最多具有2^k-1个结点,最少有k个结点。性质3:对于一棵非空的二叉树,度为0的结点(即叶子结点)总比度为1的结点多一个,即叶子结点数为n0,度为2的结点数为n2,则有n0=n2+1。证明:如果n0表示度为0(即叶子结点)的结点数,用n1表示度为1的结点数,n2表示度为2的结点数,

    2022年5月31日
  • linux服务器安装svn超详细介绍

    linux服务器安装svn超详细介绍 #!/bin/shREPOS="$1"REV="$2"exportLANG=en_US.UTF-8LOG_PATH=/tmp/svn.logecho

    2022年7月2日
  • Laravel修改配置后一定要清理缓存 “php artisan config:clear”!

    Laravel修改配置后一定要清理缓存 “php artisan config:clear”!

    2021年10月22日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号