java中javamail发送带附件的邮件实现方法
程序员文章站
2024-03-01 10:20:04
本文实例讲述了java中javamail发送带附件的邮件实现方法。分享给大家供大家参考。具体分析如下:
javamail,顾名思义,提供给开发者处理电子邮件相关的编程接口...
本文实例讲述了java中javamail发送带附件的邮件实现方法。分享给大家供大家参考。具体分析如下:
javamail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是sun发布的用来处理email的api。它可以方便地执行一些常用的邮件传输,javamail是可选包,因此如果需要使用的话你需要首先从java官网上下载。目前最新版本是javamail1.5.0,下面我们来看看javamail发送带附件的邮件实例
mail.java 代码:
复制代码 代码如下:
package mail;
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class mail {
//定义发件人、收件人、smtp服务器、用户名、密码、主题、内容等
private string displayname;
private string to;
private string from;
private string smtpserver;
private string username;
private string password;
private string subject;
private string content;
private boolean ifauth; //服务器是否要身份认证
private string filename="";
private vector file = new vector(); //用于保存发送附件的文件名的集合
/**
* 设置smtp服务器地址
*/
public void setsmtpserver(string smtpserver){
this.smtpserver=smtpserver;
}
/**
* 设置发件人的地址
*/
public void setfrom(string from){
this.from=from;
}
/**
* 设置显示的名称
*/
public void setdisplayname(string displayname){
this.displayname=displayname;
}
/**
* 设置服务器是否需要身份认证
*/
public void setifauth(boolean ifauth){
this.ifauth=ifauth;
}
/**
* 设置e-mail用户名
*/
public void setusername(string username){
this.username=username;
}
/**
* 设置e-mail密码
*/
public void setpassword(string password){
this.password=password;
}
/**
* 设置接收者
*/
public void setto(string to){
this.to=to;
}
/**
* 设置主题
*/
public void setsubject(string subject){
this.subject=subject;
}
/**
* 设置主体内容
*/
public void setcontent(string content){
this.content=content;
}
/**
* 该方法用于收集附件名
*/
public void addattachfile(string fname){
file.addelement(fname);
}
public mail(){
}
/**
* 初始化smtp服务器地址、发送者e-mail地址、用户名、密码、接收者、主题、内容
*/
public mail(string smtpserver,string from,string displayname,string username,string password,string to,string subject,string content){
this.smtpserver=smtpserver;
this.from=from;
this.displayname=displayname;
this.ifauth=true;
this.username=username;
this.password=password;
this.to=to;
this.subject=subject;
this.content=content;
}
/**
* 初始化smtp服务器地址、发送者e-mail地址、接收者、主题、内容
*/
public mail(string smtpserver,string from,string displayname,string to,string subject,string content){
this.smtpserver=smtpserver;
this.from=from;
this.displayname=displayname;
this.ifauth=false;
this.to=to;
this.subject=subject;
this.content=content;
}
/**
* 发送邮件
*/
public hashmap send(){
hashmap map=new hashmap();
map.put("state", "success");
string message="邮件发送成功!";
session session=null;
properties props = system.getproperties();
props.put("mail.smtp.host", smtpserver);
if(ifauth){ //服务器需要身份认证
props.put("mail.smtp.auth","true");
smtpauth smtpauth=new smtpauth(username,password);
session=session.getdefaultinstance(props, smtpauth);
}else{
props.put("mail.smtp.auth","false");
session=session.getdefaultinstance(props, null);
}
session.setdebug(true);
transport trans = null;
try {
message msg = new mimemessage(session);
try{
address from_address = new internetaddress(from, displayname);
msg.setfrom(from_address);
}catch(java.io.unsupportedencodingexception e){
e.printstacktrace();
}
internetaddress[] address={new internetaddress(to)};
msg.setrecipients(message.recipienttype.to,address);
msg.setsubject(subject);
multipart mp = new mimemultipart();
mimebodypart mbp = new mimebodypart();
mbp.setcontent(content.tostring(), "text/html;charset=gb2312");
mp.addbodypart(mbp);
if(!file.isempty()){//有附件
enumeration efile=file.elements();
while(efile.hasmoreelements()){
mbp=new mimebodypart();
filename=efile.nextelement().tostring(); //选择出每一个附件名
filedatasource fds=new filedatasource(filename); //得到数据源
mbp.setdatahandler(new datahandler(fds)); //得到附件本身并至入bodypart
mbp.setfilename(fds.getname()); //得到文件名同样至入bodypart
mp.addbodypart(mbp);
}
file.removeallelements();
}
msg.setcontent(mp); //multipart加入到信件
msg.setsentdate(new date()); //设置信件头的发送日期
//发送信件
msg.savechanges();
trans = session.gettransport("smtp");
trans.connect(smtpserver, username, password);
trans.sendmessage(msg, msg.getallrecipients());
trans.close();
}catch(authenticationfailedexception e){
map.put("state", "failed");
message="邮件发送失败!错误原因:\n"+"身份验证错误!";
e.printstacktrace();
}catch (messagingexception e) {
message="邮件发送失败!错误原因:\n"+e.getmessage();
map.put("state", "failed");
e.printstacktrace();
exception ex = null;
if ((ex = e.getnextexception()) != null) {
system.out.println(ex.tostring());
ex.printstacktrace();
}
}
//system.out.println("\n提示信息:"+message);
map.put("message", message);
return map;
}
}
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class mail {
//定义发件人、收件人、smtp服务器、用户名、密码、主题、内容等
private string displayname;
private string to;
private string from;
private string smtpserver;
private string username;
private string password;
private string subject;
private string content;
private boolean ifauth; //服务器是否要身份认证
private string filename="";
private vector file = new vector(); //用于保存发送附件的文件名的集合
/**
* 设置smtp服务器地址
*/
public void setsmtpserver(string smtpserver){
this.smtpserver=smtpserver;
}
/**
* 设置发件人的地址
*/
public void setfrom(string from){
this.from=from;
}
/**
* 设置显示的名称
*/
public void setdisplayname(string displayname){
this.displayname=displayname;
}
/**
* 设置服务器是否需要身份认证
*/
public void setifauth(boolean ifauth){
this.ifauth=ifauth;
}
/**
* 设置e-mail用户名
*/
public void setusername(string username){
this.username=username;
}
/**
* 设置e-mail密码
*/
public void setpassword(string password){
this.password=password;
}
/**
* 设置接收者
*/
public void setto(string to){
this.to=to;
}
/**
* 设置主题
*/
public void setsubject(string subject){
this.subject=subject;
}
/**
* 设置主体内容
*/
public void setcontent(string content){
this.content=content;
}
/**
* 该方法用于收集附件名
*/
public void addattachfile(string fname){
file.addelement(fname);
}
public mail(){
}
/**
* 初始化smtp服务器地址、发送者e-mail地址、用户名、密码、接收者、主题、内容
*/
public mail(string smtpserver,string from,string displayname,string username,string password,string to,string subject,string content){
this.smtpserver=smtpserver;
this.from=from;
this.displayname=displayname;
this.ifauth=true;
this.username=username;
this.password=password;
this.to=to;
this.subject=subject;
this.content=content;
}
/**
* 初始化smtp服务器地址、发送者e-mail地址、接收者、主题、内容
*/
public mail(string smtpserver,string from,string displayname,string to,string subject,string content){
this.smtpserver=smtpserver;
this.from=from;
this.displayname=displayname;
this.ifauth=false;
this.to=to;
this.subject=subject;
this.content=content;
}
/**
* 发送邮件
*/
public hashmap send(){
hashmap map=new hashmap();
map.put("state", "success");
string message="邮件发送成功!";
session session=null;
properties props = system.getproperties();
props.put("mail.smtp.host", smtpserver);
if(ifauth){ //服务器需要身份认证
props.put("mail.smtp.auth","true");
smtpauth smtpauth=new smtpauth(username,password);
session=session.getdefaultinstance(props, smtpauth);
}else{
props.put("mail.smtp.auth","false");
session=session.getdefaultinstance(props, null);
}
session.setdebug(true);
transport trans = null;
try {
message msg = new mimemessage(session);
try{
address from_address = new internetaddress(from, displayname);
msg.setfrom(from_address);
}catch(java.io.unsupportedencodingexception e){
e.printstacktrace();
}
internetaddress[] address={new internetaddress(to)};
msg.setrecipients(message.recipienttype.to,address);
msg.setsubject(subject);
multipart mp = new mimemultipart();
mimebodypart mbp = new mimebodypart();
mbp.setcontent(content.tostring(), "text/html;charset=gb2312");
mp.addbodypart(mbp);
if(!file.isempty()){//有附件
enumeration efile=file.elements();
while(efile.hasmoreelements()){
mbp=new mimebodypart();
filename=efile.nextelement().tostring(); //选择出每一个附件名
filedatasource fds=new filedatasource(filename); //得到数据源
mbp.setdatahandler(new datahandler(fds)); //得到附件本身并至入bodypart
mbp.setfilename(fds.getname()); //得到文件名同样至入bodypart
mp.addbodypart(mbp);
}
file.removeallelements();
}
msg.setcontent(mp); //multipart加入到信件
msg.setsentdate(new date()); //设置信件头的发送日期
//发送信件
msg.savechanges();
trans = session.gettransport("smtp");
trans.connect(smtpserver, username, password);
trans.sendmessage(msg, msg.getallrecipients());
trans.close();
}catch(authenticationfailedexception e){
map.put("state", "failed");
message="邮件发送失败!错误原因:\n"+"身份验证错误!";
e.printstacktrace();
}catch (messagingexception e) {
message="邮件发送失败!错误原因:\n"+e.getmessage();
map.put("state", "failed");
e.printstacktrace();
exception ex = null;
if ((ex = e.getnextexception()) != null) {
system.out.println(ex.tostring());
ex.printstacktrace();
}
}
//system.out.println("\n提示信息:"+message);
map.put("message", message);
return map;
}
}
smtpauth.java 代码:
复制代码 代码如下:
package mail;
public class smtpauth extends javax.mail.authenticator {
private string username,password;
public smtpauth(string username,string password){
this.username = username;
this.password = password;
}
protected javax.mail.passwordauthentication getpasswordauthentication() {
return new javax.mail.passwordauthentication(username,password);
}
}
public class smtpauth extends javax.mail.authenticator {
private string username,password;
public smtpauth(string username,string password){
this.username = username;
this.password = password;
}
protected javax.mail.passwordauthentication getpasswordauthentication() {
return new javax.mail.passwordauthentication(username,password);
}
}
存在的问题就是发送到163的邮件全部都带有一个附件的符号,不管有没有发送附件,感兴趣的朋友可以对此加以改进和完善。
希望本文所述对大家的java程序设计有所帮助。
上一篇: java实现两台服务器间文件复制的方法