public static void main(String[] args) {
String mailServer, From, To, mailSubject, MailContent;
String username, password;
Session mailSession;
Properties prop;
Message message;
username = "kakaxi"; //
password = "kakaxi";
mailServer = "localhost";
From = "kakaxi@localhost";
To = "mingren@localhost";
mailSubject = "Hello Scientist";
MailContent = "How are you today!";
System.out.println("sending......");
prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", mailServer);
// 产生新的Session服务
//mailSession = mailSession.getDefaultInstance(prop,
// (Authenticator) mailauth);
mailSession=Session.getInstance(prop);
message = new MimeMessage(mailSession);
try {
message.setFrom(new InternetAddress(From)); // 设置发件人
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(To));// 设置收件人
message.setSubject(mailSubject);// 设置主题
message.setContent(MailContent, "text/plain");// 设置内容
message.setSentDate(new Date());// 设置日期
Transport tran = mailSession.getTransport("smtp");
tran.connect(mailServer, username, password);
tran.send(message, message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finished!");
}}
大家看看这段程序没问题吧。。我用james邮件服务器测试的时候总是抛这个javax.mail.AuthenticationFailedException
大家谁惹有能调的通的程序发小弟一用,,
String mailServer, From, To, mailSubject, MailContent;
String username, password;
Session mailSession;
Properties prop;
Message message;
username = "kakaxi"; //
password = "kakaxi";
mailServer = "localhost";
From = "kakaxi@localhost";
To = "mingren@localhost";
mailSubject = "Hello Scientist";
MailContent = "How are you today!";
System.out.println("sending......");
prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", mailServer);
// 产生新的Session服务
//mailSession = mailSession.getDefaultInstance(prop,
// (Authenticator) mailauth);
mailSession=Session.getInstance(prop);
message = new MimeMessage(mailSession);
try {
message.setFrom(new InternetAddress(From)); // 设置发件人
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(To));// 设置收件人
message.setSubject(mailSubject);// 设置主题
message.setContent(MailContent, "text/plain");// 设置内容
message.setSentDate(new Date());// 设置日期
Transport tran = mailSession.getTransport("smtp");
tran.connect(mailServer, username, password);
tran.send(message, message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finished!");
}}
大家看看这段程序没问题吧。。我用james邮件服务器测试的时候总是抛这个javax.mail.AuthenticationFailedException
大家谁惹有能调的通的程序发小弟一用,,
http://topic.csdn.net/u/20081024/17/81c59ed7-ac70-485e-b4c9-145464dd0b59.html上千次点击率见证,下面的程序没问题
http://blog.csdn.net/sunyujia/archive/2008/06/10/2528696.aspx
可尝试的解决方法:mailSession=Session.getInstance(prop);
改为:
mailSession=Session.getInstance(prop, new MyAuthenticator()); 然后创建一个MyAuthenticator类如下:
public class MyAuthenticator extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("yourUserName", "yourPassword");//这两个参数就是你邮箱smtp身份验证的账号和密码
}
}
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 dXNlcm5hbWU6
dG9tY3Rlc3Q=
334 UGFzc3dvcmQ6
YXNkZjEzMzIwNQ==
550 用户被锁定
Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.163.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1138)
at org.apache.commons.mail.Email.send(Email.java:1163)
at TestMail.sendMain(TestMail.java:16)
at TestMail.main(TestMail.java:20)
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1128)
... 3 more
*/
你恢复正身了.
是呀,偶又被放出来了,哇嘎嘎。There is no accident!