使用Oracle官方的JavaMail进行实现,JavaMail下载地址:​​https://java.net/projects/javamail/pages/Home​

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

public class SimpleMail {
public static void main(String[] args) {
Properties props = new Properties();
//这里使用smtp协议发送邮件。我的新浪邮箱是.cn的不是.com的,所以smtpserver为smtp.sina.cn
props.put("mail.smtp.host", "smtp.sina.cn");
Session session = Session.getInstance(props, null);

try {
MimeMessage msg = new MimeMessage(session);
//设置发件人邮箱
msg.setFrom("theonegis@sina.cn");
//设置收件人邮箱
msg.setRecipients(Message.RecipientType.TO, "123456789@qq.com");
//设置主题
msg.setSubject("This is a test");
//设置日期
msg.setSentDate(new Date());
//设置正文内容
msg.setText("How are you?\nThis is a test, please do not reply!");
//发送邮件,參数为邮件信息,发件人邮箱和发件人邮箱password
Transport.send(msg, "theonegis@sina.cn", "这里是发件人的password");
} catch (MessagingException mex) {
System.err.println("Send failed! Exception: " + mex);
}
}
}


JavaMail中比較重要的的类是Session、Store和Folder。