java发送邮件(qq邮箱)

1、依赖

 <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

2、发送

package com.demo;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @author : xipeng
 * @description : 发送电子邮件
 * @date : 2021/7/3 19:29
 */
public class SendEmail {

    public static String emailProtocol = "smtp";
    public static String emailAddress = "123456789@qq.com";
    public static String emailPassword = "shgaiuhg";
    public static String emailSMTPHost = "smtp.qq.com";
    public static String emailSMTPPort = "465";
    public static String emailToAddress = "123456789@qq.com";

    public static void main(String[] args) {
        try {
            Properties properties = new Properties();
            /**
             * 使用的协议(JavaMail规范要求)
             */
            properties.put("mail.transport.protocol", emailProtocol);
            /**
             * 发件人的邮箱的 SMTP 服务器地址
             */
            properties.put("mail.smtp.host", emailSMTPHost);
            /**
             * 端口号
             */
            properties.put("mail.smtp.port", emailSMTPPort);
            /**
             * 需要请求认证
             */
            properties.put("mail.smtp.auth", "true");
            /**
             * 是否使用ssl安全连接
             */
            properties.put("mail.smtp.ssl.enable", "true");
            /**
             * ;设置debug信息
             */
            properties.put("mail.debug", "true");

            Session session = Session.getInstance(properties);
            MimeMessage message = new MimeMessage(session);
            /**
             * 设置发件人邮箱
             */
            message.setFrom(new InternetAddress(emailAddress));
            /**
             * 设置多个收件人
             */
//        message.setRecipients(Message.RecipientType.TO,new InternetAddress[]{
//                new InternetAddress(emailToAddress),
//                new InternetAddress(emailToAddress)
//        });
            /**
             * 设置单个收件人
             */
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAddress));
            /**
             * 设置邮件标题
             */
            message.setSubject("测试发送");
            /**
             * 设置邮件内容
             */
            message.setText("邮件正文");
            /**
             * 获取邮差
             */
            Transport transport = session.getTransport();
            /**
             * 接入发送方邮箱
             */
            transport.connect(emailAddress, emailPassword);
            /**
             * 发送邮件
             * 邮件内容及邮件接收方信息
             */
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值