java 发送QQ邮件工具类详细步骤
前言:通过发送QQ邮件提醒用户还书的时间到了。
第一步:打开QQ邮箱,点击设置->出现下面图片的界面,再点击账号,如图所示
第二步:开启IMAP/SMTP服务,开启后会返回授权码,记得好好保存
第三步:导入邮箱jar包
<!-- 邮箱 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
第四步:编写邮箱工具类sendEmailUtils
package com.library.core.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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 SendmailUtil {
// 设置服务器
private static String KEY_SMTP = "mail.host";
private static String VALUE_SMTP = "smtp.qq.com";
private static String SMTP_PORT_KEY = "mail.smtp.port";
private static String SMTP_PORT_VALUE = "465";
// 服务器验证
private static String MAIL_SMTP_AUTH = "mail.smtp.auth";
private static String VALUE_AUTH = "true";
// 发件人用户名、密码
private String SEND_USER = "[email protected]";
private String SEND_UNAME = "2431850729";
private String SEND_PWD = "你的授权码";
// 建立会话
private MimeMessage message;
private Session s;
/*
* 初始化方法
*/
public SendmailUtil() {
Properties props = System.getProperties();
props.setProperty(KEY_SMTP, VALUE_SMTP);
props.setProperty(MAIL_SMTP_AUTH, VALUE_AUTH);
props.setProperty(SMTP_PORT_KEY,SMTP_PORT_VALUE );
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", SMTP_PORT_VALUE);
props.setProperty("mail.debug", "true");
s = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
}});
s.setDebug(true);
message = new MimeMessage(s);
}
/**
* 发送邮件
*
* @param headName
* 邮件头文件名
* @param sendHtml
* 邮件内容
* @param receiveUser
* 收件人地址
*/
public void doSendHtmlEmail(String headName, String sendHtml,
String receiveUser) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendmailUtil se = new SendmailUtil();
se.doSendHtmlEmail("message for your", "hello,I can take case for you?I need you vary much,can you help me", "[email protected]");
}
}
第五步:运行一下 main函数,效果图如下