依赖包:mail.jar
JAR链接地址: http://pan.baidu.com/s/1o8LNl0Y 密码: ja52
package com.dava; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.BodyPart; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class sendEmal { public static void main(String[] args) { String content="尊敬的客户,您好:<br> 您的退换货申请已由博乐宝客服受理,现需您将机器故障照片及检测结果等附件直接回复至此邮箱。我们收到您的邮件后会尽快为您处理。<br>如有任何问题,请致电。感谢您的配合与支持!"; try { System.out.println("开始发送邮件!"); Properties props = new Properties(); props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.auth", true); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("yangxinghe_it@163.com")); message.setSubject("测试邮件标题");// 邮件标题 Multipart mp = new MimeMultipart("related"); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=UTF-8")); mp.addBodyPart(bodyPart); message.setContent(mp);// 设置邮件内容对象 message.setSentDate(new java.util.Date(System.currentTimeMillis())); message.saveChanges(); message.addRecipient(RecipientType.TO, new InternetAddress( "1049232220@qq.com")); Transport transport = session.getTransport("smtp"); transport.connect("smtp.163.com", "yangxinghe_it@163.com", ".dava4978$."); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("发送邮件结束!"); } }
一步一步行走,做好每一件事情。