package
util;
import
java.io.InputStream;
import
java.util.Properties;
import
javax.activation.DataHandler;
import
javax.activation.FileDataSource;
import
javax.mail.BodyPart;
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;
/***
* Send edm to mail
* Jerry.li
* 2012-05-02
*/
public
class
EdmMail {
private
static
String MAIL_TITLE =
null
;
private
String mailServerHost =
""
;
private
String fromAddress =
""
;
private
String toAddress =
""
;
private
MimeMessage mimemsg;
private
Session session;
private
Properties properties;
private
Multipart part;
private
String username =
""
;
private
String password =
""
;
private
String profile =
"search.properties"
;
private
EdmMail(){
getValues();
setSmtpHost(mailServerHost);
getMailSession();
}
public
EdmMail(String sub){
this
();
MAIL_TITLE = sub;
}
private
boolean
getValues(){
Properties proties =
null
;
InputStream input =
null
;
try
{
input = EdmMail.
class
.getClassLoader().getResourceAsStream(profile);
proties =
new
Properties();
proties.load(input);
mailServerHost = proties.getProperty(
"MAIL_HOST"
);
fromAddress = proties.getProperty(
"MAIL_FROM"
);
username = proties.getProperty(
"MAIL_USERNAME"
);
password = proties.getProperty(
"MAIL_PASSWORD"
);
toAddress = proties.getProperty(
"MAILTO_LIST"
);
return
true
;
}
catch
(Exception e) {
System.out.println(
"读取配置文件出错!"
+e);
return
false
;
}
}
/***
* 设置SMTP主机
*/
private
void
setSmtpHost(String mailhost){
if
(properties ==
null
){
properties = System.getProperties();
}
properties.setProperty(
"mail.smtp.host"
, mailhost);
}
/***
* 获得邮件会话对象
* 创建MimeMessage对象
*/
private
boolean
getMailSession(){
try
{
session = Session.getDefaultInstance(properties,
null
);
}
catch
(Exception e) {
System.out.println(
"获取邮件会话对象出错,原因:"
+e);
return
false
;
}
try
{
mimemsg =
new
MimeMessage(session);
part =
new
MimeMultipart();
return
true
;
}
catch
(Exception e) {
System.out.println(
"创建Mime邮件对象出错,原因:"
+e);
return
false
;
}
}
/***
* 设置smtp身份认证
* mail.smtp.auth = true
*/
private
void
setSmtpAuth(
boolean
bool){
if
(properties ==
null
){
properties = System.getProperties();
}
if
(bool){
properties.put(
"mail.smtp.auth"
,
"true"
);
}
else
{
properties.put(
"mail.smtp.auth"
,
"false"
);
}
}
/***
* 设置邮件的主题
*/
private
boolean
setMailSub(String mailsubject){
try
{
mimemsg.setSubject(mailsubject,
"GBK"
);
return
true
;
}
catch
(Exception e) {
System.out.println(
"设置邮件标题出错,原因:"
+e);
return
false
;
}
}
/***
* 设置邮件体格式
*/
private
boolean
setMailBody(String mailBody){
BodyPart bdyPart =
new
MimeBodyPart();
try
{
bdyPart.setContent(mailBody,
"text/html;charset=GBK"
);
part.addBodyPart(bdyPart);
return
true
;
}
catch
(Exception e) {
System.out.println(
"设置邮件体格式出错,原因:"
+e);
return
false
;
}
}
/***
* 添加邮件附件
*/
private
boolean
addAttach(String filePath){
BodyPart bdy =
new
MimeBodyPart();
try
{
FileDataSource dataSource =
new
FileDataSource(filePath);
bdy.setDataHandler(
new
DataHandler(dataSource));
bdy.setFileName(dataSource.getName());
part.addBodyPart(bdy);
return
true
;
}
catch
(Exception e) {
System.out.println(
"添加附件:"
+filePath+
"出错,原因:"
+e);
return
false
;
}
}
/***
* 设置邮件发送人
*/
private
boolean
setMailFrom(String from){
try
{
mimemsg.setFrom(
new
InternetAddress(from));
return
true
;
}
catch
(Exception e) {
System.out.println(
"设置邮件发送人:"
+from+
"出错,原因:"
+e);
return
false
;
}
}
/***
* 设置邮件接收人
*/
private
boolean
setMailTo(String mailto){
if
(mailto ==
null
){
return
false
;
}
try
{
mimemsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailto));
return
true
;
}
catch
(Exception e) {
System.out.println(
"设置邮件接收人:"
+mailto+
"出错,原因:"
+e);
return
false
;
}
}
/***
* 发送邮件
*/
private
boolean
sendout(){
try
{
mimemsg.setContent(part);
mimemsg.saveChanges();
System.out.println(
"开始发送邮件......"
);
Session mailSession = Session.getInstance(properties,
null
);
Transport tsport = mailSession.getTransport(
"smtp"
);
tsport.connect((String)properties.get(
"mail.smtp.host"
), username, password);
tsport.sendMessage(mimemsg, mimemsg.getRecipients(Message.RecipientType.TO));
tsport.close();
System.out.println(
"发送邮件成功!"
);
return
true
;
}
catch
(Exception e) {
System.out.println(
"发送邮件出错原因:"
+e);
return
false
;
}
}
public
void
sendMail(String fileAttach){
StringBuffer content =
new
StringBuffer();
content.append(
"<html>"
);
content.append(
"<head><META content='text/html; charset=gbk' http-equiv=Content-Type>"
);
content.append(
"<META name=GENERATOR content='MSHTML 8.00.6001.18702'>"
);
content.append(
"<style type='text/css'>"
);
content.append(
".STYLE1 {color: #000000}"
);
content.append(
"TABLE {FONT-SIZE: 12px; COLOR: #444444;LINE-HEIGHT: 14px; FONT-FAMILY: '宋体', 'Arial'; TEXT-DECORATION: none;}"
);
content.append(
".STYLE3 { font-size: 13px;color: #FD9800; font-weight: bold;}"
);
content.append(
"</style>"
);
content.append(
"</head>"
);
content.append(
"<body>"
);
content.append(
"<table width='749px' align='left'>"
);
content.append(
"<tr><td width='749' height='10px'></td></tr>"
);
content.append(
"<tr><td><p align='left'><font size=2>for test!</font></p></td></tr>"
);
content.append(
"</table>"
);
content.append(
"</body>"
);
content.append(
"</html>"
);
System.out.println(
"content********"
+content);
setSmtpAuth(
false
);
if
(setMailSub(MAIL_TITLE) ==
false
){
return
;
}
if
(setMailBody(content.toString()) ==
false
){
return
;
}
if
(setMailTo(toAddress) ==
false
){
return
;
}
if
(setMailFrom(fromAddress) ==
false
){
return
;
}
if
(addAttach(fileAttach) ==
false
){
return
;
}
if
(sendout()==
false
){
return
;
}
}
public
static
void
main(String[] args) {
EdmMail m =
new
EdmMail(
"test"
);
m.sendMail(
"D:\\workspace\\test.xls"
);
}
}
相关推荐
java 实现发送邮件功能,内含网页版发送邮件,校验验证码功能。希望能帮助大家
实现简单的邮件发送功能,可以使用它实现邮箱验证功能,以及发送通知,代码简洁,使用方便,效率高效。
Java实现发送邮件功能,代码经多次测试运行无误,请放心下载!
java实现发送邮件功能的jar包
压缩包中有纯java程序发送邮件其中有发送纯文本格式、带附件格式、图片格式、附件图片并显示格式。
简单 一个demo javamail 发送邮件。很容易学会。
之前用163邮箱发邮件时明明是成功的,但是使用中国移动自己的邮箱时,无论如何在linux服务器中都发送不成功。下面小编给大家说下我是怎么解决的,一起看下吧
java实现了发送邮件功能,java实现了发送邮件功能java实现了发送邮件功能,java实现了发送邮件功能
java实现邮件发送功能,支持邮件的群发和单发功能,包括可以携带附件的发送形式
java发送邮件工具类,用于java程序实现邮件的发送功能
JAVA mail源码,内容从网上获得经消化整理而来。包内有mail.jar、activation.jar、Java Mail常见错误.docx
实现java发送邮件功能,以正式gmail,hotmail等主流邮箱邮件都可接收到
今天整理资料,就把java发送邮件的功能系统化了一下,里面可以发送图片和附件功能,3分钟可以看到效果的Demo里面有代码运行后效果图
用java实现发送邮件,压缩文件中有个j2ee.jar:已经包含了javamail,JAF包,只是Java mail需要的 1)直接拷贝j2ee.jar,将其添加到jbuilder的library中,或系统ClassPath中。 2)压缩文件中还包含一个配置文件Mail...
Java代码实现发送邮件功能,支持发送带有附件的。需要直接拿走
通过Java实现的邮件发放功能,可自行设置邮箱类型,有示例代码