1.打开POP3/SMTP服务并获取授权码
这里使用的是qq邮箱,路径:登录后,设置–>账户往下翻就能看到下面图片,默认是关闭状态,点击“开启”,他会让你发短信,当你发送短信后,点击“我已发送”,就可以获取到授权码,这授权码就是下面要用到的密码
2.创建快速maven项目
3.添加依赖pom.xml
<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>org.example</groupId>
<artifactId>javamail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>javamail</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3.发送普通文本的邮件
package org.example.javamail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* 发送普通文本的邮件
* 使用JavaMail发送邮件的步骤:
* 1.创建Session对象,加载Properties对象
* 2.通过Session对象得到Transport传输对象
* 3.使用邮箱的用户名和密码连接服务器
* 4.设置Message邮件对象
* 5.发送邮件
* @author Administrator
*
*/
public class TextMail {
public static void main(String[] args) throws MessagingException {
// 配置邮箱服务器的相关配置
Properties properties = new Properties();
//设置邮箱服务器的主机名
properties.setProperty("mail.smtp.host", "smtp.qq.com");
//设置邮箱服务器的端口
properties.setProperty("mail.smtp..port", "25");
//设置邮箱服务器是否需要身份认证(设置为true表示需要身份认证)
properties.setProperty("mail.smtp.auth", "true");
/*使用JavaMail发送邮件 */
//1.创建Session对象,加载Properties对象
Session session = Session.getInstance(properties);
//开启Session的debug模式,可以在控制台看到邮件发送的运行状态
session.setDebug(true);
//2.通过Session对象得到Transport传输对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和密码连接邮件服务器(用户名:@符号前面的内容;密码:授权码,不是登录密码)
transport.connect("smtp.qq.com", "邮箱@符号前面的内容", "授权码");
//4.设置Message邮件对象
Message message = createSimpleMessage(session);
//5.发送邮件
transport.sendMessage(message, message.getAllRecipients());
//6.关闭传输对象
transport.close();
}
private static Message createSimpleMessage(Session session) throws MessagingException {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件的发送人
message.setFrom("发送人的邮箱");
//设置邮件的接收人(可以多个)可以是自己
//TO表示发送这,CC表示抄送,BCC表示密送
message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的邮箱"));
//设置邮件的主题
message.setSubject("主题");
//设置发送日期
message.setSentDate(new Date());
//设置邮件的文本内容
message.setText("哎,脖子疼");
return message;
}
}
4.发送HTML的邮件
package org.example.javamail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 发送Html的邮件
* 使用JavaMail发送邮件的步骤:
* 1.创建Session对象,加载Properties对象
* 2.通过Session对象得到Transport传输对象
* 3.使用邮箱的用户名和密码连接服务器
* 4.设置Message邮件对象
* 5.发送邮件
* @author Administrator
*
*/
public class HtmlMail {
public static void main(String[] args) throws MessagingException {
// 配置邮箱服务器的相关配置
Properties properties = new Properties();
//设置邮箱服务器的主机名
properties.setProperty("mail.smtp.host", "smtp.qq.com");
//设置邮箱服务器的端口
properties.setProperty("mail.smtp..port", "25");
//设置邮箱服务器是否需要身份认证(设置为true表示需要身份认证)
properties.setProperty("mail.smtp.auth", "true");
/*使用JavaMail发送邮件 */
//1.创建Session对象,加载Properties对象
Session session = Session.getInstance(properties);
//开启Session的debug模式,可以在控制台看到邮件发送的运行状态
session.setDebug(true);
//2.通过Session对象得到Transport传输对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和密码连接邮件服务器(用户名:@符号前面的内容;密码:授权码,不是登录密码)
transport.connect("smtp.qq.com", "邮箱@符号前面的内容", "授权码");
//4.设置Message邮件对象
Message message = createHtmlMessage(session);
//5.发送邮件
transport.sendMessage(message, message.getAllRecipients());
//6.关闭传输对象
transport.close();
}
/**
* 创建html内容的邮件
* @param session
* @return
* @throws MessagingException
*/
private static Message createHtmlMessage(Session session) throws MessagingException {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件的发送人
message.setFrom("发送人的邮箱");
//设置邮件的接收人(可以多个)
//TO表示发送这,CC表示抄送,BCC表示密送
message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的邮箱"));
//设置邮件的主题
message.setSubject("主题");
//设置发送日期
message.setSentDate(new Date());
/* 准备邮件数据*/
//1.设置多媒体对象容器(一个MultiPart对象中可以包含一个或多个BodyPart对象)
MimeMultipart multipart = new MimeMultipart();
//2.设置邮件体对象
MimeBodyPart bodyPart = new MimeBodyPart();
//3.设置HTML内容
StringBuffer sb = new StringBuffer();
sb.append("<html><body><a href='http://www.baidu.com'>百度一下</a></body></html>");
//4.将HTML内容设置到邮件体对象中
bodyPart.setContent(sb.toString(), "text/html;charset=UTF-8");
//5.将邮件体添加到多媒体对象容器
multipart.addBodyPart(bodyPart);
//5.将多媒体对象容器设置到message邮件对象中
message.setContent(multipart);
return message;
}
}
5.发送带有附件的邮件
package org.example.javamail;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 发送带有附件的邮件
* 使用JavaMail发送邮件的步骤:
* 1.创建Session对象,加载Properties对象
* 2.通过Session对象得到Transport传输对象
* 3.使用邮箱的用户名和密码连接服务器
* 4.设置Message邮件对象
* 5.发送邮件
* @author Administrator
*
*/
public class AttachMail {
public static void main(String[] args) throws MessagingException {
// 配置邮箱服务器的相关配置
Properties properties = new Properties();
//设置邮箱服务器的主机名
properties.setProperty("mail.smtp.host", "smtp.qq.com");
//设置邮箱服务器的端口
properties.setProperty("mail.smtp..port", "25");
//设置邮箱服务器是否需要身份认证(设置为true表示需要身份认证)
properties.setProperty("mail.smtp.auth", "true");
/*使用JavaMail发送邮件 */
//1.创建Session对象,加载Properties对象
Session session = Session.getInstance(properties);
//开启Session的debug模式,可以在控制台看到邮件发送的运行状态
session.setDebug(true);
//2.通过Session对象得到Transport传输对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和密码连接邮件服务器(用户名:@符号前面的内容;密码:授权码,不是登录密码)
transport.connect("smtp.qq.com", "邮箱@符号前面的内容", "授权码");
//4.设置Message邮件对象
Message message = createAttachMessage(session);
//5.发送邮件
transport.sendMessage(message, message.getAllRecipients());
//6.关闭传输对象
transport.close();
}
private static Message createAttachMessage(Session session) throws MessagingException {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//设置邮件的发送人
message.setFrom("发送人的邮箱");
//设置邮件的接收人(可以多个)
//TO表示发送这,CC表示抄送,BCC表示密送
message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的邮箱"));
//设置邮件的主题
message.setSubject("主题");
//设置发送日期
message.setSentDate(new Date());
/*创建邮件内容*/
//1.创建邮件正文
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent("<h2>这是一封包含附件的邮件</h2>","text/html;charset=UTF-8");
//2.创建附件对象
MimeBodyPart attachPart = new MimeBodyPart();
//2.1本地文件
DataHandler df = new DataHandler(new FileDataSource("D:\\user.txt"));
//2.2将本地文件设置到附件对象中
attachPart.setDataHandler(df);
//2.3设置附件文件名
attachPart.setFileName(df.getName());
//3.创建多媒体容器对象
MimeMultipart multipart = new MimeMultipart();
//3.1添加正文
multipart.addBodyPart(bodyPart);
//3.2添加附件
multipart.addBodyPart(attachPart);
//3.3如果邮件中要添加附件,设置为mixed
multipart.setSubType("mixed");
//4.即将多媒体容器对象设置到邮件对象
message.setContent(multipart);
return message;
}
}