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(“yangguangli19871124@gmail.com”);
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);
}
}
分享到:
相关推荐
也许很多 同道遇到和我一样的问题,建了个web项目,写一堆代码,结果一运行老是报错!!...Java代码+mailjar 搞定!!!如果还没有成功,你可以给我发邮件973203075@qq.com,我可以把项目也给你!!
java 发邮件的3个jar包 mail.jar commons-email-1.1.jar activation.jar
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/
发送邮件服务,一般项目中都会使用,这个类库提供了详细的发送服务
commons-email-1.3.jar_mail.jar_activation.jar
用于发送邮件的java包,包括activation.jar和mail.jar
simple sending email by java. must have mail.jar first if do not.
javamail的jar包,,,,,,,,,,,,。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
activation-1.1 commons-email activation java发送邮件jar 教程: http://blog.csdn.net/sir_jun/article/details/9106397
java实现发送email验证码的jar包依赖 共有activation.jar、commons-email-1.4.jar、mail.jar
用Java开发邮箱所需要的jar包,里面都有mail.jar activation.jar commons-email-1.3.jar 邮件
javax.mail-1.6.0.jar包及移入maven仓库语句
基于Java调用邮件系统所需要的jar包,commons-email-1.5.jar,javax.mail-1.4.5.jar,activation.jar
java email邮件发送所需mail相关jar包,下载后可直接使用,发送邮件 涉及到JAVA邮件上传的JAR包 mail.jar activation.jar
Java发送邮件所需要的几个JAR包,打包发给大家,包含以下内容: commons-email-1.1.jar mail.jar additionnal.jar activation.jar
集成邮件发送所需要的四个jar包,activation.jar、additionnal.jar、commons-email-1.4.jar、mail.jar
JAVA源包所没有的,对开发基于JSP页的邮件系统这个包就非用不可了!具体信息可参见Apache官方网站!
Java发送邮件所需要的两个包。activation.jar and mail.jar
使用java发送邮件的jar包,都在这里了..(收集得..) pop3.jar smtp.jar mail.jar mailapi.jar commons-email.jar activation.jar ..
用java 开发email 使用的包都有了 mail-1.4.1.jar activation.jar commons-email-1.2.jar commons-email-1.2-javadoc.jar commons-email-1.2-sources.jar