風行草偃

陪伴,是最长情的告白。

JavaMail 发送邮件--[可发送HTML、可群发]

赞!

Hung:

来源: http://www.codefans.net/articles/209.shtml

 

package com.hung.demo_1;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

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 JavaMailDemo {

private Properties props; 

private Session session;

private MimeMessage message;

private MimeMultipart multipart;

/**

 * 构造方法

 * @param smtp邮件smtp服务器

 */

public JavaMailDemo(String smtp) {

if (props == null) {

props = System.getProperties();

}

props.put("mail.smtp.host", smtp);

}

/**

 * 发送邮件

 * @param subject邮件主题

 * @param mailBody邮件主体

 * @param addressee收件人

 * @param copyto抄送

 * @param addresser发件人

 * @param userName电子邮件账户

 * @param userPasswd电子邮件密码

 * @return执行结果

 */

public boolean sendEmail(

String subject , 

String mailBody , 

String addressee , 

String copyto , 

String addresser , 

String userName , 

String userPasswd ){

// 获取邮件会话对象

try {

System.out.println("获取邮件会话对象!");

session = Session.getDefaultInstance(props, null);

} catch (Exception e) {

e.printStackTrace();

//获取失败

System.out.println("获取邮件会话对象失败!");

return false;

}

// 创建MIME邮件对象

try{

System.out.println("创建MIME邮件对象!");

message = new MimeMessage(session);

multipart = new MimeMultipart();

}catch (Exception e) {

e.printStackTrace();

// 创建MIME邮件对象失败

System.out.println("创建MIME邮件对象失败!");

return false;

}

// 定义邮件主题

try {

System.out.println("定义邮件主题!");

message.setSubject(subject);

} catch (Exception e) {

e.printStackTrace();

//定义邮件主题发生错误

System.out.println("定义邮件主题发生错误!");

return false;

}

// 定义邮件正文主体

try {

System.out.println("定义邮件正文主体!");

BodyPart bp = new MimeBodyPart();

bp.setContent("" + mailBody, "text/html;charset=GBK");

multipart.addBodyPart(bp);

} catch (Exception e) {

e.printStackTrace();

// 定义邮件正文失败

System.out.println("定义邮件正文失败!");

return false;

}

// 定义收信人!

try {

System.out.println("定义收信人!");

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addressee));

} catch (Exception e) {

e.printStackTrace();

// 定义收件人失败

System.out.println("定义收件人失败!");

return false;

}

// 定义抄送

try {

System.out.println("定义抄送!");

message.setRecipients(

Message.RecipientType.CC, 

(Address[]) InternetAddress.parse(copyto));

} catch (Exception e) {

e.printStackTrace();

// 定义抄送失败

System.out.println("定义抄送失败!");

return false;

}

// 定义发件人

try {

System.out.println(" 定义发件人!");

message.setFrom(new InternetAddress(addresser));

} catch (Exception e) {

e.printStackTrace();

// 定义发件人失败

System.out.println("定义发件人失败!");

return false;

}

// 发送邮件

try {

System.out.println("开始发送邮件...");

message.setContent(multipart);

message.saveChanges();

System.out.println("邮件发送中....");

Session mailSession = Session.getInstance(props, null);

Transport transport = mailSession.getTransport("smtp");

transport.connect((String) props.get("mail.smtp.host"), userName, userPasswd);

transport.sendMessage(

message, message.getRecipients(Message.RecipientType.TO));

System.out.println("发送成功!");

transport.close();

} catch (Exception e) {

e.printStackTrace();

System.out.println("发送失败!");

return false;

}

return true;

}

/*

public static void main(String[] args) {

if(new JavaMailDemo("smtp.163.com").sendEmail(

"JavaMailDemo!", 

"<html>" +

"<body>" +

"<center>" +

"<h1>Hello JavaMail!</h1>" +

"<a href=\"http://hung-hung.lofter.com\">查看我的微博!</a>" +

"</center>" +

"</body>" +

"</html>", 

"XXX@XXX.com", 

"XXX@XXX.com", 

"XXX@XXX.com", 

"XXX@XXX.com", 

"***************")){

System.out.println("Done!");

}

}

*/

}






评论
热度 ( 2 )
  1. 風行草偃鸿则 转载了此文字
    赞!

© 風行草偃 | Powered by LOFTER