Java编写邮件发送程序(使用QQ邮箱)
一.准备工作
1.邮箱配置
因为需要使用QQ邮箱作为发送的服务器,所以需要进行一下邮箱的配置【这个过程很重要】,主要步骤如下:
- step1
- step2
- step3
记住最后生成的这个权限码,很重要!这个就是网上大家流传的所谓”密码”!
2.maven项目的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.enmonster.strategy</groupId>
<artifactId>MyMail</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
二.示例
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String [] args)
{
// receiver email
String to = "[email protected]";
//sender email
String from = "[email protected]";
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host","smtp.qq.com");// 设置邮件服务器
props.put("mail.user","[email protected]");
props.put("mail.password","hukn......liacf");//开启pop3/smtp时的验证码
props.put("mail.smtp.port","25");
props.put("mail.smtp.starttls.enable", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(
"[email protected]",
"hukn.....cliacf"); //发件人邮件用户名、授权码
}
});
session.setDebug(true);//代表启用debug模式,可以在控制台输出smtp协议应答的过程
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
控制台的输出
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25
EHLO DESKTOP-EC2S2V4
250-smtp.qq.com
250-PIPELINING
250-SIZE 73400320
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
220 Ready to start TLS
EHLO DESKTOP-EC2S2V4
250-smtp.qq.com
250-PIPELINING
250-SIZE 73400320
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
MTQ3MTgzMTUzOUBxcS5jb20=
334 UGFzc3dvcmQ6
aHVrbm1pZ3preGNsaWFjZg==
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<1471831539@qq.com>
250 Ok
RCPT TO:<shenliu@ahnu.edu.cn>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: shenliu@ahnu.edu.cn
DATA
354 End data with <CR><LF>.<CR><LF>
From: 1471831539@qq.com
To: shenliu@ahnu.edu.cn
Message-ID: <186370029.01529909771614.JavaMail.javamailuser@localhost>
Subject: This is the Subject Line!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
This is actual message
.
250 Ok: queued as
QUIT
221 Bye
Sent message successfully....
Process finished with exit code 0
三.一般报错:
- Error1:
javax.mail.AuthenticationFailedException: 535 authentication failed
535一般是指连接密码不对,可以尝试先使用Foxmail连接一下,然后再用代码连接。