当前位置: 技术问答>java相关
关于JAVAMAIL的问题
来源: 互联网 发布时间:2014-12-23
本文导语: 有谁会用JAVAX.MAIL发送带附件的邮件,有没有例子,给兄弟一个,万分感谢!! | See JavaMail demo!!!!!!!!!!!!!!!!!!!!!!! | 发送附件的MIME设为multipart/mixed 设置disposition为attachement 用setDataHandler设置...
有谁会用JAVAX.MAIL发送带附件的邮件,有没有例子,给兄弟一个,万分感谢!!
|
See JavaMail demo!!!!!!!!!!!!!!!!!!!!!!!
|
发送附件的MIME设为multipart/mixed
设置disposition为attachement
用setDataHandler设置文件内容
代码如下:
MimeMessage msg;
Multipart mp ;
FileDataSource fd = null;
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("attachment mail");
msg.setSentDate(new Date());
try{
fd = new FileDataSource("filename");
}
catch(Exception ee){
System.out.println("ee:"+ee.getMessage());
System.exit(1);
}
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("aaaaaa");
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setDataHandler(new DataHandler(fd));
mbp2.setDisposition(Part.ATTACHMENT);
mbp2.setFileName("filename");
mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
Transport.send(msg);
}
catch(MessagingException ee) {
ee.printStackTrace();
}
设置disposition为attachement
用setDataHandler设置文件内容
代码如下:
MimeMessage msg;
Multipart mp ;
FileDataSource fd = null;
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("attachment mail");
msg.setSentDate(new Date());
try{
fd = new FileDataSource("filename");
}
catch(Exception ee){
System.out.println("ee:"+ee.getMessage());
System.exit(1);
}
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("aaaaaa");
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setDataHandler(new DataHandler(fd));
mbp2.setDisposition(Part.ATTACHMENT);
mbp2.setFileName("filename");
mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
Transport.send(msg);
}
catch(MessagingException ee) {
ee.printStackTrace();
}