javax.mail.AuthenticationFailedException
发送邮件出现这个异常是为什么呢?是验证没有通过吗?
可是我用IE登陆邮箱的时候 都能登陆上去的啊
发送邮件出现这个异常是为什么呢?是验证没有通过吗?
可是我用IE登陆邮箱的时候 都能登陆上去的啊
解决方案 »
- 谁那里有java中的3Des加密资料啊??网上找的怎么和C加密结果不一样啊?
- 【HTML】如何动态生成文本框
- 用JCreator和Dreamweaver都出现的JSP问题,请教高人!!!
- 哪里有JSP的一些类的函数的帮助文件 ,类似于MSDN那中东西?
- SOS: tomcat下文件的读取
- tomcat的安装问题
- 提供j2ee的java代码加密方案
- 晕啊,谁愿意和偶讨论一下连接池得使用那?
- 使用poi-3.8生成excel报错org.apache.poi.ss.formula.eval.NotImplementedException: Error
- 怎样将向量中的数据转换成int
- 用“苹果茶”内网建站软件的问题
- weblogic 好像根本不编译我写的代码一样
指的是那个 stmp.qq.com smtp.sina.com
这个吗?
我都有 对比过的 下面是我写的代码 请帮忙看下
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sina.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true"); SuperAuthenticator ma = new SuperAuthenticator("my邮箱", "密码");
Session mailSession = Session.getDefaultInstance(props, ma);
mailSession.setDebug(true);
Message message = new MimeMessage(mailSession); message.setFrom(new InternetAddress("my邮箱"));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("to邮箱"));
message.setSubject("haha");
message.setText("text");
message.saveChanges();
Transport.send(message);
不保证能用呀 if (needAuth){
props.put("mail.smtp.auth", "true");
Authenticator auth = new MyPasswordAuthentication().getInstance(name,password); session = Session.getDefaultInstance(props, auth);// 创建session
}else{
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
}
class MyPasswordAuthentication extends Authenticator {
public MyPasswordAuthentication() { // TODO Auto-generated constructor stub
} String username = null; String password = null; PasswordAuthentication pa = null; public static MyPasswordAuthentication instance = null; public static synchronized MyPasswordAuthentication getInstance(String user, String pass) {
if (instance == null) {
instance = new MyPasswordAuthentication(user, pass);
}
return instance;
} public MyPasswordAuthentication(String user, String pass) {
username = user;
password = pass;
} public PasswordAuthentication getPasswordAuthentication() { if (pa == null) {
pa = new PasswordAuthentication(username, password);
} return pa;
}
}
不过即使是连接163,也是需要用有密码的方式连接。附上我自己正在用的代码。XXXX等是个人信息。能发信,收信没有做。package com.XXXXXX.common.mail;import java.util.Properties;import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;public class BaseMailMessage { protected Session mailSession; public BaseMailMessage() {
/* 初始化服务器环境 */
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.port", "25");
props.put("mail.user", "XXXXXX");
props.put("mail.password", "XXXXX");
props.put("mail.from", "XXXXXXXXXXX");
props.put("mail.smtp.auth", "true");
mailSession = Session.getDefaultInstance(props, null);
} public boolean sendMail(String to, String cc, String bcc, String title,
String text) {
Message msg = new MimeMessage(mailSession);
Properties props = mailSession.getProperties();
Transport transport = null;
try {
msg.setFrom(new InternetAddress((String) props.get("mail.from"))); msg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
if (cc != null) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress
.parse(cc));
}
if (bcc != null) {
msg.setRecipients(Message.RecipientType.BCC, InternetAddress
.parse(bcc));
}
msg.setSentDate(new java.util.Date());
msg.setSubject(title);
msg.setText(text); if (props.get("mail.user") == null) {
Transport.send(msg);
} else { transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"),
(String) props.get("mail.user"), (String) props
.get("mail.password"));
transport.sendMessage(msg, msg
.getRecipients(Message.RecipientType.TO));
if (cc != null) {
transport.sendMessage(msg, msg
.getRecipients(Message.RecipientType.CC));
}
if (bcc != null) {
transport.sendMessage(msg, msg
.getRecipients(Message.RecipientType.BCC));
}
}
return true;
} catch (AddressException e1) {
return false;
} catch (MessagingException e1) {
return false;
} finally {
if (transport != null) {
try {
transport.close();
} catch (MessagingException e) {
;
}
}
}
} public boolean sendMail(String to, String title, String text) {
return sendMail(to, null, null, title, text);
}
}