Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jgjy
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
注册表
注册表
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
svn
jgjy
Commits
76f94742
提交
76f94742
authored
5月 06, 2019
作者:
lvwei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
上级
5fb80b3e
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
478 行增加
和
134 行删除
+478
-134
EmailController.java
...d/src/main/java/com/zrqx/third/email/EmailController.java
+168
-75
EmailTest.java
...x.third/src/main/java/com/zrqx/third/email/EmailTest.java
+51
-0
RandomUtil.java
....third/src/main/java/com/zrqx/third/email/RandomUtil.java
+0
-47
EmailConfig.java
...rc/main/java/com/zrqx/third/email/config/EmailConfig.java
+0
-12
MailInfo.java
...d/src/main/java/com/zrqx/third/email/config/MailInfo.java
+110
-0
MyAuthenticator.java
...ain/java/com/zrqx/third/email/config/MyAuthenticator.java
+22
-0
SimpleMail.java
...src/main/java/com/zrqx/third/email/config/SimpleMail.java
+127
-0
没有找到文件。
com.zrqx.third/src/main/java/com/zrqx/third/email/EmailController.java
浏览文件 @
76f94742
/**
* @Title: EmailSend.java
* @Package cn.com.hxedu.commons.email
* @Description: TODO(用一句话描述该文件做什么)
* @author 费久凤
* @date 2014-11-20 下午2:28:11
* @version V5.0
*/
package
com
.
zrqx
.
third
.
email
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiParam
;
import
java.util.concurrent.TimeUnit
;
import
java.io.IOException
;
import
java.util.Map
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.
data.redis.core.StringRedisTemplat
e
;
import
org.springframework.
beans.factory.annotation.Valu
e
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.aliyuncs.DefaultAcsClient
;
import
com.aliyuncs.IAcsClient
;
import
com.aliyuncs.dm.model.v20151123.SingleSendMailRequest
;
import
com.aliyuncs.dm.model.v20151123.SingleSendMailResponse
;
import
com.aliyuncs.exceptions.ClientException
;
import
com.aliyuncs.exceptions.ServerException
;
import
com.aliyuncs.profile.DefaultProfile
;
import
com.aliyuncs.profile.IClientProfile
;
import
com.zrqx.core.check.CheckCodeUtils
;
import
com.zrqx.core.commons.redis.Redis
;
import
com.zrqx.core.constant.third.ThirdRequestPath
;
import
com.zrqx.core.form.third.email.EmailForm
;
import
com.zrqx.third.email.config.EmailConfig
;
import
com.zrqx.core.exception.BusinessValidateException
;
import
com.zrqx.core.util.datatype.UUIDUtil
;
import
com.zrqx.third.email.config.MailInfo
;
import
com.zrqx.third.email.config.SimpleMail
;
/**
* @ClassName: EmailSend
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 费久凤
* @date 2014-11-20 下午2:28:11
*
*/
@RestController
@RequestMapping
(
ThirdRequestPath
.
EMAIL
)
@Api
(
description
=
"邮件"
)
@RequestMapping
(
ThirdRequestPath
.
SDKSMS
)
@Api
(
description
=
"
发送
邮件"
)
public
class
EmailController
{
@Autowired
StringRedisTemplate
stringRedisTemplate
;
Logger
logger
=
LoggerFactory
.
getLogger
(
EmailController
.
class
);
// 服务器ip
@Value
(
"${email.EMAIL_HOST}"
)
String
EMAIL_HOST
;
// 端口
@Value
(
"${email.EMAIL_PORT}"
)
String
EMAIL_PORT
;
// 账号
@Value
(
"${email.EMAIL_ACCOUNT}"
)
String
EMAIL_ACCOUNT
;
// 密码
@Value
(
"${email.EMAIL_PASSWORD}"
)
String
EMAIL_PASSWORD
;
@ApiOperation
(
value
=
"邮件验证"
,
notes
=
"邮件验证"
)
@PostMapping
(
value
=
ThirdRequestPath
.
VERIFICATION
)
public
String
sample
(
@ApiParam
(
value
=
"邮箱号"
)
String
email
)
{
// 如果是除杭州region外的其它region(如新加坡、澳洲Region),需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"。
IClientProfile
profile
=
DefaultProfile
.
getProfile
(
"cn-hangzhou"
,
EmailConfig
.
accessKey
,
EmailConfig
.
accessSecret
);
IAcsClient
client
=
new
DefaultAcsClient
(
profile
);
SingleSendMailRequest
request
=
new
SingleSendMailRequest
();
@Autowired
Redis
redis
;
/**
* 群发 key:收件人地址 value:内容
* @param map
* @return
* @author lw
* @date: 2019年5月6日 上午9:15:25
*/
@ApiOperation
(
value
=
"群发邮件"
)
@PostMapping
(
ThirdRequestPath
.
BATCH
+
ThirdRequestPath
.
SEND
)
public
String
sendEmail
(
Map
<
String
,
String
>
map
){
try
{
// request.setVersion("2017-06-22");//
// 如果是除杭州region外的其它region(如新加坡region),必须指定为2017-06-22
request
.
setAccountName
(
EmailConfig
.
AccountName
);
request
.
setFromAlias
(
"小红"
+
"<"
+
email
+
">"
);
request
.
setAddressType
(
1
);
request
.
setTagName
(
EmailConfig
.
TagName
);
request
.
setReplyToAddress
(
true
);
request
.
setToAddress
(
email
);
request
.
setSubject
(
"邮件验证"
);
// 生成验证码
String
code
=
RandomUtil
.
suijishu
(
5
);
// 将验证码保存到session中
stringRedisTemplate
.
opsForValue
().
set
(
code
,
"100"
,
60
*
10
,
TimeUnit
.
SECONDS
);
request
.
setHtmlBody
(
"您收到这封邮件,是由于新用户注册或用户修改E-mail使用了这个邮箱地址。如果您没有访问过我们的平台或没有进行上述操作,请忽略这封邮件。您不需要退订或进行其他操作。<br/>"
+
"您的验证码为<br/>"
+
code
+
"<br/>"
);
SingleSendMailResponse
httpResponse
=
client
.
getAcsResponse
(
request
);
}
catch
(
ServerException
e
)
{
e
.
printStackTrace
();
}
catch
(
ClientException
e
)
{
e
.
printStackTrace
();
if
(
map
!=
null
&&
map
.
size
()>
0
){
for
(
String
key
:
map
.
keySet
())
{
MailInfo
mailInfo
=
new
MailInfo
();
mailInfo
.
setMailServerHost
(
EMAIL_HOST
);
mailInfo
.
setMailServerPort
(
EMAIL_PORT
);
mailInfo
.
setValidate
(
true
);
mailInfo
.
setUsername
(
EMAIL_ACCOUNT
);
mailInfo
.
setPassword
(
EMAIL_PASSWORD
);
// 您的邮箱密码 myloveru1314521
mailInfo
.
setFromAddress
(
EMAIL_ACCOUNT
);
mailInfo
.
setToAddress
(
key
);
/*mailInfo.setSubject(title); */
/*StringBuffer content = new StringBuffer();
content.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">")
.append("<html>")
.append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">")
.append("<title>")
.append(title)
.append("</title>")
.append("<style type=\"text/css\">")
.append(".test{font-family:\"Microsoft Yahei\";font-size: 18px;color: red;}")
.append("</style>")
.append("</head>")
.append("<body>")
.append("<img src='http://img0.imgtn.bdimg.com/it/u=2535477532,1479756398&fm=21&gp=0.jpg' />")
.append("<span class=\"test\">")
.append( map.get(key))
.append("</span>")
.append("</body>")
.append("</html>"); */
mailInfo
.
setContent
(
map
.
get
(
key
));
try
{
boolean
flag
=
SimpleMail
.
sendHtmlMail
(
mailInfo
);
if
(
flag
){
System
.
out
.
println
(
"邮件发送成功!!"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
"0"
;
}
}
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"邮件群发失败:"
+
e
.
getMessage
());
return
"0"
;
}
logger
.
info
(
"邮件群发成功"
);
return
"1"
;
}
@ApiOperation
(
value
=
"邮件(自己发验证码)"
,
notes
=
"邮件(自己发验证码)"
)
@PostMapping
(
value
=
ThirdRequestPath
.
SEND
)
public
String
sendEmail
(
EmailForm
emailForm
)
{
// 如果是除杭州region外的其它region(如新加坡、澳洲Region),需要将下面的"cn-hangzhou"替换为"ap-southeast-1"、或"ap-southeast-2"。
IClientProfile
profile
=
DefaultProfile
.
getProfile
(
"cn-hangzhou"
,
EmailConfig
.
accessKey
,
EmailConfig
.
accessSecret
);
IAcsClient
client
=
new
DefaultAcsClient
(
profile
);
SingleSendMailRequest
request
=
new
SingleSendMailRequest
();
try
{
// request.setVersion("2017-06-22");//
// 如果是除杭州region外的其它region(如新加坡region),必须指定为2017-06-22
request
.
setAccountName
(
EmailConfig
.
AccountName
);
request
.
setFromAlias
(
"小红"
+
"<"
+
emailForm
.
getEmail
()
+
">"
);
request
.
setAddressType
(
1
);
request
.
setTagName
(
EmailConfig
.
TagName
);
request
.
setReplyToAddress
(
true
);
request
.
setToAddress
(
emailForm
.
getEmail
());
request
.
setSubject
(
"邮件验证"
);
request
.
setHtmlBody
(
"您收到这封邮件,是由于新用户注册或用户修改E-mail使用了这个邮箱地址。如果您没有访问过我们的平台或没有进行上述操作,请忽略这封邮件。您不需要退订或进行其他操作。<br/>"
+
"您的验证码为<br/>"
+
emailForm
.
getCode
()
+
"<br/>"
);
SingleSendMailResponse
httpResponse
=
client
.
getAcsResponse
(
request
);
}
catch
(
ServerException
e
)
{
e
.
printStackTrace
();
}
catch
(
ClientException
e
)
{
e
.
printStackTrace
();
/**
* 邮箱验证
* @param toAddress
* @return 验证码key
* @author lw
* @date: 2019年5月6日 上午9:16:49
*/
@ApiOperation
(
value
=
"发邮件验证码"
)
@PostMapping
(
ThirdRequestPath
.
SEND
+
ThirdRequestPath
.
CHECK
)
public
String
emailCheck
(
String
toAddress
){
MailInfo
mailInfo
=
new
MailInfo
();
mailInfo
.
setMailServerHost
(
EMAIL_HOST
);
mailInfo
.
setMailServerPort
(
EMAIL_PORT
);
mailInfo
.
setValidate
(
true
);
mailInfo
.
setUsername
(
EMAIL_ACCOUNT
);
mailInfo
.
setPassword
(
EMAIL_PASSWORD
);
// 您的邮箱密码 myloveru1314521
mailInfo
.
setFromAddress
(
EMAIL_ACCOUNT
);
mailInfo
.
setToAddress
(
toAddress
);
/*mailInfo.setSubject(title); */
/*StringBuffer content = new StringBuffer();
content.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">")
.append("<html>")
.append("<head>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">")
.append("<title>")
.append(title)
.append("</title>")
.append("<style type=\"text/css\">")
.append(".test{font-family:\"Microsoft Yahei\";font-size: 18px;color: red;}")
.append("</style>")
.append("</head>")
.append("<body>")
.append("<img src='http://img0.imgtn.bdimg.com/it/u=2535477532,1479756398&fm=21&gp=0.jpg' />")
.append("<span class=\"test\">")
.append( map.get(key))
.append("</span>")
.append("</body>")
.append("</html>"); */
String
code
=
CheckCodeUtils
.
getCheckCode
(
4
);
String
id
=
UUIDUtil
.
getUUID
();
try
{
redis
.
set
(
id
,
code
,
60
*
5
);
}
catch
(
IOException
e
)
{
logger
.
error
(
toAddress
+
"、邮件验证码生成失败:"
+
e
.
getMessage
());
throw
new
BusinessValidateException
(
"邮件验证码生成失败:"
+
e
.
getMessage
());
}
return
"1"
;
mailInfo
.
setContent
(
code
);
boolean
flag
=
false
;
try
{
flag
=
SimpleMail
.
sendHtmlMail
(
mailInfo
);
}
catch
(
Exception
e
)
{
logger
.
error
(
toAddress
+
"、邮件验证码发送失败:"
+
e
.
getMessage
());
throw
new
BusinessValidateException
(
"邮件验证码发送失败:"
+
e
.
getMessage
());
}
if
(
flag
){
logger
.
info
(
toAddress
+
"、邮件验证码发送成功"
);
}
return
id
;
}
}
com.zrqx.third/src/main/java/com/zrqx/third/email/EmailTest.java
0 → 100644
浏览文件 @
76f94742
package
com
.
zrqx
.
third
.
email
;
import
com.zrqx.third.email.config.MailInfo
;
import
com.zrqx.third.email.config.SimpleMail
;
public
class
EmailTest
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"1212"
);
MailInfo
mailInfo
=
new
MailInfo
();
mailInfo
.
setMailServerHost
(
"smtp.exmail.qq.com"
);
mailInfo
.
setMailServerPort
(
"25"
);
//25 465 587
mailInfo
.
setValidate
(
true
);
mailInfo
.
setUsername
(
"shi_tengfei@worldaffairs.cn"
);
mailInfo
.
setPassword
(
"stf19900801"
);
// 您的邮箱密码
mailInfo
.
setFromAddress
(
"shi_tengfei@worldaffairs.cn"
);
mailInfo
.
setToAddress
(
"122995870@qq.com"
);
mailInfo
.
setSubject
(
"abcd"
);
mailInfo
.
setValidate
(
true
);
//附件
/*String[] attachFileNames={"d:/Sunset.jpg"};
mailInfo.setAttachFileNames(attachFileNames); */
// 这个类主要来发送邮件
//mailInfo.setContent("设置邮箱内容");
//SimpleMail.sendTextMail(mailInfo);// 发送文体格式
StringBuffer
demo
=
new
StringBuffer
();
demo
.
append
(
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
)
.
append
(
"<html>"
)
.
append
(
"<head>"
)
.
append
(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
)
.
append
(
"<title>测试邮件</title>"
)
.
append
(
"<style type=\"text/css\">"
)
.
append
(
".test{font-family:\"Microsoft Yahei\";font-size: 18px;color: red;}"
)
.
append
(
"</style>"
)
.
append
(
"</head>"
)
.
append
(
"<body>"
)
.
append
(
"<img src='http://img0.imgtn.bdimg.com/it/u=2535477532,1479756398&fm=21&gp=0.jpg' />"
)
.
append
(
"<span class=\"test\">大家好,这里是测试Demo</span>"
)
.
append
(
"</body>"
)
.
append
(
"</html>"
);
mailInfo
.
setContent
(
demo
.
toString
());
// SimpleMail.sendHtmlMail(mailInfo);// 发送html格式
SimpleMail
.
sendTextMail
(
mailInfo
);
// 发送html格式
System
.
out
.
println
(
"邮件发送成功!!"
);
}
}
\ No newline at end of file
com.zrqx.third/src/main/java/com/zrqx/third/email/RandomUtil.java
deleted
100644 → 0
浏览文件 @
5fb80b3e
/**
* @Title: SessionTools.java
* @Package com.book.util
* @Description: TODO(用一句话描述该文件做什么)
* @author 段思铭
* @date 2015-6-17 上午11:09:22
* @version V5.0
*/
package
com
.
zrqx
.
third
.
email
;
import
java.util.Random
;
import
java.util.concurrent.ThreadLocalRandom
;
import
java.util.concurrent.ThreadLocalRandom
;
/**
*
* @ClassName: RandomUtil
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 段思铭
* @date 2015-6-17 下午1:55:14
*
*/
public
class
RandomUtil
{
/**
*
* @Title: suijima
* @Description: TODO(生成数字随机数)
* @param num
* @return
* @return String 返回类型
* @author 段思铭
* @date 2014-9-18 下午3:30:06
*/
public
static
String
suijishu
(
int
num
)
{
char
[]
str
=
"0123456789"
.
toCharArray
();
Random
rd
=
new
Random
();
String
suiji
=
""
;
int
temp
=
0
;
for
(
int
i
=
0
;
i
<
num
;
i
++)
{
temp
=
rd
.
nextInt
(
10
);
suiji
+=
str
[
temp
];
}
return
suiji
;
}
}
com.zrqx.third/src/main/java/com/zrqx/third/email/config/EmailConfig.java
deleted
100644 → 0
浏览文件 @
5fb80b3e
package
com
.
zrqx
.
third
.
email
.
config
;
public
class
EmailConfig
{
public
static
String
accessKey
=
""
;
public
static
String
accessSecret
=
""
;
//管理控制台中配置的发信地址
public
static
String
AccountName
=
""
;
//控制台创建的标签
public
static
String
TagName
=
""
;
}
com.zrqx.third/src/main/java/com/zrqx/third/email/config/MailInfo.java
0 → 100644
浏览文件 @
76f94742
package
com
.
zrqx
.
third
.
email
.
config
;
import
java.util.Properties
;
import
org.apache.commons.lang.StringUtils
;
public
class
MailInfo
{
private
String
mailServerHost
;
// 服务器ip
private
String
mailServerPort
;
// 端口
private
String
fromAddress
;
// 发送者的邮件地址
private
String
toAddress
;
// 邮件接收者地址
private
String
username
;
// 登录邮件发送服务器的用户名
private
String
password
;
// 登录邮件发送服务器的密码
private
boolean
validate
=
false
;
// 是否需要身份验证
private
String
subject
;
// 邮件主题
private
String
content
;
// 邮件内容
private
String
[]
attachFileNames
;
// 附件名称
public
Properties
getProperties
()
{
Properties
p
=
new
Properties
();
p
.
put
(
"mail.smtp.host"
,
this
.
mailServerHost
);
if
(
StringUtils
.
isNotEmpty
(
this
.
mailServerPort
))
p
.
put
(
"mail.smtp.port"
,
this
.
mailServerPort
);
p
.
put
(
"mail.smtp.auth"
,
validate
?
"true"
:
"false"
);
p
.
put
(
"mail.transport.protocol"
,
"smtp"
);
return
p
;
}
public
String
getMailServerHost
()
{
return
mailServerHost
;
}
public
void
setMailServerHost
(
String
mailServerHost
)
{
this
.
mailServerHost
=
mailServerHost
;
}
public
String
getMailServerPort
()
{
return
mailServerPort
;
}
public
void
setMailServerPort
(
String
mailServerPort
)
{
this
.
mailServerPort
=
mailServerPort
;
}
public
String
getFromAddress
()
{
return
fromAddress
;
}
public
void
setFromAddress
(
String
fromAddress
)
{
this
.
fromAddress
=
fromAddress
;
}
public
String
getToAddress
()
{
return
toAddress
;
}
public
void
setToAddress
(
String
toAddress
)
{
this
.
toAddress
=
toAddress
;
}
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
boolean
isValidate
()
{
return
validate
;
}
public
void
setValidate
(
boolean
validate
)
{
this
.
validate
=
validate
;
}
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
[]
getAttachFileNames
()
{
return
attachFileNames
;
}
public
void
setAttachFileNames
(
String
[]
attachFileNames
)
{
this
.
attachFileNames
=
attachFileNames
;
}
}
\ No newline at end of file
com.zrqx.third/src/main/java/com/zrqx/third/email/config/MyAuthenticator.java
0 → 100644
浏览文件 @
76f94742
package
com
.
zrqx
.
third
.
email
.
config
;
import
javax.mail.Authenticator
;
import
javax.mail.PasswordAuthentication
;
public
class
MyAuthenticator
extends
Authenticator
{
private
String
username
=
null
;
private
String
password
=
null
;
public
MyAuthenticator
()
{
};
public
MyAuthenticator
(
String
username
,
String
password
)
{
this
.
username
=
username
;
this
.
password
=
password
;
}
protected
PasswordAuthentication
getPasswordAuthentication
()
{
return
new
PasswordAuthentication
(
username
,
password
);
}
}
\ No newline at end of file
com.zrqx.third/src/main/java/com/zrqx/third/email/config/SimpleMail.java
0 → 100644
浏览文件 @
76f94742
package
com
.
zrqx
.
third
.
email
.
config
;
import
java.io.File
;
import
java.util.Date
;
import
java.util.Properties
;
import
javax.activation.DataHandler
;
import
javax.activation.FileDataSource
;
import
javax.mail.Address
;
import
javax.mail.Message
;
import
javax.mail.Multipart
;
import
javax.mail.Session
;
import
javax.mail.Transport
;
import
javax.mail.internet.InternetAddress
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMultipart
;
public
class
SimpleMail
{
// 以文本格式发送邮件
public
static
boolean
sendTextMail
(
MailInfo
mailInfo
)
{
//判断是否需要身份认证
MyAuthenticator
authenticator
=
null
;
Properties
properties
=
mailInfo
.
getProperties
();
if
(
mailInfo
.
isValidate
())
{
//如果需要身份认证,则创建一个密码验证器
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUsername
(),
mailInfo
.
getPassword
());
}
//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session
sendMailSession
=
Session
.
getDefaultInstance
(
properties
,
authenticator
);
try
{
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
//根据session创建一个邮件消息
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
());
//创建邮件发送者地址
mailMessage
.
setFrom
(
from
);
//设置邮件消息的发送者
Address
to
=
new
InternetAddress
(
mailInfo
.
getToAddress
());
//创建邮件的接收者地址
mailMessage
.
setRecipient
(
Message
.
RecipientType
.
TO
,
to
);
//设置邮件消息的接收者
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
//设置邮件消息的主题
mailMessage
.
setSentDate
(
new
Date
());
//设置邮件消息发送的时间
//mailMessage.setText(mailInfo.getContent());//设置邮件消息的主要内容
//MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart
mainPart
=
new
MimeMultipart
();
MimeBodyPart
messageBodyPart
=
new
MimeBodyPart
();
//创建一个包含附件内容的MimeBodyPart
//设置HTML内容
messageBodyPart
.
setContent
(
mailInfo
.
getContent
(),
"text/html; charset=utf-8"
);
mainPart
.
addBodyPart
(
messageBodyPart
);
//存在附件
String
[]
filePaths
=
mailInfo
.
getAttachFileNames
();
if
(
filePaths
!=
null
&&
filePaths
.
length
>
0
)
{
for
(
String
filePath:
filePaths
){
messageBodyPart
=
new
MimeBodyPart
();
File
file
=
new
File
(
filePath
);
if
(
file
.
exists
()){
//附件存在磁盘中
FileDataSource
fds
=
new
FileDataSource
(
file
);
//得到数据源
messageBodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
//得到附件本身并至入BodyPart
messageBodyPart
.
setFileName
(
file
.
getName
());
//得到文件名同样至入BodyPart
mainPart
.
addBodyPart
(
messageBodyPart
);
}
}
}
//将MimeMultipart对象设置为邮件内容
mailMessage
.
setContent
(
mainPart
);
Transport
.
send
(
mailMessage
);
//发送邮件
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
// 以HTML格式发送邮件
public
static
boolean
sendHtmlMail
(
MailInfo
mailInfo
)
{
//判断是否需要身份认证
MyAuthenticator
authenticator
=
null
;
Properties
properties
=
mailInfo
.
getProperties
();
if
(
mailInfo
.
isValidate
())
{
// 如果需要身份认证,则创建一个密码验证器
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUsername
(),
mailInfo
.
getPassword
());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session
sendMailSession
=
Session
.
getDefaultInstance
(
properties
,
authenticator
);
try
{
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
//根据session创建一个邮件消息
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
());
//创建邮件发送者地址
mailMessage
.
setFrom
(
from
);
//设置邮件消息的发送者
Address
to
=
new
InternetAddress
(
mailInfo
.
getToAddress
());
//创建邮件的接收者地址
mailMessage
.
setRecipient
(
Message
.
RecipientType
.
TO
,
to
);
//设置邮件消息的接收者
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
//设置邮件消息的主题
mailMessage
.
setSentDate
(
new
Date
());
//设置邮件消息发送的时间
//MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart
mainPart
=
new
MimeMultipart
();
MimeBodyPart
messageBodyPart
=
new
MimeBodyPart
();
//创建一个包含HTML内容的MimeBodyPart
//设置HTML内容
messageBodyPart
.
setContent
(
mailInfo
.
getContent
(),
"text/html; charset=utf-8"
);
mainPart
.
addBodyPart
(
messageBodyPart
);
//存在附件
String
[]
filePaths
=
mailInfo
.
getAttachFileNames
();
if
(
filePaths
!=
null
&&
filePaths
.
length
>
0
)
{
for
(
String
filePath:
filePaths
){
messageBodyPart
=
new
MimeBodyPart
();
File
file
=
new
File
(
filePath
);
if
(
file
.
exists
()){
//附件存在磁盘中
FileDataSource
fds
=
new
FileDataSource
(
file
);
//得到数据源
messageBodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
//得到附件本身并至入BodyPart
messageBodyPart
.
setFileName
(
file
.
getName
());
//得到文件名同样至入BodyPart
mainPart
.
addBodyPart
(
messageBodyPart
);
}
}
}
//将MimeMultipart对象设置为邮件内容
mailMessage
.
setContent
(
mainPart
);
Transport
.
send
(
mailMessage
);
//发送邮件
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论