4

java实现邮件发送

 3 years ago
source link: https://segmentfault.com/a/1190000040553399
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

java实现邮件自动网易邮箱发送

1、配置依赖包

<dependency>

        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>

2、需要到网易邮箱开始服务

注意授权码保存好!!!
image.png

public class ClientEmail {

public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    //设置邮件地址
    props.put("mail.host", "smtp.163.com");
    props.put("mail.transport.protocol", "smtp");
    //开启认证
    props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props, null);
    Transport transport = session.getTransport();
    //用户名
    String user = "[email protected]";
    //授权码
    String password = "****************";      
    transport.connect(user, password);
    //创建邮件消息
    MimeMessage msg = new MimeMessage(session);
    msg.setSentDate(new Date());
    //邮件发送人
    InternetAddress fromAddress = new InternetAddress(user, "邮件服务");
    msg.setFrom(fromAddress);
    //邮件接收人
    String to = "[email protected]";
    InternetAddress[] toAddress = new InternetAddress[]{new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, toAddress);
    //邮件主题
    msg.setSubject("测试邮件发送", "UTF-8");
    //内容和附件
    Multipart multipart = new MimeMultipart();
    //内容
    BodyPart contentBodyPart = new MimeBodyPart();
    contentBodyPart.setContent("test00001", "text/html;charset=UTF-8");
    multipart.addBodyPart(contentBodyPart);
    //附件
    BodyPart fileBody = new MimeBodyPart();
    DataSource source = new FileDataSource("C:/图片");
    fileBody.setDataHandler(new DataHandler(source));
    fileBody.setFileName("0b8da2cf424d1.jpg");
    multipart.addBodyPart(fileBody);
    //邮件内容
    msg.setContent(multipart);
    msg.saveChanges();
    //发送
    transport.sendMessage(msg, msg.getAllRecipients());
}

注意要配置好参数,不然会发送有问题。


Recommend

  • 122
    • 掘金 juejin.im 6 years ago
    • Cache

    node.js使用Nodemailer发送邮件

    原文链接:Node.js使用Nodemailer发送邮件 电子邮件是—种用电子手段提供信息交换的通信方式,是互联网应用最广的服务。通过网络的电子邮件系统,用户可以以非常低廉的价格(不管发送到哪里,都只需负担网费)、非常快速的方式(几秒钟之内可以发送到世界上任何

  • 41
    • blog.51cto.com 6 years ago
    • Cache

    linux 邮件发送-技术改变生活

    vim/etc/[email protected]=smtp.163.comsetsmtp-auth-user=abcmeabcsetsmtp-auth-password=授权码setsmtp-auth=login然后即可发送邮件echohelloword|mail-s"title"[email protected]

  • 28

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。本文对python使用smtplib发送邮件做一...

  • 28

    邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送变得非常容易,本文我们就来一探究竟!看看...

  • 44
    • www.tuicool.com 4 years ago
    • Cache

    定时发送邮件

    背景 甲方爸爸:新接入业务在国庆以及军运会期间需要每天巡检业务并发送邮件告知具体情况! 我司:没问题。 甲方爸爸:假期也要发噢。 我司:没问题(草泥马)。 刚开始计划指定几个同事...

  • 42
    • 微信 mp.weixin.qq.com 4 years ago
    • Cache

    10 分钟实现 Java 发送邮件功能

    code小生  一个专注大前端领域的技术平台 公众号回复 Android 加入安卓技术群 作者 | yizhiwazi 链接 | jianshu.com/p/5eb000544dd7

  • 6

    目录 ①创建项目

  • 2

    V2EX  ›  问与答 如何实现邮件发件人名称随机发送邮件?  

  • 6
    • blog.51cto.com 1 year ago
    • Cache

    Python3实现自动发送邮件

    Python3实现自动发送邮件 精选 原创 ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。

  • 6

    Express实现定时发送邮件 Posted on 2023-04-10 17:10 

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK