javamail发送附件不通过也不报错
发送附件的代码如下,使用的是struts,所以附件的上传是使用FormFile(mailForm)
public void sendFile(MailForm mailForm,boolean debug) throws Exception
{
String from = mailForm.getFrom();
String to = mailForm.getTo();
String userName = mailForm.getUserName();
String password = mailForm.getPassword();
FormFile file = mailForm.getFile();
String content = mailForm.getContent();
String mailServer = this.getDomain(from);
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
if(debug)
{
session.setDebug(debug);
}
Message msg = new MimeMessage(session);
BodyPart filePart = new MimeBodyPart();
String fileName = mailForm.getFile().getFileName();
//这个ByteDataSource是自己实现的DataSource
ByteDataSource dataSource = new ByteDataSource(file);
filePart.setDataHandler(new DataHandler(dataSource));
filePart.setFileName(fileName);
Multipart mulPart = new MimeMultipart();
mulPart.addBodyPart(filePart);
filePart = new MimeBodyPart();
filePart.setContent(content, "text/html;charset=gb2312");
mulPart.addBodyPart(filePart);
msg.setContent(mulPart);
msg.setSubject(mailForm.getSubject());
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSentDate(new Date());
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect(userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
public void sendFile(MailForm mailForm,boolean debug) throws Exception
{
String from = mailForm.getFrom();
String to = mailForm.getTo();
String userName = mailForm.getUserName();
String password = mailForm.getPassword();
FormFile file = mailForm.getFile();
String content = mailForm.getContent();
String mailServer = this.getDomain(from);
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
if(debug)
{
session.setDebug(debug);
}
Message msg = new MimeMessage(session);
BodyPart filePart = new MimeBodyPart();
String fileName = mailForm.getFile().getFileName();
//这个ByteDataSource是自己实现的DataSource
ByteDataSource dataSource = new ByteDataSource(file);
filePart.setDataHandler(new DataHandler(dataSource));
filePart.setFileName(fileName);
Multipart mulPart = new MimeMultipart();
mulPart.addBodyPart(filePart);
filePart = new MimeBodyPart();
filePart.setContent(content, "text/html;charset=gb2312");
mulPart.addBodyPart(filePart);
msg.setContent(mulPart);
msg.setSubject(mailForm.getSubject());
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSentDate(new Date());
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect(userName, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
...全文
相关推荐
javamail发送邮件(带附件)
该资源是一个javamail发送邮件的例子,里面集成了别人的文件发送的功能,单次发送邮件只能上传一个附件。是一个测试方法,拿过来改一改邮件的地址和密码就可以使用了
Gmail邮件通过JavaMail发送(不带附件与带一个附件)
附件是所需要的包文件 博文链接:https://xagcdx.iteye.com/blog/1039425
javamail实现邮件发送(支持多接收人、多附件)
基于javamail1.4实现邮件发送,支持多收件人、抄送人、多附件等
javaMail 用三种方式实现发送附件
javaMail 用附件路径,文件流 byte[] 或 base64 字符串 实现发送附件功能
java 发邮件带excel附件,以流的形式发送附件,不生成excel文件
java 发邮件带excel附件,以流的形式发送附件,不生成excel文件


Java SE
