使用jmail发送邮件

本人使用的是qq邮箱,邮箱需要先开启smtp:

开启方法:

step1

step2,选择账户

step3下拉找到:

邮箱jar包

<dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
</dependency>

代码如下

public class MailDemo {
    public static void main(String[] args) throws AddressException, MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");//设置访问smtp服务器需要认证
        properties.setProperty("mail.transport.protocol", "smtp"); //设置访问服务器的协议
        properties.setProperty("mail.smtp.ssl.enable", "true");

        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true); //打开debug功能

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("564755645@qq.com")); //设置发件人
        msg.setText("send mail test"); //设置邮件内容
        msg.setSubject("mailTest"); //设置邮件主题

        Transport trans = session.getTransport();
        trans.connect("smtp.qq.com", 465, "564755645", "********"); //连接邮箱smtp服务器,端口,你的发件邮箱(@之前),第三个图片的授权码
        trans.sendMessage(msg, new Address[]{new InternetAddress("1240236487@qq.com")}); //发送邮件

        trans.close(); //关闭连接
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值