关于javamail发带附件的问题?

四海为家110 2003-04-01 01:40:43
网上好多这种bean,但没有具体jsp以及例子,不知道如何处理附件?
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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也有,去搜

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