服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Java Web stmp发送带附件邮件(附SSL版)

Java Web stmp发送带附件邮件(附SSL版)

2021-05-04 12:03luck-cheng Java教程

这篇文章主要为大家详细介绍了Java Web stmp发送带附件邮件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java web stmp发送带附件邮件的具体代码,供大家参考,具体内容如下

?
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
public class mailfilesendutils {
 
 private properties props; //系统属性
 private session session; //邮件会话对象
 private mimemessage mimemsg; //mime邮件对象
 private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象
 
 /**
  * constructor
  * @param
  */
 public mailfilesendutils(){
  props = system.getproperties();
  props.put("mail.smtp.auth","false");
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * constructor
  * @param smtp 邮件发送服务器
  */
 public mailfilesendutils(string smtp, string username, string password){
  props = system.getproperties();
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("username", username);
  props.put("password", password);
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * 发送邮件
  */
 public boolean sendmail(string from, string[] to, string subject, string content, string filename) {
  try {
   //设置发信人
   mimemsg.setfrom(new internetaddress(from));
   //设置接收人
   for (int i = 0; i < to.length; i++) {
    mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i]));
   }
   //设置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i]));
//   }
   //设置主题
   mimemsg.setsubject(subject);
   //设置正文
   bodypart bp = new mimebodypart();
   bp.setcontent(content, "text/html;charset=utf-8");
   mp.addbodypart(bp);
   //设置附件
   bp = new mimebodypart();
   filedatasource fileds = new filedatasource(filename);
   bp.setdatahandler(new datahandler(fileds));
   bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b"));
   mp.addbodypart(bp);
   mimemsg.setcontent(mp);
   mimemsg.savechanges();
   //发送邮件
   if(props.get("mail.smtp.auth").equals("true")){
    transport transport = session.gettransport("smtp");
    transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password"));
    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to));
//    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc));
    transport.close();
   }else{
    transport.send(mimemsg);
   }
   system.out.println("邮件发送成功");
  } catch (messagingexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (unsupportedencodingexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  return true;
 }
 
// public void tosendmail(sendmailparam sendmailparam){
//  mailfilesendutils email = new mailfilesendutils(sendmailparam.getsmtp(), sendmailparam.getusername(), sendmailparam.getpassword());
//  email.sendmail(sendmailparam.getfrom(), sendmailparam.getto(), sendmailparam.getsubject(), sendmailparam.getcontent(), sendmailparam.getfilepath());
// }
 
 
 public static void main(string[] args) {
  string smtp = "smtp.exmail.qq.com";
  string username = "发送的邮箱账号";
  string password = "发送的邮箱密码";
  string from = "发送的邮箱";
  string[] to = {"接收邮件的邮箱"};
//  string[] copyto = {"抄送的邮箱"};
  string subject = "主题6";
  string content = "邮件内容6";
  string filename = "附件的文件";
  mailfilesendutils email = new mailfilesendutils(smtp, username, password);
//  email.sendmail(from, to, copyto, subject, content, filename);
  email.sendmail(from, to, subject, content, filename);
 }
 
}

(附:ssl版)

 

?
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
public class mailfilesendutils {
 
 private properties props; //系统属性
 private session session; //邮件会话对象
 private mimemessage mimemsg; //mime邮件对象
 private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象
 
