JavaMail + 腾讯企业邮箱 发送邮件

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailSender {

    public static void main(String[] args) {

        // 邮箱用户名和密码
        final String username = "your_email_username@example.com";
        final String password = "your_email_password";

        // 邮箱配置信息
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.exmail.qq.com");  // 邮箱服务器地址
        props.put("mail.smtp.port", "465");  // 邮箱服务器端口号
        props.put("mail.smtp.auth", "true");  // 启用身份验证
        props.put("mail.smtp.ssl.enable", "true");  // 启用 SSL 加密

        // 创建 Session 对象,用于与邮箱服务器进行通信
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // 创建邮件对象
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your_email_username@example.com"));  // 设置发件人邮箱地址
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));  // 设置收件人邮箱地址
            message.setSubject("Testing Subject");  // 设置邮件主题
            message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");  // 设置邮件内容

            // 发送邮件
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

上述代码中,依赖以下 jar 包:

- javax.mail.jar:JavaMail 库,用于发送邮件
- activation.jar:JavaBeans Activation Framework 库,用于处理 MIME 数据类型

这两个 jar 包可以在 Maven 仓库中下载,也可以从以下链接下载:

- javax.mail.jar:https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
- activation.jar:https://mvnrepository.com/artifact/javax.activation/activation/1.1.1

你可以将这两个 jar 包下载后添加到项目的 classpath 中即可使用

- javax.mail.jar:https://repo1.maven.org/maven2/com/sun/mail/javax.mail/1.6.2/javax.mail-1.6.2.jar
- activation.jar:https://repo1.maven.org/maven2/javax/activation/activation/1.1.1/activation-1.1.1.jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值