当前位置: 技术问答>java相关
如何实现带发附件的javabean?
来源: 互联网 发布时间:2015-05-30
本文导语: 以下是我写的一个javabean,能发电子邮件,但不能发附件,本意是从表单中传入 上传的文件的路径,在本代码中变量是loadfile. 请高手帮忙看看,以能实现这个 javabean能发附件? //:SendMailbean.java; //用来发送电子邮件的...
以下是我写的一个javabean,能发电子邮件,但不能发附件,本意是从表单中传入
上传的文件的路径,在本代码中变量是loadfile. 请高手帮忙看看,以能实现这个
javabean能发附件?
//:SendMailbean.java;
//用来发送电子邮件的javabean
package zhnbean.mailbean;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailbean {
//设置输入输出时的变量;
private String from;
private String to;
private String forward;
private String subject;
private String text;
private String loadfile;
public SendMailbean()
{
from="";
to="";
forward="";
subject="";
text="";
loadfile="";
}
//设置输入、输出时都要使用的变量的方法
public void setFrom(String from)
{
this.from = from;
}
public String getFrom()
{
return this.from;
}
public void setTo(String to)
{
this.to = to;
}
public String getTo()
{
return this.to;
}
public void setForward(String forward)
{
this.forward = forward;
}
public String getForward()
{
return this.forward;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getSubject()
{
return this.subject;
}
public void setText(String text)
{
this.text = text;
}
public String getText()
{
return this.text;
}
public void setLoadfile(String loadfile)
{
this.loadfile = loadfile;
}
public String getLoadfile()
{
return this.loadfile;
}
/**************************************
* 发送邮件的方法
* 如果发送成功,就返回TRUE,若失败,就返回FLASE
**************************************/
public boolean sendmail()
{
try{
//设定你的smtp主机站点(不需发送认证的主机如:smtp.huptt.zj.cn)
String host ="smtp.huptt.zj.cn";
//smtp设定
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
//Session 生成和 MimeMessage 生成
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
//发信人
message.setFrom(new InternetAddress(this.from));
//收件人(或包括转发人)
if ( forward.equals("")){
//forward为空,即没有转发人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
}
else {
Address toAddress = new InternetAddress(this.to);
Address ccAddress = new InternetAddress(this.forward);
message.addRecipient(Message.RecipientType.TO,toAddress);
message.addRecipient(Message.RecipientType.CC,ccAddress);
}
//设置主题
message.setSubject(this.subject);
//设置正文及是否加附件
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(this.text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
/*判断有没有附件,有的话,将附件作为一个部件,并将它附加到BodyPart*/
if ( loadfile != "" ){
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(this.loadfile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(this.loadfile); //保留附件中的原始文件名
multipart.addBodyPart(messageBodyPart);
}
//将各部件都放入消息message中
message.setContent(multipart);
//发送邮件
Transport.send(message);
//邮件发送成功,返回TURE
return true;
}catch(AddressException addr_e){
System.out.println(addr_e.getMessage());
return false;
}catch(MessagingException msg_e){
System.out.println(msg_e.getMessage());
return false;
}
}
}
上传的文件的路径,在本代码中变量是loadfile. 请高手帮忙看看,以能实现这个
javabean能发附件?
//:SendMailbean.java;
//用来发送电子邮件的javabean
package zhnbean.mailbean;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailbean {
//设置输入输出时的变量;
private String from;
private String to;
private String forward;
private String subject;
private String text;
private String loadfile;
public SendMailbean()
{
from="";
to="";
forward="";
subject="";
text="";
loadfile="";
}
//设置输入、输出时都要使用的变量的方法
public void setFrom(String from)
{
this.from = from;
}
public String getFrom()
{
return this.from;
}
public void setTo(String to)
{
this.to = to;
}
public String getTo()
{
return this.to;
}
public void setForward(String forward)
{
this.forward = forward;
}
public String getForward()
{
return this.forward;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public String getSubject()
{
return this.subject;
}
public void setText(String text)
{
this.text = text;
}
public String getText()
{
return this.text;
}
public void setLoadfile(String loadfile)
{
this.loadfile = loadfile;
}
public String getLoadfile()
{
return this.loadfile;
}
/**************************************
* 发送邮件的方法
* 如果发送成功,就返回TRUE,若失败,就返回FLASE
**************************************/
public boolean sendmail()
{
try{
//设定你的smtp主机站点(不需发送认证的主机如:smtp.huptt.zj.cn)
String host ="smtp.huptt.zj.cn";
//smtp设定
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
//Session 生成和 MimeMessage 生成
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
//发信人
message.setFrom(new InternetAddress(this.from));
//收件人(或包括转发人)
if ( forward.equals("")){
//forward为空,即没有转发人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
}
else {
Address toAddress = new InternetAddress(this.to);
Address ccAddress = new InternetAddress(this.forward);
message.addRecipient(Message.RecipientType.TO,toAddress);
message.addRecipient(Message.RecipientType.CC,ccAddress);
}
//设置主题
message.setSubject(this.subject);
//设置正文及是否加附件
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(this.text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
/*判断有没有附件,有的话,将附件作为一个部件,并将它附加到BodyPart*/
if ( loadfile != "" ){
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(this.loadfile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(this.loadfile); //保留附件中的原始文件名
multipart.addBodyPart(messageBodyPart);
}
//将各部件都放入消息message中
message.setContent(multipart);
//发送邮件
Transport.send(message);
//邮件发送成功,返回TURE
return true;
}catch(AddressException addr_e){
System.out.println(addr_e.getMessage());
return false;
}catch(MessagingException msg_e){
System.out.println(msg_e.getMessage());
return false;
}
}
}
|
你再发邮件的程序中再写一个文件上传的程序不就可以吗?文件上传可以用jspsmart