使用java实现发送邮件功能

1、获取邮箱的授权码(这里以qq邮箱为例)

qq邮箱–>设置–>账户,开启POP3/SMTP服务,然后可以得到一串16位授权码
在这里插入图片描述

2、添加依赖,我使用maven管理依赖

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

3、编写代码

如果使用qq邮箱发送代码如下

package com.example.demo;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class SendMail {
    public static void main(String[] args) throws MessagingException, GeneralSecurityException {
        Properties properties = new Properties();
        /*
        * 指定连接的邮件服务器的主机名。如:qq邮箱就填写smtp.qq.com
        * 若在本地测试的话,需要在本地安装smtp服务器
        */
        properties.put("mail.smtp.host", "smtp.qq.com");
        /*
        * 指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件
        * mail.store.protocol:指定邮件接收协议
        */
        properties.put("mail.transport.protocol", "smtp");
        // 指定客户端是否要向邮件服务器提交验证
        properties.put("mail.smtp.auth", "true");
        
        // 邮件接收方
        String to = "xxxxxxxxx@163.com";
        // 邮件发送方
        String from = "xxxxxxxxx@qq.com";
        // 授权码
        String authCode = "xxxxxxxxxxxxx";

        // qq邮箱需要设置SSL加密
        MailSSLSocketFactory factory = new MailSSLSocketFactory();
        factory.setTrustAllHosts(true);

        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", factory);

        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, authCode);
            }
        };
        /*
         * 创建session对象
         */
        Session session = Session.getInstance(properties, authenticator);
       // 当设置为true,JavaMail API就会将其运行过程和邮件服务器的交互命令信息输出到console中,用于JavaMail的调试
        session.setDebug(true);
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 设置邮件发送方
        message.setFrom(new InternetAddress(from));
        // 设置邮件接收方
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        // 设置邮件主题
        message.setSubject("my first mail");
        // 设置邮件内容
        message.setContent("hello world", "text/html;charset=UTF-8");

        // 根据Session获取邮件传输对象
        Transport transport = session.getTransport();
        // 连接邮件服务器,前两个参数可以省略
        transport.connect("smtp.qq.com", 465, from, authCode);
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("success");
    }
}

如果使用163邮箱发送代码如下

package com.example.demo;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class SendMail {
    public static void main(String[] args) throws MessagingException {
        Properties properties = new Properties();
        /*
        * 指定连接的邮件服务器的主机名。如:qq邮箱就填写smtp.qq.com
        * 若在本地测试的话,需要在本地安装smtp服务器
        */
        properties.put("mail.smtp.host", "smtp.163.com");
        /*
        * 指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件
        * mail.store.protocol:指定邮件接收协议
        */
        properties.put("mail.transport.protocol", "smtp");
        // 指定客户端是否要向邮件服务器提交验证
        properties.put("mail.smtp.auth", "true");

        // 邮件接收方
        String to = "xxxxxxxxxx@qq.com";
        // 邮件发送方
        String from = "xxxxxxxx@163.com";
        // 授权码
        String authCode = "xxxxxxxxxxxxxx";

        Session session = Session.getInstance(properties);
        // 当设置为true,JavaMail API就会将其运行过程和邮件服务器的交互命令信息输出到console中,用于JavaMail的调试
        session.setDebug(true);
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 设置邮件发送方
        message.setFrom(new InternetAddress(from));
        // 设置邮件接收方
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        // 设置邮件主题
        message.setSubject("my first mail");
        // 设置邮件内容
        message.setContent("hello world", "text/html;charset=UTF-8");

        // 根据Session获取邮件传输对象
        Transport transport = session.getTransport();
        // 连接邮件服务器,前两个参数可以省略
        transport.connect("smtp.163.com", 25, from, authCode);
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("success");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值