|
|
@@ -58,33 +58,27 @@ public class SendMailUtils {
|
|
|
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
|
|
|
// Set Subject: 主题文字
|
|
|
message.setSubject(subject);
|
|
|
- // 创建消息部分
|
|
|
- BodyPart messageBodyPart = new MimeBodyPart();
|
|
|
- // 消息
|
|
|
- messageBodyPart.setText(msg);
|
|
|
- // 创建多重消息
|
|
|
MimeMultipart multipart = new MimeMultipart("mixed");
|
|
|
|
|
|
- if(StringUtils.isNotEmpty(filename)){
|
|
|
+ MimeBodyPart htmlPart = new MimeBodyPart();
|
|
|
+ htmlPart.setContent(msg, "text/html;charset=utf-8");
|
|
|
+ multipart.addBodyPart(htmlPart);
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(filename)) {
|
|
|
File file = new File(filename);
|
|
|
- if(file.exists()){
|
|
|
- messageBodyPart = new MimeBodyPart();
|
|
|
- DataSource source = new FileDataSource(filename);
|
|
|
- messageBodyPart.setDataHandler(new DataHandler(source));
|
|
|
- messageBodyPart.setFileName(MimeUtility.encodeText(file.getName(), "UTF-8", "B"));
|
|
|
- multipart.addBodyPart(messageBodyPart);
|
|
|
+ if (file.exists()) {
|
|
|
+ MimeBodyPart attachmentPart = new MimeBodyPart();
|
|
|
+ DataSource source = new FileDataSource(file);
|
|
|
+ attachmentPart.setDataHandler(new DataHandler(source));
|
|
|
+ attachmentPart.setFileName(
|
|
|
+ MimeUtility.encodeText(file.getName(), "UTF-8", "B")
|
|
|
+ );
|
|
|
+ multipart.addBodyPart(attachmentPart);
|
|
|
}
|
|
|
}
|
|
|
- //html代码部分
|
|
|
- MimeBodyPart htmlPart = new MimeBodyPart();
|
|
|
- multipart.addBodyPart(htmlPart);
|
|
|
- //html代码
|
|
|
- htmlPart.setContent(msg, "text/html;charset=utf-8");
|
|
|
- // 发送完整消息
|
|
|
+
|
|
|
message.setContent(multipart);
|
|
|
- // 发送消息
|
|
|
- Transport.send(message, new Address[]{new InternetAddress(receive)});
|
|
|
- // System.out.println("Sent message successfully....");
|
|
|
+ Transport.send(message);
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|