public static void main(String[]args){
//MailUtils.sendMail("853739173@shop.com", "111111");
MailUtils.sendMailIter("15113500327@163.com", "11111");
}
/**
* 发送邮件的方法
*
* @param to
* 目标邮箱
* @param code
* 激活码
*/
public static void sendMailIter(String to, String code) {
/*
* 1.获得一个session对象 2.创建一个代表邮件的对象Message 3.发送邮件Transport
*/
final String smtpHostName = "smtp." + to.split("@")[1];
//1.获得连接对象
System.out.println(smtpHostName);
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", smtpHostName);
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("q853739173@sina.com", "zxglovezz");
}
});
//2.创建邮件对象
Message message=new MimeMessage(session);
//设置发件人:
try {
message.setFrom(new InternetAddress("q853739173@sina.com"));
//设置收件人
message.addRecipient(RecipientType.TO, new InternetAddress(to));
//抄送CC 密送BCC
//设置标题
message.setSubject("shop官方激活邮件");
//设置邮件正文:
//message.setContent("<h3>shop官方激活邮件!点下面链接完成激活操作!</h3><a href='http://192.168.16.215:8989/shop1/user_active.action?code="+code+"'>http://192.168.16.215:8989/shop1/user_active.action?code="+code+"</a>", "text/html;charset=UTF-8");
message.setContent("123456","text/html;charset=utf-8");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
//3.发送邮件:
try {
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
只有目标邮箱是新浪邮箱才可以接收到,
如果目标邮箱是163邮箱显示错误:535 Error: authentication failed
如果目标邮箱是qq邮箱显示错误:535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
如果只能是新浪邮箱那这个程序不是弱爆了吗?求教,悬赏悬赏!!!