转自:无法查证。。
很直接的一段代码 拿来就用 QQ邮箱测试通过 126的不知道为何不行 其他的没试
文中两个包的下载地址
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
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, "这个不知道干什么用的。。");
Address addressTo = new InternetAddress("10000@qq.com", "这个不知道干什么用的。。");//发送地址
message.setSubject("邮件题目");
message.setText("邮件内容");
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 = "myQQ@qq.com";//登录帐号
public static final String password = "myPWD";//登录密码
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailuser, password);
}
}