Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
common
Commits
91c1da32
Commit
91c1da32
authored
May 20, 2020
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
1da43d04
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
197 additions
and
198 deletions
+197
-198
MailUtil.java
src/main/java/com/egolm/common/MailUtil.java
+197
-198
No files found.
src/main/java/com/egolm/common/MailUtil.java
View file @
91c1da32
package
com
.
egolm
.
common
;
package
com
.
egolm
.
common
;
import
java.io.File
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.Properties
;
import
javax.activation.DataHandler
;
import
javax.activation.DataHandler
;
import
javax.activation.FileDataSource
;
import
javax.activation.FileDataSource
;
import
javax.mail.Address
;
import
javax.mail.Address
;
import
javax.mail.Authenticator
;
import
javax.mail.Authenticator
;
import
javax.mail.BodyPart
;
import
javax.mail.BodyPart
;
import
javax.mail.Message
;
import
javax.mail.Message
;
import
javax.mail.Multipart
;
import
javax.mail.Multipart
;
import
javax.mail.PasswordAuthentication
;
import
javax.mail.PasswordAuthentication
;
import
javax.mail.Session
;
import
javax.mail.Session
;
import
javax.mail.Transport
;
import
javax.mail.Transport
;
import
javax.mail.internet.InternetAddress
;
import
javax.mail.internet.InternetAddress
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMultipart
;
import
javax.mail.internet.MimeMultipart
;
import
javax.mail.internet.MimeUtility
;
import
javax.mail.internet.MimeUtility
;
import
com.egolm.common.exception.PluginException
;
import
com.egolm.common.exception.PluginException
;
public
class
MailUtil
{
public
class
MailUtil
{
public
static
void
sendBySmtp
(
String
host
,
Integer
port
,
boolean
is_auth
,
final
String
username
,
final
String
password
,
final
String
from
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
public
static
void
sendBySmtp
(
String
host
,
Integer
port
,
boolean
is_auth
,
final
String
username
,
final
String
password
,
final
String
from
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
properties
.
put
(
"mail.smtp.host"
,
host
);
properties
.
put
(
"mail.smtp.host"
,
host
);
properties
.
put
(
"mail.smtp.auth"
,
is_auth
?
"true"
:
"false"
);
properties
.
put
(
"mail.smtp.auth"
,
is_auth
?
"true"
:
"false"
);
if
(
port
!=
null
)
{
if
(
port
!=
null
)
{
properties
.
put
(
"mail.smtp.port"
,
port
);
properties
.
put
(
"mail.smtp.port"
,
port
);
}
}
try
{
try
{
Session
session
=
is_auth
?
Session
.
getDefaultInstance
(
properties
,
new
Authenticator
()
{
Session
session
=
is_auth
?
Session
.
getDefaultInstance
(
properties
,
new
Authenticator
()
{
protected
PasswordAuthentication
getPasswordAuthentication
()
{
protected
PasswordAuthentication
getPasswordAuthentication
()
{
return
new
PasswordAuthentication
(
username
,
password
);
return
new
PasswordAuthentication
(
username
,
password
);
}
}
})
:
Session
.
getDefaultInstance
(
properties
);
})
:
Session
.
getDefaultInstance
(
properties
);
Message
message
=
new
MimeMessage
(
session
);
Message
message
=
new
MimeMessage
(
session
);
Address
fromAddress
=
new
InternetAddress
(
from
);
Address
fromAddress
=
new
InternetAddress
(
from
);
message
.
setFrom
(
fromAddress
);
message
.
setFrom
(
fromAddress
);
List
<
Address
>
addresses
=
new
ArrayList
<
Address
>();
List
<
Address
>
addresses
=
new
ArrayList
<
Address
>();
for
(
String
email
:
emailTo
)
{
for
(
String
email
:
emailTo
)
{
addresses
.
add
(
new
InternetAddress
(
email
));
addresses
.
add
(
new
InternetAddress
(
email
));
}
}
message
.
setRecipients
(
Message
.
RecipientType
.
TO
,
addresses
.
toArray
(
new
Address
[
addresses
.
size
()]));
message
.
setRecipients
(
Message
.
RecipientType
.
TO
,
addresses
.
toArray
(
new
Address
[
addresses
.
size
()]));
message
.
setSubject
(
subject
);
message
.
setSubject
(
subject
);
message
.
setSentDate
(
new
Date
());
message
.
setSentDate
(
new
Date
());
Multipart
m
=
new
MimeMultipart
();
Multipart
m
=
new
MimeMultipart
();
BodyPart
bp
=
new
MimeBodyPart
();
BodyPart
bp
=
new
MimeBodyPart
();
bp
.
setContent
(
content
,
"text/html; charset=gb2312"
);
bp
.
setContent
(
content
,
"text/html; charset=gb2312"
);
m
.
addBodyPart
(
bp
);
m
.
addBodyPart
(
bp
);
message
.
setContent
(
m
);
message
.
setContent
(
m
);
if
(
attachments
!=
null
)
{
if
(
attachments
!=
null
)
{
for
(
File
file
:
attachments
)
{
for
(
File
file
:
attachments
)
{
MimeBodyPart
mbpFile
=
new
MimeBodyPart
();
MimeBodyPart
mbpFile
=
new
MimeBodyPart
();
FileDataSource
fileDataSource
=
new
FileDataSource
(
file
);
FileDataSource
fileDataSource
=
new
FileDataSource
(
file
);
mbpFile
.
setDataHandler
(
new
DataHandler
(
fileDataSource
));
mbpFile
.
setDataHandler
(
new
DataHandler
(
fileDataSource
));
String
filename
=
fileDataSource
.
getName
();
String
filename
=
fileDataSource
.
getName
();
String
name
=
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
));
String
name
=
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
));
String
exname
=
filename
.
substring
(
filename
.
lastIndexOf
(
"."
));
String
exname
=
filename
.
substring
(
filename
.
lastIndexOf
(
"."
));
String
encodeName
=
MimeUtility
.
encodeText
(
name
)
+
exname
;
String
encodeName
=
MimeUtility
.
encodeText
(
name
)
+
exname
;
mbpFile
.
setFileName
(
encodeName
);
mbpFile
.
setFileName
(
encodeName
);
m
.
addBodyPart
(
mbpFile
);
m
.
addBodyPart
(
mbpFile
);
}
}
}
}
Transport
.
send
(
message
);
Transport
.
send
(
message
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
PluginException
(
e
);
throw
new
PluginException
(
e
);
}
}
}
}
public
static
void
sendBySmtps
(
String
host
,
Integer
port
,
final
String
username
,
final
String
password
,
final
String
from
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
public
static
void
sendBySmtps
(
String
host
,
Integer
port
,
final
String
username
,
final
String
password
,
final
String
from
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
sendBySmtps
(
host
,
port
,
username
,
password
,
from
,
null
,
subject
,
content
,
attachments
,
emailTo
);
sendBySmtps
(
host
,
port
,
username
,
password
,
from
,
null
,
subject
,
content
,
attachments
,
emailTo
);
/*boolean is_auth = StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password);
/*boolean is_auth = StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password);
Properties properties = new Properties();
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtps");
properties.setProperty("mail.transport.protocol", "smtps");
properties.setProperty("mail.smtp.auth", is_auth ? "true" : "false");
properties.setProperty("mail.smtp.auth", is_auth ? "true" : "false");
properties.setProperty("mail.smtp.host", String.valueOf(host));
properties.setProperty("mail.smtp.host", String.valueOf(host));
properties.setProperty("mail.smtp.port", String.valueOf(port));
properties.setProperty("mail.smtp.port", String.valueOf(port));
properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));
properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
try {
try {
Session session = is_auth ? Session.getDefaultInstance(properties, new Authenticator() {
Session session = is_auth ? Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
return new PasswordAuthentication(username, password);
}
}
}) : Session.getDefaultInstance(properties);
}) : Session.getDefaultInstance(properties);
Message message = new MimeMessage(session);
Message message = new MimeMessage(session);
Address fromAddress = new InternetAddress(from, "上海国际电影节报名 SIFF PROJECT SUBMISSION", "UTF-8");
Address fromAddress = new InternetAddress(from, "上海国际电影节报名 SIFF PROJECT SUBMISSION", "UTF-8");
message.setFrom(fromAddress);
message.setFrom(fromAddress);
List<Address> addresses = new ArrayList<Address>();
List<Address> addresses = new ArrayList<Address>();
for(String email : emailTo) {
for(String email : emailTo) {
addresses.add(new InternetAddress(email));
addresses.add(new InternetAddress(email));
}
}
message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()]));
message.setSubject(subject);
message.setSubject(subject);
message.setSentDate(new Date());
message.setSentDate(new Date());
Multipart m = new MimeMultipart();
Multipart m = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=gb2312");
bp.setContent(content, "text/html; charset=gb2312");
m.addBodyPart(bp);
m.addBodyPart(bp);
message.setContent(m);
message.setContent(m);
if(attachments != null) {
if(attachments != null) {
for(File file : attachments) {
for(File file : attachments) {
MimeBodyPart mbpFile = new MimeBodyPart();
MimeBodyPart mbpFile = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(file);
FileDataSource fileDataSource = new FileDataSource(file);
mbpFile.setDataHandler(new DataHandler(fileDataSource));
mbpFile.setDataHandler(new DataHandler(fileDataSource));
String filename = fileDataSource.getName();
String filename = fileDataSource.getName();
String name = filename.substring(0, filename.lastIndexOf("."));
String name = filename.substring(0, filename.lastIndexOf("."));
String exname = filename.substring(filename.lastIndexOf("."));
String exname = filename.substring(filename.lastIndexOf("."));
String encodeName = MimeUtility.encodeText(name) + exname;
String encodeName = MimeUtility.encodeText(name) + exname;
mbpFile.setFileName(encodeName);
mbpFile.setFileName(encodeName);
m.addBodyPart(mbpFile);
m.addBodyPart(mbpFile);
}
}
}
}
Transport.send(message);
Transport.send(message);
} catch (Exception e) {
} catch (Exception e) {
throw new PluginException(e);
throw new PluginException(e);
}*/
}*/
}
}
public
static
void
sendBySmtps
(
String
host
,
Integer
port
,
final
String
username
,
final
String
password
,
final
String
from
,
final
String
fromName
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
public
static
void
sendBySmtps
(
String
host
,
Integer
port
,
final
String
username
,
final
String
password
,
final
String
from
,
final
String
fromName
,
String
subject
,
String
content
,
File
[]
attachments
,
String
...
emailTo
)
{
System
.
out
.
println
(
fromName
);
boolean
is_auth
=
StringUtil
.
isNotBlank
(
username
)
&&
StringUtil
.
isNotBlank
(
password
);
boolean
is_auth
=
StringUtil
.
isNotBlank
(
username
)
&&
StringUtil
.
isNotBlank
(
password
);
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"mail.transport.protocol"
,
"smtps"
);
properties
.
setProperty
(
"mail.transport.protocol"
,
"smtps"
);
properties
.
setProperty
(
"mail.smtp.auth"
,
is_auth
?
"true"
:
"false"
);
properties
.
setProperty
(
"mail.smtp.auth"
,
is_auth
?
"true"
:
"false"
);
properties
.
setProperty
(
"mail.smtp.host"
,
String
.
valueOf
(
host
));
properties
.
setProperty
(
"mail.smtp.host"
,
String
.
valueOf
(
host
));
properties
.
setProperty
(
"mail.smtp.port"
,
String
.
valueOf
(
port
));
properties
.
setProperty
(
"mail.smtp.port"
,
String
.
valueOf
(
port
));
properties
.
setProperty
(
"mail.smtp.socketFactory.port"
,
String
.
valueOf
(
port
));
properties
.
setProperty
(
"mail.smtp.socketFactory.port"
,
String
.
valueOf
(
port
));
properties
.
setProperty
(
"mail.smtp.socketFactory.class"
,
"javax.net.ssl.SSLSocketFactory"
);
properties
.
setProperty
(
"mail.smtp.socketFactory.class"
,
"javax.net.ssl.SSLSocketFactory"
);
properties
.
setProperty
(
"mail.smtp.socketFactory.fallback"
,
"false"
);
properties
.
setProperty
(
"mail.smtp.socketFactory.fallback"
,
"false"
);
try
{
try
{
Session
session
=
is_auth
?
Session
.
getDefaultInstance
(
properties
,
new
Authenticator
()
{
Session
session
=
is_auth
?
Session
.
getDefaultInstance
(
properties
,
new
Authenticator
()
{
protected
PasswordAuthentication
getPasswordAuthentication
()
{
protected
PasswordAuthentication
getPasswordAuthentication
()
{
return
new
PasswordAuthentication
(
username
,
password
);
return
new
PasswordAuthentication
(
username
,
password
);
}
}
})
:
Session
.
getDefaultInstance
(
properties
);
})
:
Session
.
getDefaultInstance
(
properties
);
Message
message
=
new
MimeMessage
(
session
);
Message
message
=
new
MimeMessage
(
session
);
Address
fromAddress
;
Address
fromAddress
;
if
(
fromName
!=
null
)
{
if
(
fromName
!=
null
)
{
fromAddress
=
new
InternetAddress
(
from
,
fromName
,
"UTF-8"
);
fromAddress
=
new
InternetAddress
(
from
,
fromName
,
"UTF-8"
);
}
else
{
}
else
{
fromAddress
=
new
InternetAddress
(
from
);
fromAddress
=
new
InternetAddress
(
from
);
}
}
message
.
setFrom
(
fromAddress
);
message
.
setFrom
(
fromAddress
);
List
<
Address
>
addresses
=
new
ArrayList
<
Address
>();
List
<
Address
>
addresses
=
new
ArrayList
<
Address
>();
for
(
String
email
:
emailTo
)
{
for
(
String
email
:
emailTo
)
{
addresses
.
add
(
new
InternetAddress
(
email
));
addresses
.
add
(
new
InternetAddress
(
email
));
}
}
message
.
setRecipients
(
Message
.
RecipientType
.
TO
,
addresses
.
toArray
(
new
Address
[
addresses
.
size
()]));
message
.
setRecipients
(
Message
.
RecipientType
.
TO
,
addresses
.
toArray
(
new
Address
[
addresses
.
size
()]));
message
.
setSubject
(
subject
);
message
.
setSubject
(
subject
);
message
.
setSentDate
(
new
Date
());
message
.
setSentDate
(
new
Date
());
Multipart
m
=
new
MimeMultipart
();
Multipart
m
=
new
MimeMultipart
();
BodyPart
bp
=
new
MimeBodyPart
();
BodyPart
bp
=
new
MimeBodyPart
();
bp
.
setContent
(
content
,
"text/html; charset=gb2312"
);
bp
.
setContent
(
content
,
"text/html; charset=gb2312"
);
m
.
addBodyPart
(
bp
);
m
.
addBodyPart
(
bp
);
message
.
setContent
(
m
);
message
.
setContent
(
m
);
if
(
attachments
!=
null
)
{
if
(
attachments
!=
null
)
{
for
(
File
file
:
attachments
)
{
for
(
File
file
:
attachments
)
{
MimeBodyPart
mbpFile
=
new
MimeBodyPart
();
MimeBodyPart
mbpFile
=
new
MimeBodyPart
();
FileDataSource
fileDataSource
=
new
FileDataSource
(
file
);
FileDataSource
fileDataSource
=
new
FileDataSource
(
file
);
mbpFile
.
setDataHandler
(
new
DataHandler
(
fileDataSource
));
mbpFile
.
setDataHandler
(
new
DataHandler
(
fileDataSource
));
String
filename
=
fileDataSource
.
getName
();
String
filename
=
fileDataSource
.
getName
();
String
name
=
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
));
String
name
=
filename
.
substring
(
0
,
filename
.
lastIndexOf
(
"."
));
String
exname
=
filename
.
substring
(
filename
.
lastIndexOf
(
"."
));
String
exname
=
filename
.
substring
(
filename
.
lastIndexOf
(
"."
));
String
encodeName
=
MimeUtility
.
encodeText
(
name
)
+
exname
;
String
encodeName
=
MimeUtility
.
encodeText
(
name
)
+
exname
;
mbpFile
.
setFileName
(
encodeName
);
mbpFile
.
setFileName
(
encodeName
);
m
.
addBodyPart
(
mbpFile
);
m
.
addBodyPart
(
mbpFile
);
}
}
}
}
Transport
.
send
(
message
);
Transport
.
send
(
message
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
PluginException
(
e
);
throw
new
PluginException
(
e
);
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
MailUtil
.
sendBySmtps
(
"smtphz.qiye.163.com"
,
994
,
"filmpassword@siff.com"
,
"siffstvf2018"
,
"filmpassword@siff.com"
,
"上海国际电影节"
,
"测试"
,
"测试"
,
null
,
"13384825689@163.com"
);
MailUtil
.
sendBySmtps
(
"smtphz.qiye.163.com"
,
994
,
"filmpassword@siff.com"
,
"siffstvf2018"
,
"filmpassword@siff.com"
,
"上海国际电影节"
,
"测试"
,
"测试"
,
null
,
"13384825689@163.com"
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment