import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;public class MailExample { public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "你自己的电子信箱"; //发邮件的出发地(发件人的信箱)
String to = "收件人信箱"; //发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("你自己的电子信箱", "你自己的信箱密码");
Session session = Session.getDefaultInstance(props, myauth);// session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from)); // Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set the subject
message.setSubject("测试程序!"); // Set the content
message.setText("这是用java写的发送电子邮件的测试程序!"); message.saveChanges(); Transport.send(message);
}
}校验发信人权限的方法
package com.hyq.test;import javax.mail.PasswordAuthentication;class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
这是我在网上找到的代码,说是可以用的,但是不能用哦!报告的错误是:
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.hyq.test.MailExample.main(MailExample.java:48)
没有权限验证,所以不能发送,可是上面明明有我自己的邮箱和密码啊,怎么还是不行呢》》》》有做过邮件开发系统的吗,帮小弟个忙吧,,,,,,,,,,
import javax.mail.*;
import javax.mail.internet.*;public class MailExample { public static void main (String args[]) throws Exception {
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "你自己的电子信箱"; //发邮件的出发地(发件人的信箱)
String to = "收件人信箱"; //发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("你自己的电子信箱", "你自己的信箱密码");
Session session = Session.getDefaultInstance(props, myauth);// session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from)); // Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to)); // Set the subject
message.setSubject("测试程序!"); // Set the content
message.setText("这是用java写的发送电子邮件的测试程序!"); message.saveChanges(); Transport.send(message);
}
}校验发信人权限的方法
package com.hyq.test;import javax.mail.PasswordAuthentication;class MyAuthenticator
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
这是我在网上找到的代码,说是可以用的,但是不能用哦!报告的错误是:
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.hyq.test.MailExample.main(MailExample.java:48)
没有权限验证,所以不能发送,可是上面明明有我自己的邮箱和密码啊,怎么还是不行呢》》》》有做过邮件开发系统的吗,帮小弟个忙吧,,,,,,,,,,
用163的应该可以的,我以前写过这样的代码,用的就是163的邮箱。
不过,有可能会出现几种情况:
1、网络不好,连接不上,导致发送失败。
2、邮件发多了,163会禁止该ip的继续发送的。这种情况缓缓就好了。
我的网速是慢点,但是也应该能连接上,因为我可以上网;
所以原因应该就是163那边的问题,我估计
怪我没有说清楚,意思没有表达清楚!!!是不是163那边采取什么措施了,所以才会连不上的;
按照代码所写,用户名和密码应该都有了,应该是可以连接上的,要是没有连接上的话,应该是代码的问题啊。
就抄代码吧,呵呵