import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class javaMailTest {
public static void main(String[] args) throws AddressException, MessagingException {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.smtp.host", "smtp.163.com"); //smtp 163的服务器地址
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "25"); //smtp 对于163默认开放的端口
Authenticator auth = new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("发送人邮箱用户名", "发送人邮箱密码"); }
};
Session session = Session.getInstance(props,auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("发送人邮箱地址"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress("接收人邮箱地址"));
// 设置主题
message.setSubject("邮件发送测试");
// 设置html内容
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
String context="<div>"
+ "<div>表格</div>"
+ "<style type='text/css'>"
+ "table,thead,tbody,th,td{border: 1px solid black;}"
+ "</style>"
+ "</div>"
+"<div>11111111111111111</div></br><div>"
+ "<table>"
+ "<thead>"
+ "<th>第一列</th> "
+ "<th>第二列</th>"
+ "<th>第二列</th>"
+ "</thead>"
+ "<tbody>"
+ "<td>1</td>"
+ "<td>2</td>"
+ "<td>3</td>"
+ "</tbody>"
+ "</table>"
+"</div>";
html.setContent(context, "text/html; charset=utf-8");
mainPart.addBodyPart(html);
message.setContent(mainPart);
message.saveChanges();
Transport.send(message);
}
}