/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ssh.utity.AttatchEmail;
/**
*
* @author belon
*/
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class Mail {
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);
msg.setFrom(from_address);
}catch(Exception 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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SSH_Demo.rar (152个子文件)
.classpath.bak 6KB
Mail.class 7KB
UserwebAction.class 4KB
ValidationCodeAction.class 4KB
UserDAOImpl.class 3KB
SendMail.class 2KB
AthorizationIntercapter.class 2KB
SendMail.class 2KB
Users.class 1KB
UserServiceImpl.class 1KB
MD5.class 1KB
SaveUserAction.class 1KB
UpdataPUserAction.class 1KB
UpdataUserAction.class 1KB
SmtpAuth.class 663B
UserDAO.class 397B
UserService.class 384B
.classpath 7KB
SSH整合说明文档.doc 908KB
entries 696B
entries 696B
format 2B
format 2B
xwork-2.1.2.jar 2.18MB
hibernate3.jar 2.15MB
jasperreports-2.0.5.jar 1.87MB
aspectjweaver.jar 1.82MB
xwork-core-2.1.6.jar 1.49MB
iText-2.0.7.jar 1.15MB
xerces-2.6.2.jar 987KB
poi-3.0.1.jar 888KB
freemarker-2.3.13.jar 850KB
struts2-core-2.1.6.jar 719KB
jxl.jar 704KB
mysql-connector-java-5.1.7-bin.jar 693KB
c3p0-0.9.1.2.jar 596KB
spring-beans.jar 473KB
javassist.jar 460KB
spring-context.jar 458KB
mail.jar 451KB
antlr-2.7.6.jar 433KB
jboss-archive-browsing.jar 404KB
spring-webmvc.jar 393KB
velocity-1.5.jar 383KB
log4j-1.2.15.jar 383KB
spring-orm.jar 364KB
spring-aop.jar 318KB
cglib-nodep-2.1_3.jar 317KB
spring-jdbc.jar 310KB
dom4j-1.6.1.jar 307KB
commons-httpclient.jar 298KB
spring-core.jar 279KB
cglib-2.1.3.jar 276KB
hibernate-annotations.jar 259KB
mailapi.jar 244KB
commons-lang.jar 237KB
spring-tx.jar 222KB
jaxen-1.1-beta-7.jar 222KB
ehcache-1.2.3.jar 203KB
commons-beanutils.jar 184KB
spring-web.jar 183KB
commons-collections-2.1.1.jar 171KB
imap.jar 169KB
ognl-2.6.11.jar 164KB
spring-webmvc-portlet.jar 146KB
commons-email-1.2-javadoc.jar 144KB
jotm.jar 121KB
xml-apis.jar 121KB
commons-dbcp.jar 119KB
hibernate-entitymanager.jar 114KB
aspectjrt.jar 112KB
jaas.jar 102KB
xapool.jar 90KB
commons-io.jar 82KB
persistence.jar 69KB
hibernate-commons-annotations.jar 65KB
commons-pool.jar 61KB
hibernate-validator.jar 60KB
commons-logging.jar 59KB
commons-fileupload.jar 52KB
ejb3-persistence.jar 49KB
commons-codec.jar 46KB
smtp.jar 44KB
commons-logging-1.0.4.jar 37KB
spring-webmvc-struts.jar 36KB
commons-attributes-api.jar 35KB
pop3.jar 35KB
asm-util-2.2.3.jar 34KB
commons-email-1.2-sources.jar 33KB
commons-email-1.2.jar 32KB
commons-attributes-compiler.jar 29KB
asm.jar 26KB
spring-aspects.jar 20KB
asm-attrs.jar 16KB
portlet-api.jar 15KB
asm-commons-2.2.3.jar 15KB
struts2-spring-plugin-2.1.6.jar 9KB
jta.jar 9KB
struts2-pell-multipart-plugin-2.0.9.jar 9KB
spring-tomcat-weaver.jar 8KB
共 152 条
- 1
- 2
资源评论
- yongjiangb2012-11-03例子中简单明了,对学习SSH框架有一定帮助
tangweitong
- 粉丝: 6
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- mysql多表联合查询案例
- IMG20240609013653.jpg
- 分别通过BP神经网络和GRNN广义回归神经网络对电力负荷数据进行训练和预测matlab仿真,包括程序,注释,操作步骤
- 基于图像纹理特征提取的图像分割算法matlab仿真,包括程序,注释,操作步骤
- Spring Boot整合Redis通过Zset数据类型+定时任务实现延迟队列教程对应源码
- 创意互动:HTML+CSS+JS打造《三维端午节动态文字秀》大屏特效
- Python爬虫框架scrapy获取百度热搜
- IMG_20240609_015836.jpg
- 初音未来开机动画.zip
- 《创意互动:HTML+CSS+JS打造"儿童节"3D立体大屏动态文字动画》
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功