java发送邮件
package com.leyou;
import java.util.Properties;
import javax.mail.Authenticator;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class youxiangTest {
public static void main(String[] args) throws AddressException,MessagingException {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "587");
props.put("mail.user", "229236940@qq.com");
props.put("mail.password", "授权码");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession);
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
InternetAddress to = new InternetAddress("1412744582@qq.com");
message.setRecipient(RecipientType.TO, to);
message.setSubject("标题");
message.setContent("内容", "text/html;charset=UTF-8");
Transport.send(message);
}
}