//参考菜鸟教程javaMail代码



package com.tgb;

//文件名 SendEmail.java

import java.security.GeneralSecurityException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

import java.util.logging.SimpleFormatter;


import javax.mail.Address;

import javax.mail.Authenticator;

import javax.mail.Message;

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 com.sun.mail.util.MailSSLSocketFactory;

/*

*需要先开启自己的邮箱的POP3/SMTP服务

*/

public class SendEmail {

public static void main(String[] args) throws MessagingException, GeneralSecurityException {

Properties props = new Properties();

       // 开启debug调试

       props.setProperty("mail.debug", "true");

       // 发送服务器需要身份验证

       props.setProperty("mail.smtp.auth", "true");

       // 设置邮件服务器主机名

       props.setProperty("mail.host", "smtp.qq.com");

       // 发送邮件协议名称

       props.setProperty("mail.transport.protocol", "smtp");


       MailSSLSocketFactory sf = new MailSSLSocketFactory();

       sf.setTrustAllHosts(true);

       props.put("mail.smtp.ssl.enable", "true");

       props.put("mail.smtp.ssl.socketFactory", sf);


       Session session = Session.getInstance(props);


       Message msg = new MimeMessage(session);

       msg.setSubject("i'm xxx");

       msg.setSubject("这是我的一个java程序发出的邮件,如果你收到此邮件,那么我成功了");

       StringBuilder builder = new StringBuilder();

       builder.append("hello,");

       SimpleDateFormat simpleFormatter=new SimpleDateFormat();

       Date date=new Date();

       String d=simpleFormatter.format(date);

       builder.append("\n时间 " + d);

       msg.setText(builder.toString());

       msg.setFrom(new InternetAddress("xxx@qq.com"));          //发送邮箱


       Transport transport = session.getTransport();

       transport.connect("smtp.qq.com", "发送邮箱", "whgaugxmwviteach");

                //1.发送邮箱的邮件服务器,2.发送邮箱,3.授权码

       transport.sendMessage(msg, new Address[] { new InternetAddress(" 2290039123@qq.com") });       //收件箱

       transport.close();

   }

}