package cn.ming.javamail;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class Demo1 { public static void main(String[] args) throws Exception {// 创建邮件 Properties props=System.getProperties();// 设置SMTP主机 props.put("mail.smtp.host","smtp.sina.com");// 设置SMTP验证属性 props.put("mail.smtp.auth", "true");// 获得邮件会话对象 MyAuthenticator auth=new MyAuthenticator("ming_foryou","********"); Session session=Session.getInstance(props, auth);//getDefaultInstance每次共用,当要多次调用session且不同用getInstance session.setDebug(true);//调试信息 // 创建MIME邮件对象 MimeMessage msg=new MimeMessage(session);// 设置发件人 msg.setFrom(new InternetAddress("ming_foryou@sina.com"));// 设置收件人 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("ming_foryou@sohu.com"));// 设置邮件主题 msg.setSubject("test");// 设置正文 msg.setText("How do you do?"); // 发送消息 //创建SMTP邮件发送对象 Transport transport=session.getTransport("smtp");// 获取与邮件服务器的连接 transport.connect((String)props.getProperty("mail.smtp.host"), "ming_foryou@sina.com", "********");// 通过服务器发送邮件 transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));// 关闭 transport.close(); }}