public static void main(String args[]){
sendEmail("XXX@qq.com", "你好", "ss");
}
public static void sendEmail(String touser,String title,String content){
String host = "smtp.exmail.qq.com";// 邮件服务器地址
String addresser = "service@XX.cn";// 发件人地址
String username = "XXX";// 发件人邮箱用户名
String password = "XXXXXX";// 发件人邮箱密码
try {
Properties props = new Properties();
props.put("mail.smtp.host", host);// 设置smtp的邮件服务器地址
props.put("mail.smtp.auth", "true");// 注意的是此处必须加上true要不然smtp连接的时候不会认证
props.put("mail.transport.protocol", "smtp"); // 设置邮件的传输协议是smtp
MyAuthenticator auth = new MyAuthenticator(username, password);
// Session类定义了基本的邮件会话
Session session = Session.getDefaultInstance(props, auth);// 创建一个Session
MimeMessage message = new MimeMessage(session);
// 设置发件人邮件地址
message.setFrom(new InternetAddress(addresser));
// 设置收件人邮件地址 Message.RecipientType.TO 设置发送的类型
message.setRecipient(Message.RecipientType.TO,
........
错误信息
serviceddservice...
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at com.jingdu.common.Function.sendEmail(Function.java:89)
at com.jingdu.common.Function.main(Function.java:54)
------解决方案--------------------------------------------------------
http://blog.csdn.net/zyuc_wangxw/article/details/6911741
我写过这个,这个地址有源码