使用JavaMail发送邮件需要用到mail.jar和activtion.jar两个包。
该类实现了较完整的邮件发送功能,包括以HTML格式发送,添加附件和抄送人。下面是具体的代码:
package cn.cgw.util.mail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 Mail {
private MimeMessage mimeMsg; //MIME邮件对象
private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = false; //smtp是否需要认证
//smtp认证用户名和密码
private String username;
private String password;
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
/**
* Constructor
* @param smtp 邮件发送服务器
*/
public Mail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
}
/**
* 设置邮件发送服务器
* @param hostName String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = "+hostName);
if(props == null)
props = System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host",hostName); //设置SMTP主机
}
/**
* 创建MIME邮件对象
* @return
*/
public boolean createMimeMessage()
{
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props,null); //获得邮件会话对象
}
catch(Exception e){
System.err.println("获取邮件会话对象时发生错误!"+e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session); //创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch(Exception e){
System.err.println("创建MIME邮件对象失败!"+e);
return false;
}
}
/**
* 设置SMTP是否需要验证
* @param need
*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = "+need);
if(props == null) props = System.getProperties();
if(need){
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
/**
* 设置用户名和密码
* @param name
* @param pass
*/
public void setNamePass(String name,String pass) {
username = name;
password = pass;
}
/**
* 设置邮件主题
* @param mailSubject
* @return
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try{
mimeMsg.setSubject(mailSubject);
return true;
}
catch(Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
/**
* 设置邮件正文
* @param mailBody String
*/
public boolean setBody(String mailBody) {
try{
BodyPart bp = new MimeBodyPart();
bp.setContent(""+mailBody,"text/html;charset=GBK");
mp.addBodyPart(bp);
return true;
} catch(Exception e){
System.err.println("设置邮件正文时发生错误!"+e);
return false;
}
}
/**
* 添加附件
* @param filename String
*/
public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:"+filename);
try{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch(Exception e){
System.err.println("增加邮件附件:"+filename+"发生错误!"+e);
return false;
}
}
/**
* 设置发信人
* @param from String
*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try{
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
return true;
} catch(Exception e) {
return false;
}
}
/**
* 设置收信人
* @param to String
*/
public boolean setTo(String to){
if(to == null)return false;
try{
mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
return true;
} catch(Exception e) {
return false;
}
}
/**
* 设置抄送人
* @param copyto String
*/
public boolean setCopyTo(String copyto)
{
if(copyto == null)return false;
try{
mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
return true;
}
catch(Exception e)
{ return false; }
}
/**
* 发送邮件
*/
public boolean sendOut()
{
try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props,null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),username,password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC));
//transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch(Exception e) {
System.err.println("邮件发送失败!"+e);
return false;
}
}
/**
* 调用sendOut方法完成邮件发送
* @param smtp
* @param from
* @param to
* @param subject
* @param content
* @param username
* @param password
* @return boolean
*/
public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //需要验证
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* 调用sendOut方法完成邮件发送,带抄送
* @param smtp
* @param from
* @param to
* @param copyto
* @param subject
* @param content
* @param username
* @param password
* @return boolean
*/
public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //需要验证
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setCopyTo(copyto)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* 调用sendOut方法完成邮件发送,带附件
* @param smtp
* @param from
* @param to
* @param subject
* @param content
* @param username
* @param password
* @param filename 附件路径
* @return
*/
public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password,String filename) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //需要验证
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.addFileAffix(filename)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* 调用sendOut方法完成邮件发送,带附件和抄送
* @param smtp
* @param from
* @param to
* @param copyto
* @param subject
* @param content
* @param username
* @param password
* @param filename
* @return
*/
public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password,String filename) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //需要验证
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.addFileAffix(filename)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setCopyTo(copyto)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
}
需要用到javamail的jar包,网上有。找不到把邮箱贴出来,我发给你。 package test.servlet;import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.Properties;import javax.mail.Message; import j...
mail.jar 和activation.jar
javax.activation.jar java 发送邮件 需要哪些jar包~
activation.jar
mail.jar
之前我做了一个mail的好像就额外加了两个jar包。
activation.jar 和 mail.jar
#15551114062#
如何用Java编写一个发送邮件的程序 - ******
#山柱# 补充回答:如果不涉及到mime,只是收发简单文本邮件.就会比较简单.因为就像你说的,smtp和pop3协议就是文本格式的.你用socket建立连接后,就像读文件一样,一行一行的读数据,然后按照协议分析数据格式.例如如果开始时user那...
#15551114062#
Java 开发邮件收发功能有些什么东西要配置 - ******
#山柱# 1. 邮件服务器地址,端口2. 是否需要认证,如果是,需要用户名密码3. 本地连出是否需要代理,如果是,需要代理地址,端口;代理是否需要认证,如果是,需要用户名密码.
#15551114062#
java发邮件之前需要先做什么,要不要先设置邮件服务器什么的 - ******
#山柱# 我对java不懂,但是你想发邮箱,服务器要安装jmail组件.程序里面肯定要设置pop服务器的.
#15551114062#
java如何实现邮件的发送. ******
#山柱# smtp实现邮件的发送,而pop3时间邮件的接受,具体实现细节可以在网上下载代码
#15551114062#
我想用java编写邮件系统需要掌握什么知识 - ******
#山柱# apache也有一个开源项目叫做James邮件系统,可以去看一下 除此之外呢,肯定需要掌握的就是一门编程语言咯,上面给你推荐的james邮件系统的话,你就必须会java,希望能够帮助到你,也恳请你的采纳,谢谢……
#15551114062#
用java开发邮件系统需要了解什么知识呢?跪求 ******
#山柱# Vernal邮件系统完全基于JAVA开发,包括服务器核心也是根据邮件相关协议用JAVA实现,而其他的WEBMAIL 管理平台等则基于J2EE技术开发.如果只是开始WEBMAIL等,最基本的要精通JAVAMAIL这个SUN公司发布的库;当然如果是开发服务器核心,则所有邮件相关的IEFT协议标准要精通,包括SMTP POP3 IMAP4等大概几百份标准文档至少要熟悉,最好是精通.欢迎到Vernal邮件系统版块讨论.
#15551114062#
java邮件发送附件 怎么添加内容 - ******
#山柱# 实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Message对象,然后设置邮件主题和正文 利用Transport对象发送邮件 需要的jar有2个
#15551114062#
java邮件发送代码 - ******
#山柱# package cn.gov.gdcct.base; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import java.util.Date; import javax.activation.*; import java.io.*;public class SendMail {private MimeMessage mimeMsg; // MIME邮件对象private Session ...
#15551114062#
在java中,如何配置email发送啊! - ******
#山柱# 在下发邮件教程和API,网上可以找的到,有本张孝祥的用JAVA开发Email书要下一个mail发邮件的JAR包
#15551114062#
java发送html格式邮件是否能有js - ******
#山柱# 要带js的,如果是想用HTML做发送的内容及布局的话.如果你是在发送邮件的时候将HTML文件作为附件的话,那就不要用js,那是可以有js的