1.所需jar包
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
还要注意的是 邮箱要smtp服务
2.直接贴代码吧。。
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import java.io.FileInputStream;
import java.util.Properties;
public class Mail {
private MimeMessage mimeMessage;//Mime邮件对象
private Session session;//邮件会话对象
private Properties properties;//系统属性
private boolean needAuth = false;//smtp是否需要认证
//smtp认证的用户名和密码
private String username;
private String password;
private Multipart multipart;//Multipart对象 邮件内容 标题 附件等内容添加到这里面 然后生成MimeMessage对象
/**
* 构造方法
* @param smtp
*/
public Mail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
}
/**
* 创建MimeMessage邮件对象
* @return
*/
public boolean createMimeMessage() {
//获取邮件会话对象
session = Session.getDefaultInstance(properties, null);
//创建Mime邮件对象
mimeMessage = new MimeMessage(session);
multipart = new MimeMultipart();
return true;
}
/**
* 设置邮件发送服务器
* @param hostName
*/
public void setSmtpHost(String hostName) {
if (properties==null){
properties=System.getProperties();//获得系统属性对象
}
properties.put("mail.smtp.host",hostName);//设置smtp主机
}
/**
* 设置smtp是否需要认证
* @param need
*/
public void setNeedAuth(boolean need){
if (properties == null){
properties = System.getProperties();
}
if (need){
properties.put("mail.smtp.auth","true");
}else {
properties.put("mail.smtp.auth","false");
}
}
/**
* 发件人的用户名和密码 163的用户名就是邮箱的前缀
* @param username
* @param password
*/
public void setNamePassword(String username,String password){
this.username = username;
this.password = password;
}
/**
* 邮件主题
* @param subject
* @return
*/
public boolean setSubject(String subject){
try {
mimeMessage.setSubject(subject);
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 邮件正文
* @param mailBody
* @return
*/
public boolean setBody(String mailBody){
BodyPart bodyPart = new MimeBodyPart();
try {
bodyPart.setContent(""+mailBody,"text/html;charset=utf-8");
multipart.addBodyPart(bodyPart);
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 邮件正文(带图片的)
* @param mailBody
* @param imgFile
* @return
*/
public boolean setBodyWithImg(String mailBody,String imgFile){
BodyPart content = new MimeBodyPart();
BodyPart img = new MimeBodyPart();
try {
multipart.addBodyPart(content);
multipart.addBodyPart(img);
ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(new FileInputStream(imgFile),"application/octet-stream");
// DataHandler imgDataHandler = new DataHandler(new FileDataSource(imgFile));
DataHandler imgDataHandler = new DataHandler(byteArrayDataSource);
img.setDataHandler(imgDataHandler);
// img.setContent
String imgFilename = imgFile.substring(imgFile.lastIndexOf("/")+1);//图片文件名
//注意:Content-ID的属性值一定要加上<>,不能直接写文件名
String headerValue = "<"+imgFilename+">";
img.setHeader("Content-ID",headerValue);
//为图片设置文件名,有的邮箱会把html内嵌的图片也当成附件
img.setFileName(imgFilename);
//在html代码中要想显示刚才的图片名 src里不能直接写Content-ID的值,要用cid:这种方式
mailBody+="<img src='cid:"+imgFilename+"' alt='picture' width='100px' height='100px' />,骚吗?";
content.setContent(""+mailBody,"text/html;charset=utf-8");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 邮件添加附件
* @param file
* @return
*/
public boolean addFileAffix(String file){
String[] fileArray = file.split(",");
for (int i = 0; i < fileArray.length; i++) {
FileDataSource fileDataSource = new FileDataSource(fileArray[i]);
try {
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(fileDataSource));
bodyPart.setFileName(fileDataSource.getName());
multipart.addBodyPart(bodyPart);
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
return true;
}
/**
* 发件人邮箱
* @param from
* @return
*/
public boolean setFrom(String from){
try {
mimeMessage.setFrom(new InternetAddress(from));
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 收件人邮箱
* @param to
* @return
*/
public boolean setTo(String to){
if (to==null)
return false;
try {
//电子邮件可以有三种类型的收件人,分别to、cc(carbon copy)和bcc(blind carbon copy),分别是收件人、抄送、密送
mimeMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 抄送人邮箱 字符串中逗号分开
* @param copyto
* @return
*/
public boolean setCopyTo(String copyto){
if (copyto == null)
return false;
try {
mimeMessage.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto));
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 发送
* @param copyto
* @return
*/
public boolean sendOut(String copyto){
try {
//multipart放入message
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
Session mailSession = Session.getInstance(properties, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect(properties.getProperty("mail.smtp.host"),username,password);
transport.sendMessage(mimeMessage,mimeMessage.getRecipients(Message.RecipientType.TO));
if (copyto!=null){
transport.sendMessage(mimeMessage,mimeMessage.getRecipients(Message.RecipientType.CC));
}
System.out.println("邮件发送成功");
transport.close();
return true;
} catch (MessagingException e) {
System.out.println("邮件发送失败");
e.printStackTrace();
return false;
}
}
/**
* 该方法调用上边定义的方法 选择性的组合 完成邮件发送
* 普普通通的一对一发送
* @param smtp
* @param from
* @param to
* @param subject
* @param content
* @param username
* @param password
* @return
*/
public static boolean send(String smtp,String from,String to,String subject,
String content,String username,String password){
Mail mail = new Mail(smtp);
mail.setNeedAuth(true);//需要认证
if (!mail.setSubject(subject))
return false;
if (!mail.setBody(content))
return false;
if (!mail.setFrom(from))
return false;
if (!mail.setTo(to)){
return false;
}
mail.setNamePassword(username,password);
if (!mail.sendOut(null))
return false;
return true;
}
/**
* 带附件的正文有图片的带有抄送的邮件
* @return
*/
public static boolean sendAndCcWithFile(String smtp,String from,String to,String subject,
String content,String imageFile,String username,String password,String copyto,String filename){
Mail mail = new Mail(smtp);
mail.setNeedAuth(true);//需要认证
if (!mail.setSubject(subject)) {
return false;
}
if (!mail.setBodyWithImg(content,imageFile)) {
return false;
}
if (!mail.addFileAffix(filename))
return false;
if (!mail.setFrom(from)) {
return false;
}
if (!mail.setTo(to)){
return false;
}
if (!mail.setCopyTo(copyto)) {
return false;
}
mail.setNamePassword(username,password);
if (!mail.sendOut(copyto))
return false;
return true;
}
}
- 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
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
测试类:
package com.yannana.jmail;
import javax.mail.internet.MimeBodyPart;
public class MailTest {
public static void main(String[] args) {
String smtp ="smtp.163.com";//SMTP服务器地址
String from = "xiepanpan1995@163.com";
String to = "2908136316@qq.com";
String copyto="xiepanpan2017@163.com,3068611755@qq.com";
String subject = " 狗年大吉";
// String content ="<h1>狗年大吉吧</h1>";
MimeBodyPart img = new MimeBodyPart();
String content ="<div style='color:red;font-size:18px;'>从163发来的邮件</div>我这里有一张自拍";
//正文中的图片
String imgFile="D:/binaryzation/wuyanzu.jpg";
//附件
String filename = "D:/binaryzation/binaryzation-wuyanzu.jpg,D:/binaryzation/newPic.jpg";
//163邮箱用户名就是去掉@163.com
String username = "你的用户名";
String password = "你的邮箱密码";
// Mail.send(smtp,from,to,subject,content,username,password);
Mail.sendAndCcWithFile(smtp,from,to,subject,content,imgFile,username,password,copyto,filename);
}
}