Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sendmail
Overview
Overview
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
zhiwei
sendmail
Commits
51ea2333
Commit
51ea2333
authored
Apr 23, 2020
by
zhiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加100M以下文件发送
parent
4b464581
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
195 additions
and
193 deletions
+195
-193
pom.xml
+40
-39
src/main/java/com/zhiwei/sendmail/SimpleMailSender.java
+122
-121
src/test/java/com/zhiwei/sendmail/test/SendMailTest.java
+33
-33
No files found.
pom.xml
View file @
51ea2333
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.zhiwei
</groupId>
<groupId>
com.zhiwei
</groupId>
<artifactId>
sendmail
</artifactId>
<artifactId>
sendmail
</artifactId>
<version>
0.0.
3-SNAPSHOT
</version>
<version>
0.0.
4-SNAPSHOT
</version>
<name>
sendmail
</name>
<name>
sendmail
</name>
<description>
发送邮件
</description>
<description>
发送邮件
</description>
<developers>
<developers>
<developer>
<developer>
<id>
Bewilder ZW
</id>
<id>
Bewilder ZW
</id>
<name>
zhiwei zhang
</name>
<name>
zhiwei zhang
</name>
<email>
zhangzhiwei@zhiweidata.com
</email>
<email>
zhangzhiwei@zhiweidata.com
</email>
</developer>
</developer>
</developers>
</developers>
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>
javax.mail
</groupId>
<groupId>
javax.mail
</groupId>
<artifactId>
mail
</artifactId>
<artifactId>
mail
</artifactId>
<version>
1.4.7
</version>
<version>
1.4.7
</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
<!-- 分发管理:管理distribution和supporting files -->
<!-- 分发管理:管理distribution和supporting files -->
<distributionManagement>
<distributionManagement>
<snapshotRepository>
<snapshotRepository>
<id>
nexus-releases
</id>
<id>
nexus-releases
</id>
<name>
User Porject Snapshot
</name>
<name>
User Porject Snapshot
</name>
<url>
http://192.168.0.30:8081/nexus/content/repositories/snapshots/
</url>
<url>
http://192.168.0.30:8081/nexus/content/repositories/snapshots/
</url>
<uniqueVersion>
true
</uniqueVersion>
<uniqueVersion>
true
</uniqueVersion>
</snapshotRepository>
</snapshotRepository>
<repository>
<repository>
<id>
nexus-releases
</id>
<id>
nexus-releases
</id>
<name>
User Porject Release
</name>
<name>
User Porject Release
</name>
<url>
http://192.168.0.30:8081/nexus/content/repositories/releases/
</url>
<url>
http://192.168.0.30:8081/nexus/content/repositories/releases/
</url>
</repository>
</repository>
</distributionManagement>
</distributionManagement>
</project>
</project>
\ No newline at end of file
src/main/java/com/zhiwei/sendmail/SimpleMailSender.java
View file @
51ea2333
package
com
.
zhiwei
.
sendmail
;
package
com
.
zhiwei
.
sendmail
;
import
com.zhiwei.sendmail.bean.MailInfo
;
import
com.zhiwei.sendmail.bean.MailInfo
;
import
javax.activation.DataHandler
;
import
javax.activation.DataHandler
;
import
javax.activation.FileDataSource
;
import
javax.activation.FileDataSource
;
import
javax.mail.*
;
import
javax.mail.*
;
import
javax.mail.internet.*
;
import
javax.mail.internet.*
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Properties
;
import
java.util.Properties
;
public
class
SimpleMailSender
{
public
class
SimpleMailSender
{
/**
/**
* @Title: sendTextMail
* @Title: sendTextMail
* @Description: (发送文本样式的邮件)
* @Description: (发送文本样式的邮件)
* @param @param
* @param @param
* mailInfo
* mailInfo
* @param @return
* @param @return
* 设定文件
* 设定文件
* @return boolean 返回类型
* @return boolean 返回类型
* @throws MessagingException
* @throws MessagingException
*/
*/
public
static
boolean
sendTextMail
(
MailInfo
mailInfo
)
throws
MessagingException
{
public
static
boolean
sendTextMail
(
MailInfo
mailInfo
)
throws
MessagingException
{
// 判断是否需要身份验证֤
// 判断是否需要身份验证֤
MyAuthenticator
authenticator
=
null
;
MyAuthenticator
authenticator
=
null
;
Properties
pro
=
mailInfo
.
getProperties
();
Properties
pro
=
mailInfo
.
getProperties
();
if
(
mailInfo
.
isValidate
())
{
if
(
mailInfo
.
isValidate
())
{
// 如果需要身份验证,则创建一个密码验证器
// 如果需要身份验证,则创建一个密码验证器
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUserName
(),
mailInfo
.
getPassword
());
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUserName
(),
mailInfo
.
getPassword
());
}
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session
sendMailSession
=
Session
.
getInstance
(
pro
,
authenticator
);
Session
sendMailSession
=
Session
.
getInstance
(
pro
,
authenticator
);
// 根据Session创建一个邮件消息
// 根据Session创建一个邮件消息
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
// 创建邮件发送者地址ߵ�ַ
// 创建邮件发送者地址ߵ�ַ
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
());
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
());
// 设置邮件发送者
// 设置邮件发送者
mailMessage
.
setFrom
(
from
);
mailMessage
.
setFrom
(
from
);
// 创建邮件的接收者地址,并设置到邮件消息中
// 创建邮件的接收者地址,并设置到邮件消息中
Address
to
=
new
InternetAddress
(
mailInfo
.
getToAddress
());
Address
to
=
new
InternetAddress
(
mailInfo
.
getToAddress
());
mailMessage
.
setRecipient
(
Message
.
RecipientType
.
TO
,
to
);
mailMessage
.
setRecipient
(
Message
.
RecipientType
.
TO
,
to
);
// 设置邮件消息主题
// 设置邮件消息主题
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
// 设置邮件消息发送的时间
// 设置邮件消息发送的时间
mailMessage
.
setSentDate
(
new
Date
());
mailMessage
.
setSentDate
(
new
Date
());
// 设置邮件消息发送的时间
// 设置邮件消息发送的时间
String
mailContent
=
mailInfo
.
getContent
();
String
mailContent
=
mailInfo
.
getContent
();
mailMessage
.
setText
(
mailContent
);
mailMessage
.
setText
(
mailContent
);
// 发送邮件
// 发送邮件
Transport
.
send
(
mailMessage
);
Transport
.
send
(
mailMessage
);
return
true
;
return
true
;
}
}
/**
/**
* @Title: sendHtmlMail
* @Title: sendHtmlMail
* @Description: (发送网页形式的邮件)
* @Description: (发送网页形式的邮件)
* @param @param
* @param @param
* mailInfo
* mailInfo
* @param @return
* @param @return
* 设定文件
* 设定文件
* @return boolean 返回类型
* @return boolean 返回类型
* @throws MessagingException
* @throws MessagingException
* @throws UnsupportedEncodingException
* @throws UnsupportedEncodingException
*/
*/
public
static
boolean
sendHtmlMail
(
MailInfo
mailInfo
)
throws
UnsupportedEncodingException
,
MessagingException
{
public
static
boolean
sendHtmlMail
(
MailInfo
mailInfo
)
throws
UnsupportedEncodingException
,
MessagingException
{
// 判断是否需要身份认证
// 判断是否需要身份认证
MyAuthenticator
authenticator
=
null
;
MyAuthenticator
authenticator
=
null
;
Properties
pro
=
mailInfo
.
getProperties
();
Properties
pro
=
mailInfo
.
getProperties
();
// 如果需要身份认证,则创建一个密码验证器
// 如果需要身份认证,则创建一个密码验证器
if
(
mailInfo
.
isValidate
())
{
if
(
mailInfo
.
isValidate
())
{
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUserName
(),
mailInfo
.
getPassword
());
authenticator
=
new
MyAuthenticator
(
mailInfo
.
getUserName
(),
mailInfo
.
getPassword
());
}
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session
sendMailSession
=
Session
.
getDefaultInstance
(
pro
,
authenticator
);
Session
sendMailSession
=
Session
.
getDefaultInstance
(
pro
,
authenticator
);
// 根据session创建一个邮件消息
// 根据session创建一个邮件消息
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
// 创建邮件发送者地址
// 创建邮件发送者地址
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
(),
mailInfo
.
getFromName
());
Address
from
=
new
InternetAddress
(
mailInfo
.
getFromAddress
(),
mailInfo
.
getFromName
());
// 设置邮件消息的发送者
// 设置邮件消息的发送者
mailMessage
.
setFrom
(
from
);
mailMessage
.
setFrom
(
from
);
// 创建邮件的接收者地址,并设置到邮件消息中
// 创建邮件的接收者地址,并设置到邮件消息中
// Message.RecipientType.TO属性表示接收者的类型为TO
// Message.RecipientType.TO属性表示接收者的类型为TO
InternetAddress
[]
to
=
InternetAddress
.
parse
(
mailInfo
.
getToAddress
());
InternetAddress
[]
to
=
InternetAddress
.
parse
(
mailInfo
.
getToAddress
());
mailMessage
.
setRecipients
(
Message
.
RecipientType
.
TO
,
to
);
mailMessage
.
setRecipients
(
Message
.
RecipientType
.
TO
,
to
);
// 抄送地址
// 抄送地址
if
(
mailInfo
.
getCopyAddress
()!=
null
)
{
if
(
mailInfo
.
getCopyAddress
()!=
null
)
{
InternetAddress
[]
cc
=
InternetAddress
.
parse
(
mailInfo
.
getCopyAddress
());
InternetAddress
[]
cc
=
InternetAddress
.
parse
(
mailInfo
.
getCopyAddress
());
mailMessage
.
setRecipients
(
Message
.
RecipientType
.
CC
,
cc
);
mailMessage
.
setRecipients
(
Message
.
RecipientType
.
CC
,
cc
);
}
}
// 设置邮件消息的主题
// 设置邮件消息的主题
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
mailMessage
.
setSubject
(
mailInfo
.
getSubject
());
// 设置邮件消息发送的时间
// 设置邮件消息发送的时间
mailMessage
.
setSentDate
(
new
Date
());
mailMessage
.
setSentDate
(
new
Date
());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart
multiPart
=
new
MimeMultipart
();
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
// 创建一个包含HTML内容的MimeBodyPart
Multipart
multiPart
=
new
MimeMultipart
();
BodyPart
bodyPart
=
new
MimeBodyPart
();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart
bodyPart
=
new
MimeBodyPart
();
// 设置HTML内容
// 设置HTML内容
bodyPart
.
setContent
(
mailInfo
.
getContent
(),
"text/html; charset=utf-8"
);
bodyPart
.
setContent
(
mailInfo
.
getContent
(),
"text/html; charset=utf-8"
);
multiPart
.
addBodyPart
(
bodyPart
);
multiPart
.
addBodyPart
(
bodyPart
);
// 将MiniMultipart对象设置为邮件内容
// 将MiniMultipart对象设置为邮件内容
mailMessage
.
setContent
(
multiPart
);
mailMessage
.
setContent
(
multiPart
);
if
(
null
!=
mailInfo
.
getAttachFileNames
()
&&
mailInfo
.
getAttachFileNames
().
size
()
!=
0
)
{
if
(
null
!=
mailInfo
.
getAttachFileNames
()
&&
mailInfo
.
getAttachFileNames
().
size
()
!=
0
)
{
for
(
String
attachFile
:
mailInfo
.
getAttachFileNames
())
{
for
(
String
attachFile
:
mailInfo
.
getAttachFileNames
())
{
bodyPart
=
new
MimeBodyPart
();
bodyPart
=
new
MimeBodyPart
();
FileDataSource
fds
=
new
FileDataSource
(
attachFile
);
// 得到数据源
FileDataSource
fds
=
new
FileDataSource
(
attachFile
);
// 得到数据源
bodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
// 得到附件本身并放入BodyPart
bodyPart
.
setDataHandler
(
new
DataHandler
(
fds
));
// 得到附件本身并放入BodyPart
bodyPart
.
setFileName
(
MimeUtility
.
encodeText
(
fds
.
getName
()));
// 得到文件名并编码(防止中文文件名乱码)同样放入BodyPart
bodyPart
.
setFileName
(
MimeUtility
.
encodeText
(
fds
.
getName
()));
// 得到文件名并编码(防止中文文件名乱码)同样放入BodyPart
multiPart
.
addBodyPart
(
bodyPart
);
multiPart
.
addBodyPart
(
bodyPart
);
}
}
}
}
// 发送邮件
// 发送邮件
Transport
.
send
(
mailMessage
);
mailMessage
.
saveChanges
();
return
true
;
Transport
.
send
(
mailMessage
,
mailMessage
.
getAllRecipients
());
return
true
;
}
}
}
}
src/test/java/com/zhiwei/sendmail/test/SendMailTest.java
View file @
51ea2333
package
com
.
zhiwei
.
sendmail
.
test
;
package
com
.
zhiwei
.
sendmail
.
test
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
com.zhiwei.sendmail.SendMail
;
import
com.zhiwei.sendmail.SendMail
;
import
com.zhiwei.sendmail.bean.MailInfo
;
import
com.zhiwei.sendmail.bean.MailInfo
;
public
class
SendMailTest
{
public
class
SendMailTest
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// 这个类主要是设置邮件
// 这个类主要是设置邮件
String
mailServerHost
=
"smtp
.ym.163.com"
;
String
mailServerHost
=
"smtp
hz.qiye.163.com"
;
String
mailServerPort
=
"25"
;
String
mailServerPort
=
"25"
;
String
fromAddress
=
"zhangzhiwei@zhiweidata.com"
;
String
fromAddress
=
"zhangzhiwei@zhiweidata.com"
;
String
fromName
=
"测试"
;
String
fromName
=
"测试"
;
String
toAddress
=
"
859548429@qq.com"
;
String
toAddress
=
"
yuchengyi@zhiweidata.com"
;
String
userName
=
"zhangzhiwei@zhiweidata.com"
;
String
userName
=
"zhangzhiwei@zhiweidata.com"
;
String
password
=
"olp1437z.."
;
String
password
=
"olp1437z.."
;
boolean
validate
=
true
;
boolean
validate
=
true
;
String
subject
=
"带附件的邮件测试程序"
;
String
subject
=
"带附件的邮件测试程序"
;
String
content
=
"发送邮件测试"
;
String
content
=
"发送邮件测试"
;
String
copyAddress
=
"
825290417@qq.com,chenweiyang@zhiweidata.com"
;
String
copyAddress
=
"
chenweiyang@zhiweidata.com"
;
List
<
String
>
attachFileNames
=
new
ArrayList
<
String
>();
List
<
String
>
attachFileNames
=
new
ArrayList
<
String
>();
attachFileNames
.
add
(
"
E://数据//2017年数据//9月份数据//南京组-美团日报数据//南京组-美团日报数据2017-09-28-微博.xlsx"
);
attachFileNames
.
add
(
"
C:\\Users\\admin\\Desktop\\质检数据-大词-2020-04-13.xlsx"
);
MailInfo
mailInfo
=
new
MailInfo
(
mailServerHost
,
mailServerPort
,
fromAddress
,
fromName
,
toAddress
,
userName
,
password
,
MailInfo
mailInfo
=
new
MailInfo
(
mailServerHost
,
mailServerPort
,
fromAddress
,
fromName
,
toAddress
,
userName
,
password
,
validate
,
subject
,
content
,
copyAddress
,
attachFileNames
);
validate
,
subject
,
content
,
copyAddress
,
attachFileNames
);
boolean
f
=
false
;
boolean
f
=
false
;
// 这个类主要来发送邮件
// 这个类主要来发送邮件
f
=
SendMail
.
sendMailByHtml
(
mailInfo
);
// 发送文体格式
f
=
SendMail
.
sendMailByHtml
(
mailInfo
);
// 发送文体格式
System
.
out
.
println
(
"f==========="
+
f
);
System
.
out
.
println
(
"f==========="
+
f
);
}
}
}
}
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