读书人

java发送邮件503异常!

发布时间: 2012-03-09 16:54:57 作者: rapoo

java发送邮件503错误!!!
代码如下:

Java code
package test;import java.io.IOException;import java.util.Properties;import javax.activation.DataHandler;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;import javax.mail.util.ByteArrayDataSource;public class SeUglyBO {    private String host = "smtp.qq.com"; // 定义发送用户帐号密码    private String from = "593456521@qq.com";    private String user = "593456521@qq.com";        private String password = "13206511521";/**     * 大部分     * @param name     * @param to     * @param title     * @param content     * @param lie     * @return     * @throws IOException     */    public boolean send(String to, String title, String content) throws IOException {        //System.out.println(to);        boolean ioke = true;        try {            System.out.println(to+"======"+title+"======"+content);            // 架设smtp            Properties pro = new Properties();            pro.put("smtp.qq.com", host);            pro.put("smtp.qq.com", "true");// ******8            Session session = Session.getDefaultInstance(pro);            session.setDebug(true); // 是否在控制台打出语句            MimeMessage message = new MimeMessage(session); // 定义重哪个邮箱到哪个邮箱的地址和内容            message.setFrom(new InternetAddress(from));            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));            message.setSubject(title);            collect(content, message);            message.saveChanges();            Transport tran = session.getTransport("smtp"); // 通过SMTP效验用户,密码等进行连接            tran.connect(host, user, password);            tran.sendMessage(message, message.getAllRecipients());            tran.close();        } catch (AddressException e) {            // TODO Auto-generated catch block            e.printStackTrace();            ioke = false;        } catch (MessagingException e) {            // TODO Auto-generated catch block            e.printStackTrace();            ioke = false;        }        return ioke;    }    

测试调用代码如下:
Java code
package test;import java.io.IOException;import action.SendEmailAction;public class test {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        SeUglyBO se=new SeUglyBO();        try {                    se.send("123456@qq.com","撒的发生的发生","撒旦法。");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

错误如下:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25

220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25

EHLO cnjsb-f06601272
250-smtp.qq.com
250-PIPELINING
250-SIZE 52428800
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""


DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<593456521@qq.com>
503 Error: need EHLO and AUTH first !
javax.mail.MessagingException: 503 Error: need EHLO and AUTH first !

at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
at test.SeUglyBO.send(SeUglyBO.java:56)
at test.test.main(test.java:17)


[解决办法]
服务器拒绝响应啊 看看你的smtp什么的是不是对的 账号密码之类的
[解决办法]
这是没有做登录认证
[解决办法]
你换个邮箱试试 不要用qq
有的qq邮箱是拒绝用这种api开发的邮箱发送的邮件的 要在安全设置里面改变下级别
[解决办法]
我以前的一个案例你可以参考下:
public void sendMail(String fromAddress,String toAddress,String subject,String content)throws Exception{
Properties pro = new Properties();
pro.put("mail.smtp.host", "localhost");
pro.put("mail.smtp.auth","true");

Session session = Session.getInstance(pro);//取得邮件会话

MimeMessage mess = new MimeMessage(session);

InternetAddress addr = new InternetAddress(fromAddress);//来处
mess.setFrom(addr);

InternetAddress toaddr = new InternetAddress(toAddress);//去处
mess.setRecipient(MimeMessage.RecipientType.TO, toaddr);

mess.setSubject(subject);//主题
mess.setText(content);

mess.saveChanges();
session.setDebug(true);

Transport transport = session.getTransport("smtp");
transport.connect("chen9366","asd123");
transport.sendMessage(mess, mess.getAllRecipients());

transport.close();

}
[解决办法]
lz 你把qq密码也贴出来了。。。。。
[解决办法]
lz搞定了吗 你做的这个测试从来就没成功过 一直是这个错误吗 我以前也做过这个 不过好久了 我整体看了下没太大问题 细节方便的我也记不住 回去帮你看看
[解决办法]
少了句代码
参考下我下面代码
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("***","***");//登录用户名/密码
}
});
session.setDebug(true);
[解决办法]
host 指定的端口被封掉了吧 给你割草看网址http://www.hetaoblog.com/javamail-could-not-connect-to-smtp-host-25%E9%94%99%E8%AF%AF/
[解决办法]
http://blog.csdn.net/lrbyantai/article/details/7056676

auth=true

读书人网 >Java Web开发

热点推荐