 /**
  * constructor
  * @param
  */
 public mailfilesendutils(){
  props = system.getproperties();
  props.put("mail.smtp.auth","false");
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * constructor
  * @param smtp 邮件发送服务器
  */
 public mailfilesendutils(string smtp,
        string username,
        string password){
  security.addprovider(new com.sun.net.ssl.internal.ssl.provider());
  final string ssl_factory = "javax.net.ssl.sslsocketfactory";
  props = system.getproperties();
  mailsslsocketfactory sf = null;
  try {
   sf = new mailsslsocketfactory();
  } catch (generalsecurityexception e) {
  }
  sf.settrustallhosts(true);
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("mail.smtp.socketfactory.class", ssl_factory);
  props.put("mail.smtp.socketfactory.fallback", "false");
  props.put("mail.smtp.ssl.enable", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.ssl.socketfactory", sf);
 
//  props.put("username", username);
//  props.put("password", password);
  session = session.getinstance(props, new authenticator() {
   @override
   protected passwordauthentication getpasswordauthentication() {
    return new passwordauthentication(username, password);
   }
  });
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * 发送邮件
  */
 public boolean sendmail(string from,
       string[] to,
       string subject,
       string content,
       string filename) {
  try {
   //设置发信人
   mimemsg.setfrom(new internetaddress(from));
   //设置接收人
   for (int i = 0; i < to.length; i++) {
    mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i]));
   }
   //设置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i]));
//   }
   //设置主题
   mimemsg.setsubject(subject);
   //设置正文
   bodypart bp = new mimebodypart();
   bp.setcontent(content, "text/html;charset=utf-8");
   mp.addbodypart(bp);
   //设置附件
   bp = new mimebodypart();
   filedatasource fileds = new filedatasource(filename);
   bp.setdatahandler(new datahandler(fileds));
   bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b"));
   mp.addbodypart(bp);
   mimemsg.setcontent(mp);
   mimemsg.savechanges();
   //发送邮件
   if(props.get("mail.smtp.auth").equals("true")){
    transport transport = session.gettransport("smtp");
    transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password"));
    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to));
//    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc));
    transport.close();
   }else{
    transport.send(mimemsg);
   }
   system.out.println("邮件发送成功");
  } catch (messagingexception e) {
   e.printstacktrace();
  } catch (unsupportedencodingexception e) {
   e.printstacktrace();
  }
  return true;
 }
 
 public boolean tosendmail(sendmailparam sendmailparam){
  mailfilesendutils email = new mailfilesendutils(
    sendmailparam.getsmtp(),
    sendmailparam.getusername(),
    sendmailparam.getpassword());
  email.sendmail(
    sendmailparam.getfrom(),
    sendmailparam.getto(),
    sendmailparam.getsubject(),
    sendmailparam.getcontent(),
    sendmailparam.getfilepath());
  return true;
 }
 
 
// public static void main(string[] args) {
//  string smtp = "smtp.mxhichina.com";
//  string username = "邮箱";
//  string password = "邮箱密码";
//  string from = "谁去发";
//  string[] to = {"发给谁"};
////  string[] copyto = {"抄送的邮箱"};
//  string subject = "huawei";
//  string content = "邮件内容6666";
//  string filename = "gdt-3583118353-ad-20170823.xls";
//  mailfilesendutils email = new mailfilesendutils(smtp, username, password);
////  email.sendmail(from, to, copyto, subject, content, filename);
//  email.sendmail(from, to, subject, content, filename);
// }
 
}

在项目中使用这套工具,main方法我注释掉,然后使用tosendmail(sendmailparam sendmailparam)。
这里定义的sendmailparam 为:

 

?
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
public class sendmailparam {
 private string smtp;
 private string username;
 private string password;
 private string from;//发送人
 private string[] to;//接收人
 //  string[] copyto = {"909891736@qq.com"};
 private string subject;//邮件主题
 private string content;//邮件内容
 private string filepath;//文件拿到的路径
 
 public sendmailparam(){
  this.smtp = "smtp.exmail.qq.com";//例子
  this.username = "邮箱账号";
  this.password = "邮箱密码";
  this.from = "邮箱";
  this.subject = "";
  this.content = "";
  this.filepath = "";
 }
 
 public string getsmtp() {
  return smtp;
 }
 
 public void setsmtp(string smtp) {
  this.smtp = smtp;
 }
 
 public string getusername() {
  return username;
 }
 
 public void setusername(string username) {
  this.username = username;
 }
 
 public string getpassword() {
  return password;
 }
 
 public void setpassword(string password) {
  this.password = password;
 }
 
 public string getfrom() {
  return from;
 }
 
 public void setfrom(string from) {
  this.from = from;
 }
 
 public string[] getto() {
  return to;
 }
 
 public void setto(string[] to) {
  this.to = to;
 }
 
 public string getsubject() {
  return subject;
 }
 
 public void setsubject(string subject) {
  this.subject = subject;
 }
 
 public string getcontent() {
  return content;
 }
 
 public void setcontent(string content) {
  this.content = content;
 }
 
 public string getfilepath() {
  return filepath;
 }
 
 public void setfilepath(string filepath) {
  this.filepath = filepath;
 }
}

maven依赖包

?
1
2
3
4
5
<dependency>
  <groupid>javax.mail</groupid>
  <artifactid>mail</artifactid>
  <version>1.4.7</version>
 </dependency>

gradle依赖包

?
1
compile "javax.mail:mail:1.4.7"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_20032995/article/details/72458636

延伸 · 阅读

精彩推荐