package com.javamail; import java.util.Properties; 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.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class JavaMailDoMain2 { /** * @param args * @throws MessagingException * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Properties props=new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.host", "smtp.qq.com"); Session session=Session.getInstance(props,new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { // TODO Auto-generated method stub return new PasswordAuthentication("登录名","登录密码"); } }); session.setDebug(true);//调试的时候使用的 Message megs=new MimeMessage(session); megs.setFrom(new InternetAddress("发送人邮箱地址")); megs.setSubject("来自田智文的问候~~!"); megs.setContent("<span style="color:red" mce_style="color:red">新年快乐,身体健康</span>", "text/html;charset=gbk"); megs.setRecipients(RecipientType.TO, InternetAddress.parse("目标邮箱地址,目标邮箱地址")); Transport.send(megs); } }