关于javamail发带附件的问题?

四海为家110 浙江际通天下科技有限公司 技术总监  2003-04-01 01:40:43
网上好多这种bean,但没有具体jsp以及例子,不知道如何处理附件?
...全文
18 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cgsun 2003-04-12
发送的
package mails;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class QuickMailAttach {
public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText,String fileName)
throws MessagingException{
//step 1:Configure the mail session
System.out.println("Configuring mail session for:"+smtpHost);
SmtpAuth sa=new SmtpAuth();
sa.getuserinfo("??","??");
sa.getPasswordAuthentication();
java.util.Properties props=new java.util.Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHost);
Session mailSession=Session.getDefaultInstance(props,sa);

//Step2:Construct the message
System.out.println("Constructing message -from="+from+" to="+to);
InternetAddress fromAddress=new InternetAddress(from);
InternetAddress toAddress=new InternetAddress(to);
MimeMessage testMessage=new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject);

//Step3:create a body part to hold the "text" portion of the message
System.out.println("Constructing 'text' body part");
MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText(messageText);

//Step 4:Create a body part to hold the "file" portion of the message
System.out.println("Attaching 'file' body part:"+fileName);
MimeBodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(fileName);
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
System.out.println("Finished attaching file");

//Step 5:Create a Multipart/container and add the parts
Multipart container=new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart);

//Step 6:Add the Multipart to the actual messge
testMessage.setContent(container);
System.out.println("Messge constructed");

//Step 7:Now send the message
Transport.send(testMessage);
System.out.println("Message sent.");


}

public static void main(String[] args){
String smtpHost="smtp.citiz.net";
String from="??@citiz.net";
String to="??@citiz.net";
String filename="D:/dd.txt";
String subject="Test message";
StringBuffer theMessage=new StringBuffer();
theMessage.append("ddddhello22,\n\n");
theMessage.append("Hope all is well on you end.\n");
theMessage.append("Cheers");
try{
QuickMailAttach.sendMessage(smtpHost,from,to,subject,theMessage.toString(),filename);
}catch(javax.mail.MessagingException exc){
exc.printStackTrace();
}
}

static class SmtpAuth extends javax.mail.Authenticator {
private String user,password;

public void getuserinfo(String getuser,String getpassword){
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
}

回复
四海为家110 2003-04-01
找了很久,没有找到亚,就是bean,jsp一起比较全的例子,没有亚
回复
Philzzf 2003-04-01
现成的JSP也有,去搜
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-01 01:40
社区公告
暂无公告