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 是一个强大的开源库,用于在Java应用程序中发送和接收电子邮件。这个资源提供了一个具体的示例,展示了如何使用JavaMail API 来发送带有附件的邮件。
Gmail邮件通过JavaMail发送(不带附件与带一个附件)
本教程将详细讲解如何使用JavaMail API在Gmail上发送邮件,包括不带附件和带有一个附件的情况。首先,JavaMail是一个开源的Java库,它提供了用于处理邮件的标准API。
javamail实现邮件发送(支持多接收人、多附件)
本教程将详细讲解如何使用 JavaMail 实现邮件发送功能,包括支持多个接收人、抄送人以及添加多个附件。首先,确保你已经添加了 JavaMail 的依赖库到你的项目中。
javaMail 用三种方式实现发送附件
javaMail 用附件路径,文件流 byte[] 或 base64 字符串 实现发送附件功能
java 发邮件带excel附件,以流的形式发送附件,不生成excel文件
Transport.send(message);```以上代码片段展示了如何在不生成临时Excel文件的情况下,使用JavaMail API和Apache POI发送带有Excel附件的邮件。


Java SE
