import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
?
/**
?* ClassName: ZhJavaMail <br/>
?* Function: JavaMail发送邮件 <br/>
?* date: 2013-11-05 <br/>
?*
?* @author zhanghao06
?* @version?
?* @since JDK 1.6
?*/
?
?
public class ZhJavaMail?
{
public static void main(String[] args) throws Exception?
{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");//必须 普通客户端
props.setProperty("mail.transport.protocol", "smtp");//必须选择协议
Session session=Session.getInstance(props, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication("邮箱用户名", "密码");
}
});
session.setDebug(true);//设置debug模式 ? 在控制台看到交互信息
Message msg = new MimeMessage(session); ?//建立一个要发送的信息
String context = new String();
?
context+="<html>";
context+="<body>";
context+="<html><body><table border='1'><tr><th>Month</th><th>Savings</th></tr>";
context+="<tr><td>January</td><td>$100</td></tr>";
context+="</body>";
context+="</html>";
?
//(1)设置简单的发送纯文本内容
//msg.setText(context);
?
//(2)发送html内容,以发送表格为例
msg.setContent(context, "text/html;charset=gb2312");
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
msg.setSubject("邮件Title["+formatter.format(new Date())+"]");
msg.setFrom(new InternetAddress("邮箱账号"));//发件人邮箱号
Transport transport = session.getTransport();//发送信息的工具
transport.connect("smtp.exmail.qq.com", 25, "发件人邮箱", "密码");//发件人邮箱号 和密码
transport.sendMessage(msg, new Address[] { new InternetAddress("收件人邮箱") });//对方的地址
transport.close();
}
}