0%

JavaMail

1.加载jar包

1
2
3
4
5
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>

JavaMail jar包下载地址
maven仓库地址

2.使用的主要类

  • Session 类
  • Message 类
  • Transport 类

    3.发送一封邮件

  1. 设置session类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //加载props
    Properties prop = new Properties();
    //从文件中读取
    InputStreamReader inputStreamReader = new InputStreamReader(UtilMail.class.getClassLoader().getResourceAsStream("mail.properties"),"GBK");
    //或者在代码中设置
    props.setProperty("mail.smtp.host","smtp.163.com");//这里用163邮箱举例,不同的邮箱smtp服务器地址不同。
    props.setProperty("mail.smtp.auth","true");
    props.setProperty("mail.transport.protocol","smtp");
    Session session = Session.getInstance(prop);
    //mail.smtp.host是必须设置的。
    其他参数设置详见 javaMail官方文档
  2. 设置Message类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Message mimeMessage = new MimeMessage(session);
    //设置发送人的信息
    mimeMessage.setFrom(new InternetAddress(xxx@xxx.com,"xxx","UTF-8"));
    //设置发送的邮件正文
    mimeMessage.setContent("正文","text/html;charset=UTF-8");
    //设置发送的邮件主题
    mimeMessage.setSubject("主题","UTF-8");
    //设置接受人邮件地址
    mimeMessage.setRecipients(MimeMessage.RecipientType.TO,xxx@xxx.com);
    //设置发送时间
    mimeMessage.setSentDate(new Date());
  3. 设置Transport类
    1
    Transport.send(mimeMessage,"xxx@xxx.com","这写发送邮箱的授权码");//此为懒人写法,好用就行

4.代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package util.utilmail;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

public class UtilMail {
private static Properties prop = null;
private static Session session =null;
private static MimeMessage mimeMessage =null;

private static String sendAddress = null;
private static String receiveAddress = null;
private static String sendAddressPassword = null;
private static String contentType = null;
private static String subjectType = null;
private static String sendNicknameType = null;
private static String sendNickname = null;

static {
Properties properties = new Properties();
prop = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(UtilMail.class.getClassLoader().getResourceAsStream("mail_user.properties"),"GBK");
InputStreamReader inputStreamReader1 = new InputStreamReader(UtilMail.class.getClassLoader().getResourceAsStream("mail_connection.properties"),"GBK");
prop.load(inputStreamReader1);
properties.load(inputStreamReader);
} catch (IOException e) {
e.printStackTrace();
}

sendAddress = properties.getProperty("sendAddress");
receiveAddress = properties.getProperty("receiveAddress");
sendAddressPassword = properties.getProperty("sendAddressPassword");
contentType = properties.getProperty("contentType");
subjectType = properties.getProperty("subjectType");
sendNicknameType = properties.getProperty("sendNicknameType");
sendNickname = properties.getProperty("sendNickname");
session = Session.getInstance(prop);
mimeMessage = new MimeMessage(session);
}

public static void sendMessage(String nickname,String subject,String content,String receiver) throws MessagingException, UnsupportedEncodingException {
mimeMessage.setFrom(new InternetAddress(sendAddress,nickname,sendNicknameType));
mimeMessage.setContent(content,contentType);
mimeMessage.setSubject(subject,subjectType);
mimeMessage.setRecipients(MimeMessage.RecipientType.TO,receiver);
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage,sendAddress,sendAddressPassword);
}

public static void sendMessage(String subject,String content) throws MessagingException, UnsupportedEncodingException {
mimeMessage.setFrom(new InternetAddress(sendAddress,sendNickname,sendNicknameType));
mimeMessage.setContent(content,contentType);
mimeMessage.setSubject(subject,subjectType);
mimeMessage.setRecipients(MimeMessage.RecipientType.TO,receiveAddress);
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage,sendAddress,sendAddressPassword);
}
}