java发送qq邮件

1.开启POP3/SMTP协议

设置--》账户--》

2.添加依赖:

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

3.代码

package com.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * JavaMail发送邮件:前提是QQ邮箱里帐号设置要开启POP3/SMTP协议
 */
public class SendMail {

	public static void main(String[] args) throws Exception {

		Properties prop = new Properties();
		// 开启debug调试,以便在控制台查看
		prop.setProperty("mail.debug", "true");
		// 设置邮件服务器主机名
		prop.setProperty("mail.host", "smtp.qq.com");
		// 发送服务器需要身份验证
		prop.setProperty("mail.smtp.auth", "true");
		// 发送邮件协议名称
		prop.setProperty("mail.transport.protocol", "smtp");

		// 开启SSL加密,否则会失败
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);

		// 创建session
		Session session = Session.getInstance(prop);
		// 通过session得到transport对象
		Transport ts = session.getTransport();
		// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
		// =====================自行修改账号和授权码
		ts.connect("smtp.qq.com", "xxxxxxxxxxx", "dtsctlfcdwwwwwwww");// 后面的字符是授权码
		// 创建邮件
		Message message = createSimpleMail(session);
		// 发送邮件
		ts.sendMessage(message, message.getAllRecipients());
		ts.close();
	}

	/**
	 * @Method: createSimpleMail
	 * @Description: 创建一封只包含文本的邮件
	 */
	public static MimeMessage createSimpleMail(Session session) throws Exception {
		// 创建邮件对象
		MimeMessage message = new MimeMessage(session);
		// 指明邮件的发件人
		message.setFrom(new InternetAddress("xxxxxxxxxx@qq.com"));
		// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
		message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxxxxxxxxx@qq.com"));
		// 邮件的标题
		message.setSubject("JavaMail测试");
		// 邮件的文本内容
		message.setContent("<font style='color:red'>JavaMail发送邮件成功!</font>", "text/html;charset=UTF-8");
		// 返回创建好的邮件对象
		return message;
	}
}

 

### 使用 Java 发送 QQ 邮件的 SMTP API 示例 为了使用 JavaQQ 邮箱发送邮件,需要配置 SSL 连接并设置正确的属性。以下是完整的代码示例: #### Maven 依赖项 首先,在项目的 `pom.xml` 文件中添加必要的依赖项来引入 JavaMail 库。 ```xml <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> ``` #### 设置 QQ 邮箱授权码 在发送邮件之前,需获取 QQ 邮箱的授权码。这可以通过登录 QQ 邮箱,进入设置页面中的账户选项卡完成。按照提示操作以获得用于应用程序的身份验证令牌[^4]。 #### 编写发送邮件程序 下面是一个基于 JUnit 测试框架编写的简单例子,展示了如何利用带有 SSL 的 SMTP 协议向 QQ 用户发送消息。 ```java import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendEmail { private static final String MY_ACCOUNT = "your_qq_email@qq.com"; private static final String AUTHORIZATION_CODE = "your_authorization_code"; public void send() throws MessagingException { Properties props = new Properties(); // 开启SSL加密传输 props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(MY_ACCOUNT, AUTHORIZATION_CODE); } }); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(MY_ACCOUNT)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Test Email from Java App"); message.setText("This is a test email sent using JavaMail and QQ's SMTP server."); Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 此段代码设置了与 QQ SMTP 服务器通信所需的所有必要参数,并创建了一个简单的纯文本电子邮件对象。注意这里使用的是 SSL 加密方式(端口号为 465),并且提供了用户名和密码来进行身份验证[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝文龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值