Java Mail 发送带附件邮件
@Override public String sendEmail(Mail mail) { try{ logger.debug("email props begin..."); // 配置发送邮件的环境属性 final Properties props = new Properties(); // 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host / mail.user / mail.from // 表示SMTP发送邮件,需要进行身份验证 props.put("mail.smtp.ssl.enable", true); props.put("mail.smtp.auth", mail.getConfig().getAuth()); props.put("mail.smtp.host", mail.getConfig().getHost()); props.put("mail.smtp.port", mail.getConfig().getPort()); // 发件人的账号 props.put("mail.user", mail.getConfig().getFrom()); // 访问SMTP服务时需要提供的密码 props.put("mail.password", mail.getConfig().getPwd()); logger.debug("email authenticator begin..."); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 logger.debug("email session begin..."); Session mailSession = Session.getInstance(props, authenticator); mailSession.setDebug(true); // 创建邮件消息 logger.debug("email message begin..."); MimeMessage message = new MimeMessage(mailSession); // 设置发件人 logger.debug("email from begin..."); InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form); // 设置收件人 logger.debug("email to begin..."); InternetAddress to = new InternetAddress(mail.getConfig().getTo()); message.setRecipient(Message.RecipientType.TO, to); // 设置抄送 if(!Utility.isEmpty(mail.getConfig().getCc())){ logger.debug("email cc begin..."); InternetAddress cc = new InternetAddress(mail.getConfig().getCc()); message.setRecipient(Message.RecipientType.CC, cc); } // 设置邮件标题 logger.debug("email subject begin..."); message.setSubject(mail.getSubject()); // 设置邮件的内容体 logger.debug("email content begin..."); Multipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(mail.getContent(), "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); if (mail.getAttachment() != null) { BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(mail.getAttachment()); attachmentBodyPart.setDataHandler(new DataHandler(source)); // 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定 // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码 //sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); //messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?="); //MimeUtility.encodeWord可以避免文件名乱码 attachmentBodyPart.setFileName(MimeUtility.encodeWord(mail.getAttachment().getName())); multipart.addBodyPart(attachmentBodyPart); } message.setContent(multipart); // 发送邮件 logger.debug("email send message begin..."); Transport.send(message); }catch (Exception e){ logger.debug("email exception e:{}", e.getMessage()); return e.getMessage(); }finally { logger.debug("email send message end!"); } return "success"; }