Send a JavaMail email

Source: Internet
Author: User

001
/*
002
* Copyright (C) Shanghai woshang Information Technology Co., Ltd. 2011-2020
003
* Copyright 2009-2020 voke technology co., LTD.
004
*
005
* This software is the confidential and proprietary information
006
* Voke Corporation ("Confidential Information"). You
007
* Shall not disclose such Confidential Information and shall use
008
* It only in accordance with the terms of the license agreement
009
* You entered into with Voke.
010
*/
011
Package com. wokejia. space. core. util;
012
 
013
Import java. io. UnsupportedEncodingException;
014
Import java. util. ArrayList;
015
Import java. util. Date;
016
Import java. util. List;
017
Import java. util. Properties;
018
 
019
Import javax. mail. Authenticator;
020
Import javax. mail. Message;
021
Import javax. mail. MessagingException;
022
Import javax. mail. PasswordAuthentication;
023
Import javax. mail. Session;
024
Import javax. mail. Transport;
025
Import javax. mail. internet. InternetAddress;
026
Import javax. mail. internet. MimeBodyPart;
027
Import javax. mail. internet. MimeMessage;
028
Import javax. mail. internet. MimeMultipart;
029
 
030
/**
031
* <P class = "detail"> email tool </p>
032
*
033
* @ ClassName: EmailUtil
034
* @ Version V1.0 @ date 2012-5-22 03:57:16 pm
035
* <A href = "http://my.oschina.net/arthor" class = "referer" target = "_ blank"> @ author </a> <a href = "mailto: luoweijun@wokejia.com> Luo Weijun </a>
036
*
037
*/
038
Public class EmailUtil {
039
Private static final int PORT = 25;
040
// Sender information
041
Private static final String SERVER = "smtp.ym.163.com"; // email SERVER mail.cpip.net.cn
042
Private static final String FROM = "name"; // sender, the sender name displayed
043
Private static final String USER = "aaaaaa@qq.com"; // the sender's email address
044
Private static final String PASSWORD = "xxxx"; // PASSWORD
045
Private static final String ENCODING = "GBK"; // email ENCODING
046

047

048
Private String title; // mail title
049
Private String html; // The html content of the email.
050
Private List <EmailEvent> emailEvents = new ArrayList <EmailEvent> (); // email data
051
Private static EmailUtil emailUtil = new EmailUtil ();
052
Private Session session;
053

054
Public EmailUtil (){
055
Properties props = new Properties ();
056
Props. put ("mail. smtp. host", SERVER); // smtp server address
057
Props. put ("mail. smtp. port", String. valueOf (PORT); // port
058
Props. put ("mail. smtp. auth", "true"); // whether the SMTP server requires user authentication. The default value is false.
059
Props. put ("mail. stmp. timeout", "2000 ");
060
Session = Session. getDefaultInstance (props, new Authenticator (){
061
// Account Verification
062
Public PasswordAuthentication getPasswordAuthentication (){
063
Return new PasswordAuthentication (USER, PASSWORD );
064
}
065
});
066
}
067

068
Public static EmailUtil getInstance (){
069
Return emailUtil;
070
}
071
Public EmailUtil setTitle (String title ){
072
This. title = title;
073
Return this;
074
}
075
Public EmailUtil setHTML (String html ){
076
This.html = html;
077
Return this;
078
}
079

080
Private Email getEmail (){
081
Return new Email ();
082
}
083

084
Public EmailUtil addEmailGiveUser (String addresseeEmail, String addresseeName ){
085
EmailEvent emailEvent = new EmailEvent ();
086
EmailEvent. setAddresseeEmail (addresseeEmail );
087
EmailEvent. setAddresseeName (addresseeName );
088
EmailEvents. add (emailEvent );
089
Return this;
090
}
091
 
092
Public void send (){
093
Email email = emailUtil. getEmail ();
094
Try {
095
Email. sendEmail ();
096
} Catch (UnsupportedEncodingException e ){
097
E. printStackTrace ();
098
} Catch (MessagingException e ){
099
E. printStackTrace ();
100
}
101
Clear ();
102
}
103

104

105
Private void clear (){
106
EmailEvents = new ArrayList <EmailEvent> ();
107
Html = null;
108
Title = null;
109
}
110

111
Public class Email {
112
Public void sendEmail () throws UnsupportedEncodingException, MessagingException {
113
MimeMessage msg = new MimeMessage (session );
114
Msg. setSentDate (new Date ());
115
Msg. setFrom (new InternetAddress (USER, FROM, ENCODING ));
116

117
// Multiple target users can be added here
118
For (EmailEvent e: emailEvents ){
119
Msg. addRecipient (Message. RecipientType. TO, new InternetAddress (e. addresseeEmail, e. addresseeName, ENCODING ));
120
}
121

122
Msg. setSubject (title, ENCODING );
123

124
// Set the mail content format to mixed content
125
MimeMultipart msgMultipart = new MimeMultipart ("mixed ");
126
MimeBodyPart content = new MimeBodyPart ();
127
// Set html content
128
Content. setContent (html, "text/html; charset = gbk ");
129
MsgMultipart. addBodyPart (content );
130
Msg. setContent (msgMultipart );
131
Transport. send (msg );
132
}
133
}
134

135
Public class EmailEvent {
136
Private String addresseeName; // recipient's name
137
Private String addresseeEmail; // recipient's email address
138
Public String getAddresseeName (){
139
Return addresseeName;
140
}
141
Public void setAddresseeName (String addresseeName ){
142
This. addresseeName = addresseeName;
143
}
144
Public String getAddresseeEmail (){
145
Return addresseeEmail;
146
}
147
Public void setAddresseeEmail (String addresseeEmail ){
148
This. addresseeEmail = addresseeEmail;
149
}
150
}
151

152
Public static void main (String args []) throws UnsupportedEncodingException {
153
String ss = "<STYLE type = \" text/css \ "> BODY {font-size: 14px; line-height: 1.5 }</STYLE> 154
String ss2 = "<STYLE type = \" text/css \ "> BODY {font-size: 14px; line-height: 1.5 }</STYLE> 155

156
// Recipient
157
EmailUtil. getInstance ()
158
. SetTitle ("This is the first sss email ")
159
. SetHTML (ss)
160
. AddEmailGiveUser ("xuxin@wokejia.com", "Xu Xin ")
161
. AddEmailGiveUser ("luoweijun@wokejia.com", "Luo Weijun ")
162
. Send ();
163

164

165
EmailUtil. getInstance ()
166
. SetTitle ("this is the second sss mail ")
167
. SetHTML (ss2)
168
. AddEmailGiveUser ("xuxin@wokejia.com", "Xu Xin ")
169
. AddEmailGiveUser ("luoweijun@wokejia.com", "Luo Weijun ")
170
. Send ();
171

172
System. out. println ("OK ");
173
}
174
}

Author: loowj

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.