Java发送邮件功能
package com.hd.all.test.testjava; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Mail { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.transport.protocol", "smtp"); //协议 prop.setProperty("mail.smtp.host", "smtp.163.com"); //主机名 prop.setProperty("mail.smtp.auth", "true"); //是否开启权限控制 prop.setProperty("mail.debug", "true"); //返回发送的cmd源码 Session session = Session.getInstance(prop); Message msg = new MimeMessage(session); Address add1 = new InternetAddress(); Address[] address = {new InternetAddress("hui.ji@hdvalve.com"),new InternetAddress("xiang.yin@hdvalve.com")}; msg.setFrom(new InternetAddress("17712060812@163.com","jihui")); //自己的email,后面的是别名 msg.setRecipients(RecipientType.TO, address); // 要发送的email,可以设置数组 msg.setSubject("合同评审流程问题"); //邮件标题 msg.setText("123jihui");//邮件正文 //不被当作垃圾邮件的关键代码--Begin ,如果不加这些代码,发送的邮件会自动进入对方的垃圾邮件列表 msg.addHeader("X-Priority", "3"); msg.addHeader("X-MSMail-Priority", "Normal"); msg.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); //本文以outlook名义发送邮件,不会被当作垃圾邮件 msg.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); msg.addHeader("ReturnReceipt", "1"); //不被当作垃圾邮件的关键代码--end Transport trans = session.getTransport(); trans.connect("17712060812", "密码"); // 邮件的账号密码,账号不要@及后面的内容,密码是授权码 trans.sendMessage(msg, msg.getAllRecipients()); } }