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

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

我使用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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm默认主题_pycharm主题插件

    pycharm默认主题_pycharm主题插件一、Pycharm基本设置(小白篇)1、打开Pycharm设置,【File】-【Settings】2、设置解释器,【File】-【Settings】-【Project:项目名字】-【ProjectInterpreter】-【设置图标】-【Add】-【浏览到目标解释器】,选择相应解释器即可。3、设置pycharm主题,【File】-【Settings】-【Appearance&Behavior】-【Appearance】;Theme:修改主题、Usecustomf

    2022年8月26日
    2
  • 【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌

    【伪装位置神器】神行者AnyLocation 1.3.0001可用于微信,陌陌软件名称:神行者(激活成功教程)软件版本:v1.3.0001授权类别:免费测试机型:大可乐手机下载链接:http://pan.baidu.com/s/1qWwSMde密码:m7mq应用简介:神行者Any

    2022年8月2日
    7
  • maven配置阿里云镜像仓库「建议收藏」

    maven配置阿里云镜像仓库「建议收藏」1.修改maven根目录下的conf文件夹中的setting.xml文件<mirrors><mirror><id>alimaven</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf&g.

    2022年6月24日
    59
  • 深度信念网络(Deep Belief Network)[通俗易懂]

    深度信念网络(DeepBeliefNetwork,DBN)由GeoffreyHinton在2006年提出。它是一种生成模型,通过训练其神经元间的权重,我们可以让整个神经网络按照最大概率来生成训练数据。我们不仅可以使用DBN识别特征、分类数据,还可以用它来生成数据。DBN由多层神经元构成,这些神经元又分为显性神经元和隐性神经元(以下简称显元和隐元)。显元用于接受输入,隐…

    2022年4月12日
    224
  • laravel 5以后数据库插入自动转化方式

    laravel 5以后数据库插入自动转化方式

    2021年11月10日
    44
  • C++进阶

    C++进阶函数模板函数模板语法函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。语法:template<typenameT>

    2021年12月13日
    46

发表回复

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

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