Mailer类的说明:
具有两个构造函数
- Mailer() 读取properties文件的数据配置javamail的发送属性
- Mailer(String mail_host, String stmp_port, String template_file_path, String send_from, String pw, String charset) 使用参数配置javamail的发送属性
- sendMail()发送邮件主函数
- getTemplate()从服务器指定路径读取以存文件作为发送邮件的content
环境配置说明:
必须引入的两个开发包:mail.jar activation.jar
可能出现的错误说明:
Mailer.java
/**/
/*---------Operating instructions----edit by lynn at 20080319
* Make a JSP file like this:
* <%@ page import="mailer.*"%>
<%
Mailer mailer = new Mailer();
//In Mailer class,you can use a template file for the mail content by the method of getTemplate().
String content = Mailer.getTemplate("MemberApplyFirst.txt");
mailer.sendMail("yehell@hotmail.com", "test", content ,"VOGUE");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Mailer mail = new Mailer("gmail-smtp.l.google.com","25","E://test//","lynn.yehell@gmail.com","password","utf-8");
mail.sendMail("yehell@hotmail.com", "test", content ,"VOGUE");
%>
*/
package
mailer;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.io.BufferedReader;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.util.ArrayList;
import
java.util.Date;
import
java.util.HashMap;
import
java.util.Properties;
import
java.util.ResourceBundle;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.mail.Authenticator;
import
javax.mail.Message;
import
javax.mail.MessagingException;
import
javax.mail.PasswordAuthentication;
import
javax.mail.Session;
import
javax.mail.Transport;
import
javax.mail.internet.AddressException;
import
javax.mail.internet.InternetAddress;
import
javax.mail.internet.MimeMessage;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Mailer
...
{
private static ResourceBundle bundle;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static String TEMPLATE_FILE_PATH;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static String MAIL_HOST;
private static String SMTP_PORT;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static String USER_NAME;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static String USER_PASSWORD;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static String SEND_FROM;
private static String MAIL_CHARSET;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Properties props;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Session session;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private MimeMessage msg;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static HashMap templates;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Mailer() ...{
super();
//Get the data of properties file
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (bundle == null) ...{
bundle = ResourceBundle.getBundle("mail_config");
TEMPLATE_FILE_PATH = bundle.getString("TEMPLATE_FILE_PATH");
MAIL_HOST = bundle.getString("MAIL_HOST");
SMTP_PORT = bundle.getString("SMTP_PORT");
USER_NAME = bundle.getString("USER_NAME");
USER_PASSWORD = bundle.getString("USER_PASSWORD");
SEND_FROM = bundle.getString("SEND_FROM");
MAIL_CHARSET = bundle.getString("MAIL_CHARSET");
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
e.printStackTrace();
}
bundle = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Get a Properties object
this.props = System.getProperties();
props.setProperty("mail.smtp.host", MAIL_HOST );
props.setProperty("mail.smtp.port", SMTP_PORT );
props.setProperty("mail.smtp.socketFactory.port", SMTP_PORT );
props.put("mail.smtp.socketFactory.fallback", "true" );
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable", "true" );
props.put("mail.smtp.auth", "true" );
// props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.session = Session.getInstance(props, new Authenticator() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
protected PasswordAuthentication getPasswordAuthentication() ...{
return new PasswordAuthentication(USER_NAME, USER_PASSWORD);
}
});
//this.session.setDebug(true);
this.msg = new MimeMessage(session);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Mailer(String mail_host, String stmp_port, String template_file_path, String send_from, String pw, String charset) ...{
super();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
TEMPLATE_FILE_PATH = template_file_path;
SEND_FROM = send_from;
USER_NAME = send_from;
USER_PASSWORD = pw;
MAIL_CHARSET = charset;
// Get a Properties object
this.props = System.getProperties();
props.setProperty("mail.smtp.host", mail_host );
props.setProperty("mail.smtp.port", stmp_port );
props.setProperty("mail.smtp.socketFactory.port", stmp_port );
props.put("mail.smtp.socketFactory.fallback", "true" );
props.put("mail.transport.protocol", "smtp" );
props.put("mail.smtp.starttls.enable", "true" );
props.put("mail.smtp.auth", "true" );
// props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
this.session = Session.getInstance(props, new Authenticator() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
protected PasswordAuthentication getPasswordAuthentication() ...{
return new PasswordAuthentication(USER_NAME, USER_PASSWORD);
}
});
this.session.setDebug(true);
this.msg = new MimeMessage(session);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void sendMail(String to, String title, String content, String site)throws Exception ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
msg.setFrom(new InternetAddress(SEND_FROM , SEND_FROM.substring(0,SEND_FROM.indexOf("@"))));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(title, MAIL_CHARSET );
msg.setText(content);
msg.setSentDate(new Date());
Transport.send(msg);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (AddressException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (MessagingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static String getMailContent(String template, ArrayList params) ...{
String strReturn = template;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = params.size(); i > 0; i--) ...{
strReturn = strReturn.replaceAll("%" + (i), params.get(i - 1).toString());
}
return strReturn;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static String getTemplate(String fileName) throws Exception ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (templates == null) ...{
templates = new HashMap();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (templates.get(fileName) != null) ...{
return templates.get(fileName).toString();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
String filepath = TEMPLATE_FILE_PATH + fileName;
File file = new File(filepath);
String returnStr = "";
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (file.isFile()) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
InputStreamReader read = new InputStreamReader(new FileInputStream(file), MAIL_CHARSET);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
BufferedReader bReader = new BufferedReader(read);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String line;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while ((line = bReader.readLine()) != null) ...{
returnStr += line;
returnStr += " ";
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (FileNotFoundException e) ...{
e.printStackTrace();
throw new Exception(e);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (IOException e) ...{
e.printStackTrace();
throw new Exception(e);
}
templates.put(fileName, returnStr);
}
return returnStr;
}
}
}
mail_config.properties 文件(该文件必须放在classes目录下才能被读取)
TEMPLATE_FILE_PATH
=
E:
//
test
//
MAIL_HOST
=
gmail-smtp
.
l
.
google
.
com
SMTP_PORT
=
25
MAIL_CHARSET
=
SHIFT
-JIS
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
USER_NAME
=
guofeng
.
matest@gmail
.
com
USER_PASSWORD
=
maguofeng1
SEND_FROM
=
guofeng
.
matest@gmail
.
com