发送QQ邮件

参考 Java发送邮件示例 

发邮件必须的几个参数,发件人邮箱地址,发件人QQ邮箱开通的stmp服务后得到的客户端授权码,收件人邮箱,邮件subject,邮件content

 需要依赖jar包


	javax.mail
	mail
	1.5.0-b01

开启qq邮箱stmp服务 登陆qq邮箱,选择设置-账户- POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 开启

发送QQ邮件_第1张图片

 java文件

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

public class SendQQMailUtil {

	public static void SendQQMail(String to, String subject, String content)
			throws AddressException, MessagingException {
		String user = "*****@qq.com"; // qq邮箱地址
		String password = "stmppassword"; // 密码为QQ邮箱开通的stmp服务后得到的客户端授权码

		Properties properties = new Properties();
		properties.put("mail.transport.protocol", "smtp");// 连接协议
		properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
		properties.put("mail.smtp.port", 465);// 端口号
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
		properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息

		Session session = Session.getInstance(properties);// 得到回话对象

		Message message = new MimeMessage(session);// 获取邮件对象
		message.setFrom(new InternetAddress(user)); // 设置发件人邮箱地址 //设置收件人邮箱地址
		message.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });// to
		message.setRecipients(Message.RecipientType.CC, new InternetAddress[] { new InternetAddress(to) });// 抄送
		// message.setRecipient(Message.RecipientType.TO,newInternetAddress(to));//一个收件人
		message.setSubject(subject);// 设置邮件标题
		message.setText(content);// 设置邮件内容

		Transport transport = session.getTransport(); // 得到邮差对象
		transport.connect(user, password);// // 连接自己的邮箱账户
		transport.sendMessage(message, message.getAllRecipients());// 发送邮件
		transport.close();
	}
}

测试

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;


public class App {
	public static void main(String[] args) throws AddressException, MessagingException {
		String to = "****@**.com";// 收件箱地址
		String subject = "send email test from qq.com";
		String content = "this is email content";
		SendQQMailUtil.SendQQMail(to, subject, content);
	}
}

 

你可能感兴趣的:(java)