=========================注意事项=============================
使用自己邮箱的时候,一定要去邮箱设置里 开启邮件服务器,然后获取一个第三方客户端登陆密码,Java代码里就用第三方密码,不用以前的密码
=========================java代码如下==========================
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailUtil {
// 设置服务器
private static String KEY_SMTP = "mail.smtp.host";
private static String VALUE_SMTP = "smtp.163.com";
// 服务器验证
private static String KEY_PROPS = "mail.smtp.auth";
private static boolean VALUE_PROPS = true;
// 发件人用户名、密码
private String SEND_USER = "15566930303@163.com";
private String SEND_UNAME = "15566930303";
private String SEND_PWD = "libixin5210327";
// 建立会话
private MimeMessage message;
private Session s;
/*
* 初始化方法
*/
public SendMailUtil() {
Properties props = System.getProperties();
props.setProperty(KEY_SMTP, VALUE_SMTP);
props.put(KEY_PROPS, "true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port", "465");
s = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
}});
s.setDebug(true);
message = new MimeMessage(s);
}
/**
* 单发
*/
public void doSendHtmlEmail(String headName, String sendHtml,
String[] receiveUser) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
int receiverLen = receiveUser.length;
//群发
for (int i = 0; i < receiverLen; i++) {
InternetAddress toAddr = new InternetAddress(receiveUser[i]);
message.addRecipient(javax.mail.Message.RecipientType.TO,toAddr);
}
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 群发
*/
public void doSendHtmlEmail(String headName, String sendHtml,
String receiveUser) {
try {
// 发件人
InternetAddress from = new InternetAddress(SEND_USER);
message.setFrom(from);
//单发
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject(headName);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("send success!");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMailUtil se = new SendMailUtil();
//单发
se.doSendHtmlEmail("宝贝我爱你", "宝贝我爱你", "342097593@qq.com");
//群发
//String[] receiverAddr = {"415405800@qq.com","342097593@qq.com"};
//se.doSendHtmlEmail("宝贝我爱你", "宝贝我爱你", receiverAddr);
}
}