java中利用mail.jar發送email

近期項目中有個利用程序發送email的需求,今天上網查了下,使用mail.jar庫實現起來還是比較簡單的,java中的工具是在是太多了,贊下,不過也會讓人變得越來越笨的…..

總結下大體的步驟:
1,下載activation.jar和mail.jar包,可以到sun的官網上下載不過速度比較慢,我是在http://download.csdn.net/source/640980上下載的,沒有版本說明,不過一般的要求應該是能夠滿足的
下面開始編寫發送email程序:
<1>利用Properties設置一些基本的配置,比如

//設置一些基本屬性
property.put(“mail.smtp.host”, SMTP_HOST);
property.put(“mail.smtp.port”, SMTP_PORT);
property.put(“mail.smtp.protocol” , SMTP_PROTOCOL);
property.put(“mail.smtp.auth”,SMTP_AUTH);


<2>自己編寫一個認證類,繼承自抽象類Authenticator,並實現getPasswordAuthentication方法,如下:

class MyAuthenticator extends Authenticator
{
private String user;
private String password;
public MyAuthenticator(String user,String password)
{
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,password);
}
}

其中user和password是正常登陸郵箱時屬性的用戶名和密碼的驗證信息
<3>獲得發送郵件的會話

MyAuthenticator myauth = new MyAuthenticator(AUTH_USER,AUTH_PASSWORD);
//獲得發送郵件的會話
Session mailSession = Session.getDefaultInstance(property,myauth);

<4>生成所要發送的message,包括目的地址,發送地址,title,content

//生成發送的消息
Message message = new MimeMessage(mailSession);
try{
//形成發送的mail地址
InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
//加入發送消息的目的地址addRecipients()兩個重載函數
message.addRecipient(Message.RecipientType.TO, toAddress);
//設置消息題
message.setSubject(title);
//設置消息主題
message.setText(content);
//保存
message.saveChanges();
}

<5>發送email

//發送mail
try
{
Transport.send(message, message.getRecipients(Message.RecipientType.TO));
}

總的來說,發送email的過程是:生成會話包括認證信息和配置信息->message消息->發送->結束。
具體的API請參看官方手冊
源程序如下

package org.solucking.smf.util;
import java.util.Date;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.AddressException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendMail {
public static String SMTP_HOST = “smtp.163.com”;
public static String SMTP_PORT = “25″;
public static String SMTP_PROTOCOL = “SMTP”;
public static String SMTP_AUTH = “true”;
public static String AUTH_USER = “*****”;
public static String AUTH_PASSWORD = “****”;
public static String FROM_ADDRESS = “*****@163.com”;
/**
* 發送email,目的地址爲一個
* @param to 目的email地址
* @param title email的標題
* @param content email的內容
* @return 返回是否發送成功
*/
public static boolean send(String to,String title,String content)
{
boolean isSuccess = true;
if( to == null || title == null || content == null)return false;
Properties property = new Properties();
//設置一些基本屬性
property.put(“mail.smtp.host”, SMTP_HOST);
property.put(“mail.smtp.port”, SMTP_PORT);
property.put(“mail.smtp.protocol” , SMTP_PROTOCOL);
property.put(“mail.smtp.auth”,SMTP_AUTH);
MyAuthenticator myauth = new MyAuthenticator(AUTH_USER,AUTH_PASSWORD);
//獲得發送郵件的會話
Session mailSession = Session.getDefaultInstance(property,myauth);
//生成發送的消息
Message message = new MimeMessage(mailSession);
try{
//形成發送的mail地址
InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
//加入發送消息的目的地址addRecipients()兩個重載函數
message.addRecipient(Message.RecipientType.TO, toAddress);
//設置消息題
message.setSubject(title);
//設置消息主題
message.setText(content);
//保存
message.saveChanges();
}
catch(Exception e)
{
isSuccess = false;
System.out.println(e.getMessage());
}
//發送mail
try
{
Transport.send(message, message.getRecipients(Message.RecipientType.TO));
}
catch(Exception e)
{
isSuccess = false;
System.out.println(e.getMessage());
}
return isSuccess;
}
/**
* 發送email,目的地址爲一組
* @param toList 一組email地址
* @param title email的標題
* @param content email的內容
* @return boolean 返回是否成功
*/
public static boolean send(List toList,String title,String content)
{
boolean isSuccess = true;
if( toList == null || title == null || content == null || toList.size() == 0 )return false;
Properties property = new Properties();
//設置一些基本屬性
property.put(“mail.smtp.host”, SMTP_HOST);
property.put(“mail.smtp.port”, SMTP_PORT);
property.put(“mail.smtp.protocol” , SMTP_PROTOCOL);
property.put(“mail.smtp.auth”,SMTP_AUTH);
MyAuthenticator myauth = new MyAuthenticator(AUTH_USER,AUTH_PASSWORD);
//獲得發送郵件的會話
Session mailSession = Session.getDefaultInstance(property,myauth);
//生成發送的消息
Message message = new MimeMessage(mailSession);
try{
//形成發送的mail地址
InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);
message.setFrom(fromAddress);
for(String to:toList)
{
InternetAddress toAddress = new InternetAddress(to);
//加入發送消息的目的地址addRecipients()兩個重載函數
message.addRecipient(Message.RecipientType.TO, toAddress);
}
//設置消息題
message.setSubject(title);
//設置消息主題
message.setText(content);
//保存
message.saveChanges();
}
catch(Exception e)
{
isSuccess = false;
System.out.println(e.getMessage());
}
//發送mail
try
{
Transport.send(message, message.getRecipients(Message.RecipientType.TO));
}
catch(Exception e)
{
isSuccess = false;
System.out.println(e.getMessage());
}
return isSuccess;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO
String to = “*****@163.com”;
String title = “email 測試程序”;
String content = “我測試用得,不要回復呀。”;
List toList = new ArrayList();
toList.add(to);
toList.add(“[email protected]”);
boolean res = SendMail.send(toList, title, content);
if( res == true )System.out.println(“發送成功”);

}
}
class MyAuthenticator extends Authenticator
{
private String user;
private String password;
public MyAuthenticator(String user,String password)
{
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,password);
}
}

 

 

另參考:http://zhaozhi3758.iteye.com/blog/584517

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章