代码如下:
public static void main(String args[]) throws AddressException, MessagingException { Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.sohu.com");//设置服务器 props.setProperty("mail.smtp.auth", "true");//是否需要认证 props.setProperty("mail.transport.protocol", "smtp");//邮件传输协议 Session session = Session.getDefaultInstance(props,new Authenticator() { @Override /** * 返回需要进行验证的用户名和密码 */ protected PasswordAuthentication getPasswordAuthentication() { // TODO Auto-generated method stub return new PasswordAuthentication("*********@sohu.com","*********"); } }); session.setDebug(true); Message message = new MimeMessage(session); message.setFrom(new InternetAddress("*********@sohu.com")); message.setRecipient(RecipientType.TO, new InternetAddress("*********@qq.com")); message.setSubject("subject"); message.setText("content"); Transport.send(message); }