java 发送邮件 附件_java发送邮件(包含正文、附件)

1、pom.xml依赖包引入

com.sun.mail

javax.mail

1.5.5

2、email.properties文件

#email

mail.smtp.host=smtp.mxhichina.com

mail.smtp.socketFactory.port=465

mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

mail.smtp.auth=true

mail.smtp.port=465

fromEmail=XX@XX.com

password=XXXXXXX

toEmail=XX@qq.com,XX@XX.com

3、前期工作

//创建Properties对象

Properties props = new Properties();

try {

//ClassUtils.getDefaultClassLoader().getResource("").getPath()获取项目地址

props.load(new FileInputStream(ClassUtils.getDefaultClassLoader().getResource("").getPath() + File.separator + "email.properties"));

} catch (Exception e) {

logger.error(e.getMessage(), e);

}

final String fromEmail = props.getProperty("fromEmail");//邮件发起人

final String password = props.getProperty("password");//邮件发起人密码

final String toEmail = props.getProperty("toEmail"); //接收人邮件

//获得网络连接验证的对象

Authenticator auth = new Authenticator() {

//override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(fromEmail, password);

}

};

//得到默认的对话对象

Session session = Session.getDefaultInstance(props, auth);

//标题

String subject = "";

//内容

StringBuilder temp = new StringBuilder();

String filePath = "";

//发送邮件

EmailUtil.sendEmailWithAttachment(session,fromEmail,toEmail,subject,body,filePath);

4、EmailUtil工具类

/**

* send HTML email with Attachment

* @param session

* @param fromEmail

* @param toEmail

* @param subject

* @param body

* @param fileName

*/

public static void sendEmailWithAttachment(Session session, String fromEmail, String toEmail, String subject, String body, String fileName) throws Exception{

//消息创建

MimeMessage msg = new MimeMessage(session);

//set message headers

msg.addHeader("Content-type", "text/HTML; charset=UTF-8");

msg.addHeader("format", "flowed");

msg.addHeader("Content-Transfer-Encoding", "8bit");

msg.setFrom(new InternetAddress(fromEmail));

// 创建邮件的接收者地址,并设置到邮件消息中

Address[] toEmails = null;

if(toEmail.indexOf(",")>-1) {

String[] emailList = toEmail.split(",");

toEmails= new InternetAddress[emailList.length];

for (int i = 0; i < emailList.length; i ++) {

toEmails[i] = new InternetAddress(emailList[i]);

}

} else {

toEmails= new InternetAddress[1];

toEmails[0] = new InternetAddress(toEmail);

}

msg.setRecipients(Message.RecipientType.TO,toEmails);

msg.setSentDate(new Date());

//标题

msg.setSubject(subject,"UTF-8");

//创建body

MimeMultipart allBodyPart = new MimeMultipart("mixed");

//创建正文html

MimeBodyPart htmlBodyPart = new MimeBodyPart();

htmlBodyPart.setContent(body,"text/html;charset=gb2312");

allBodyPart.addBodyPart(htmlBodyPart);

//创建附件

MimeBodyPart attachBodyPart = new MimeBodyPart();

FileDataSource source = new FileDataSource(fileName);

attachBodyPart.setDataHandler(new DataHandler(source));

//不带路径文件名称

File tempFile =new File(fileName.trim());

attachBodyPart.setFileName(tempFile.getName());

allBodyPart.addBodyPart(attachBodyPart);

//设置整个邮件内容为最终组合出的MimeMultipart对象

msg.setContent(allBodyPart);

//发送邮件

Transport transport = session.getTransport("smtp");

//transport.connect("smtp.mxhichina.com" , 465 , fromEmail, password);链接邮件服务器

transport.send(msg);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值