关键字: javamail, 邮件
1、MimeMessage的包装类
2、发送邮件的工具类
3、包含邮件配置信息的bean对象
4、实例代码
- public class SimpleMessageWraper {
- private Session session;
- private MimeMessage mimeMessage;
- private Multipart multipart = new MimeMultipart();;
- public Session getSession() {
- return session;
- }
- public MimeMessage getMimeMessage() {
- return mimeMessage;
- }
- private String arrayToString(String[] array){
- String s = "";
- if(array != null && array.length > 0){
- for(int i=0;i<array.length;i++){
- if(s == ""){
- s = array[i].trim();
- }else{
- s += "," + array[i].trim();
- }
- }
- }
- return s;
- }
- public SimpleMessageWraper(Properties props){
- session = Session.getInstance(props);
- session.setDebug(false);
- mimeMessage = new MimeMessage(session);
- }
- public void setFrom(String from)throws MessagingException{
- mimeMessage.setFrom(new InternetAddress(from));
- mimeMessage.saveChanges();
- }
- public void setTo(String[] toArray)throws MessagingException{
- String s = arrayToString(toArray);
- Address[] addresses = InternetAddress.parse(s);
- mimeMessage.setRecipients(Message.RecipientType.TO, addresses);
- mimeMessage.saveChanges();
- }
- public void setCc(String[] ccArray)throws MessagingException{
- String s = arrayToString(ccArray);
- Address[] addresses = InternetAddress.parse(s);
- mimeMessage.setRecipients(Message.RecipientType.CC, addresses);
- mimeMessage.saveChanges();
- }
- public void setSubject(String subject)throws MessagingException{
- mimeMessage.setSubject(subject);
- mimeMessage.saveChanges();
- }
- public void setSentDate(Date date)throws MessagingException{
- mimeMessage.setSentDate(date);
- mimeMessage.saveChanges();
- }
- public void setText(String text)throws MessagingException{
- MimeBodyPart mimeBodyPart = new MimeBodyPart();
- mimeBodyPart.setText(text);
- multipart.addBodyPart(mimeBodyPart);
- mimeMessage.setContent(multipart);
- mimeMessage.saveChanges();
- }
- public void addAttactment(File file)throws MessagingException,UnsupportedEncodingException{
- if(file == null) return;
- MimeBodyPart mimeBodyPart = new MimeBodyPart();
- mimeBodyPart.setFileName(MimeUtility.encodeText(file.getName(), "GBK", "B")); //对邮件头采用base64方式编码
- mimeBodyPart.setDataHandler(new DataHandler(new FileDataSource(file)));
- multipart.addBodyPart(mimeBodyPart);
- mimeMessage.setContent(multipart);
- mimeMessage.saveChanges();
- }
- }
public class SimpleMessageWraper {
private Session session;
private MimeMessage mimeMessage;
private Multipart multipart = new MimeMultipart();;
public Session getSession() {
return session;
}
public MimeMessage getMimeMessage() {
return mimeMessage;
}
private String arrayToString(String[] array){
String s = "";
if(array != null && array.length > 0){
for(int i=0;i<array.length;i++){
if(s == ""){
s = array[i].trim();
}else{
s += "," + array[i].trim();
}
}
}
return s;
}
public SimpleMessageWraper(Properties props){
session = Session.getInstance(props);
session.setDebug(false);
mimeMessage = new MimeMessage(session);
}
public void setFrom(String from)throws MessagingException{
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.saveChanges();
}
public void setTo(String[] toArray)throws MessagingException{
String s = arrayToString(toArray);
Address[] addresses = InternetAddress.parse(s);
mimeMessage.setRecipients(Message.RecipientType.TO, addresses);
mimeMessage.saveChanges();
}
public void setCc(String[] ccArray)throws MessagingException{
String s = arrayToString(ccArray);
Address[] addresses = InternetAddress.parse(s);
mimeMessage.setRecipients(Message.RecipientType.CC, addresses);
mimeMessage.saveChanges();
}
public void setSubject(String subject)throws MessagingException{
mimeMessage.setSubject(subject);
mimeMessage.saveChanges();
}
public void setSentDate(Date date)throws MessagingException{
mimeMessage.setSentDate(date);
mimeMessage.saveChanges();
}
public void setText(String text)throws MessagingException{
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(text);
multipart.addBodyPart(mimeBodyPart);
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
}
public void addAttactment(File file)throws MessagingException,UnsupportedEncodingException{
if(file == null) return;
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setFileName(MimeUtility.encodeText(file.getName(), "GBK", "B")); //对邮件头采用base64方式编码
mimeBodyPart.setDataHandler(new DataHandler(new FileDataSource(file)));
multipart.addBodyPart(mimeBodyPart);
mimeMessage.setContent(multipart);
mimeMessage.saveChanges();
}
}
2、发送邮件的工具类
- public class SimpleMailFactory {
- private static SimpleMailConfig simpleMailConfig = null;
- private static Properties props = null;
- static{
- //取得包含邮件配置信息的bean对象
- simpleMailConfig = (SimpleMailConfig)SpringContextUtil.getBean(Constants.simpleMailConfigBeanName);
- props = new Properties();
- props.put("mail.smtp.host", simpleMailConfig.getHost());
- props.put("mail.smtp.auth", simpleMailConfig.getAuth());
- }
- /**
- * 功能:取得邮件消息对象的包装器
- */
- public static SimpleMessageWraper getSimpleMessageWraper(){
- return new SimpleMessageWraper(props);
- }
- /**
- * 功能:邮件发送
- */
- public static void send(SimpleMessageWraper messageWraper)throws Exception{
- MimeMessage mimeMessage = messageWraper.getMimeMessage();
- Transport transport = messageWraper.getSession().getTransport(simpleMailConfig.getTransport());
- transport.connect(simpleMailConfig.getHost(), simpleMailConfig.getUsername(), simpleMailConfig.getPassword());
- transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
- transport.close();
- }
- }
public class SimpleMailFactory {
private static SimpleMailConfig simpleMailConfig = null;
private static Properties props = null;
static{
//取得包含邮件配置信息的bean对象
simpleMailConfig = (SimpleMailConfig)SpringContextUtil.getBean(Constants.simpleMailConfigBeanName);
props = new Properties();
props.put("mail.smtp.host", simpleMailConfig.getHost());
props.put("mail.smtp.auth", simpleMailConfig.getAuth());
}
/**
* 功能:取得邮件消息对象的包装器
*/
public static SimpleMessageWraper getSimpleMessageWraper(){
return new SimpleMessageWraper(props);
}
/**
* 功能:邮件发送
*/
public static void send(SimpleMessageWraper messageWraper)throws Exception{
MimeMessage mimeMessage = messageWraper.getMimeMessage();
Transport transport = messageWraper.getSession().getTransport(simpleMailConfig.getTransport());
transport.connect(simpleMailConfig.getHost(), simpleMailConfig.getUsername(), simpleMailConfig.getPassword());
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}
}
3、包含邮件配置信息的bean对象
- <bean id="simpleMailConfig" class="com.cjm.common.mail.SimpleMailConfig">
- <property name="host" value="smtp.163.com"/>
- <property name="username" value="uid"/>
- <property name="password" value="pwd"/>
- <property name="auth" value="true"/>
- </bean>
4、实例代码
- SimpleMessageWraper w1 = SimpleMailFactory.getSimpleMessageWraper();
- w1.setFrom("ServerMailAddress@163.com");
- w1.setTo(new String[]{"TargetAddress@139.com"});
- w1.setSubject("标题");
- w1.setText("邮件内容");
- w1.addAttactment(new File("c://附件.txt"));
- SimpleMailFactory.send(w1);