当我看到控制台输出"sent suc!"的时候小小激动了一下,倒腾了快一天,终于把第一封邮件发出去勒! //此实例使用qq的邮箱服务器 //如果想复制此示例请更更改代码中的中文部分 import java.util.Properties; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class sentMail { public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host","smtp.qq.com"); props.put("mail.smtp.auth","true"); try { PopupAuthenticator auth = new PopupAuthenticator(); Session session = Session.getInstance(props, auth); session.setDebug(true); MimeMessage message = new MimeMessage(session); Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser+"@qq.com", "your name"); Address addressTo = new InternetAddress("输入接收邮箱", "your name"); message.setText("Test sent Mail"); message.setSubject("Test sent Mail by javaMail"); message.setFrom(addressFrom); message.addRecipient(Message.RecipientType.TO,addressTo); message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password); transport.send(message); transport.close(); System.out.println("sent suc"); } catch (Exception e) { System.out.println(e.toString()); System.out.println("sent fail"); } } } class PopupAuthenticator extends Authenticator { public static final String mailuser="你的qq号"; public static final String password="你的qq密码"; public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mailuser,password); } } import java.util.Properties; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class sentMail { public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host","smtp.qq.com"); props.put("mail.smtp.auth","true"); try { PopupAuthenticator auth = new PopupAuthenticator(); Session session = Session.getInstance(props, auth); session.setDebug(true); MimeMessage message = new MimeMessage(session); Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser+"@qq.com", "your name"); Address addressTo = new InternetAddress("输入接收邮箱", "your name"); message.setText("Test sent Mail"); message.setSubject("Test sent Mail by javaMail"); message.setFrom(addressFrom); message.addRecipient(Message.RecipientType.TO,addressTo); message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password); transport.send(message); transport.close(); System.out.println("sent suc"); } catch (Exception e) { System.out.println(e.toString()); System.out.println("sent fail"); } } } class PopupAuthenticator extends Authenticator { public static final String mailuser="你的qq号"; public static final String password="你的qq密码"; public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mailuser,password); } }