发邮件出错javamail 503 Error: need EHLO and AUTH first

J2EE 码拜 9年前 (2015-04-03) 3876次浏览 0个评论

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
SMTP邮件协议对象创建成功
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host “smtp.qq.com”, port 25, isSSL false
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host “smtp.qq.com”, port: 25

EHLO ODR88FQJUVOOV0B
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:<yajuan.sun@qq.com>
503 Error: need EHLO and AUTH first !
com.sun.mail.smtp.SMTPSendFailedException: 503 Error: need EHLO and AUTH first !

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at beans.TextMail.send(TextMail.java:97)
at servlets.SendTextMail.doPost(SendTextMail.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
发送失败

发邮件出错javamail 503 Error: need EHLO and AUTH first
发送界面

<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"%>

<html>
  <head>   
    <title>JavaMail发送文本文件</title>    
  </head>
  
  <body>
    <div align="center">
    <font size="2">使用JavaMail发送文本邮件示例<br/>
    <form method="post" action="SendTextMail">
    	收信人:<input type="text" size="40" name="to"/><br>
    	发信人:<input type="text" size="40" name="from" value="yajuan.sun@qq.com"/><br>
    	主&nbsp;&nbsp;题<input type="text" size="40" name="subject"/><br>
    	内&nbsp;&nbsp;容<textarea rows="6" cols="38" name="content"></textarea><br>
    	<input type="submit" value="发送"/>
    	<input type="reset" value="取消"/>
    </form>
</font>
</div>
  </body>
</html>

调用的servlet

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.TextMail;

public class SendTextMail extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//设置发送方的SMTP地址
		String host = "smtp.qq.com";
		//user
		String name = "yajuan.sun@qq.com";
		//password
		String password = "qqyouxiang";

		TextMail mail = new TextMail(host,name,password);
		mail.setFrom(request.getParameter("from"));
		mail.setTo(request.getParameter("to"));
		mail.setSubject(request.getParameter("subject"));
		mail.setText(request.getParameter("content"));

		response.setContentType("text/html");
		response.setCharacterEncoding("gb2312");
		PrintWriter out = response.getWriter();

		if(mail.send())
			out.print("<font size=""2"">邮件发送成功</font>");
		else
			out.print("<font size=""2"">邮件发送失败</font>");
	}

}

邮件发送

package beans;

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 TextMail {
	private MimeMessage message; 
	private Properties props;
	private Session session;
	private String name="";
	private String password="";

	public TextMail(String host,String name,String password){
		//发送邮件初始化
		this.name = name;
		this.password = password;
		props = new Properties();
		//设置SMTP主机
		props.put("mail.smtp.host", host);
		//设置SMTP属性验证
		props.put("mail.smtp.auth", true);
		//获得邮件会话对象
		MyAuthenticator auth = new MyAuthenticator(name,password);
		session = Session.getDefaultInstance(props, auth);
		session.setDebug(true);
		//创建MIME邮件对象
		message = new MimeMessage(session);
	}

	public void setFrom(String from){
		try{
			message.setFrom(new InternetAddress(from));

		}catch(AddressException e){ System.out.println("setFrom地址错误");}
		catch(MessagingException e){System.out.println("setFrom设置message错误");}
	}
	public void setTo(String to){
		try{

			message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		}catch(AddressException e){e.printStackTrace();}
		catch(MessagingException e){e.printStackTrace();}
	}
	public void setSubject(String subject){
		try{
			message.setSubject(subject);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void setText(String text){
		try{
			message.setText(text);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	//send
	public boolean send(){
//		try {
//			System.out.println("写出message");
////			message.writeTo(System.out);
//		} catch (FileNotFoundException e1) {
//			// TODO Auto-generated catch block
//			System.out.println("写message出错");
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (MessagingException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		Transport transport = null;
		try{
			//创建SMTP邮件协议发送对象
			transport = session.getTransport("smtp");
			System.out.println("SMTP邮件协议对象创建成功");
		}catch(Exception e){ System.out.println("创建smtp邮件协议发送对象失败");return false;}
		try{//取得与邮件服务器的连接
			transport.connect((String)props.get("mail.smtp.host"),name, password);
			System.out.println("连接服务器成功");
		}catch(Exception e2){System.out.println("与邮件服务器连接失败");return false;}
		try{//通过邮件服务器发送邮件
			transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
		}catch(Exception e3){System.out.println("发送失败");  return false;}
		try{transport.close();}catch(Exception e){System.out.println("关闭连接失败");return false;}
			return true;


//		}catch(NoSuchProviderException e){
//			e.printStackTrace();
//			return false;
//		}catch(MessagingException e){
//			e.printStackTrace();
//			return false;
//		}
	}

}

认证

package beans;

import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;


public class MyAuthenticator extends Authenticator {
	String name;
	String password;

	public MyAuthenticator(String name,String password){
		this.name = name;
		this.password = password;

	}
	protected PasswordAuthentication getPasswordAuthentication(){
		return new PasswordAuthentication(name,password);
	}
}
发邮件出错javamail 503 Error: need EHLO and AUTH first
高手请帮忙看看啊,万分感谢!
已经在网上找了好多办法,但是都不行啊!请高手醍醐灌顶啊!
发邮件出错javamail 503 Error: need EHLO and AUTH first
不能沉啊  请高手帮忙看看问题出在哪儿了?
发邮件出错javamail 503 Error: need EHLO and AUTH first
20分

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
SMTP邮件协议对象创建成功
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25

EHLO moby-PC
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: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
连接服务器成功
DEBUG SMTP: use8bit false
MAIL FROM:<yajuan.sun@qq.com>
250 Ok
RCPT TO:<test@qq.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   chiqingye@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: yajuan.sun@qq.com
To: test@qq.com
Message-ID: <6037166.0.1363187885900.JavaMail.moby@moby-PC>
Subject: =?UTF-8?B?5rWL6K+V?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

5Y+q5piv5LiA5Liq5rWL6K+V
.
250 Ok: queued as 
QUIT
221 Bye
邮件发送成功

用你的代码没有有测出问题,发送成功了。只测试业务逻辑,没有使用servlet方式发送

发邮件出错javamail 503 Error: need EHLO and AUTH first
20分
引用 4 楼 bluemoby 的回复:

Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849DEBUG: setDebug: JavaMail version 1.4.7DEBUG: getProvider() returning javax.mail.Provide……

我也测试了是可以的,是不是你的配置出了问题

发邮件出错javamail 503 Error: need EHLO and AUTH first
看你的qq邮箱是否支持smtp协议?
登录qq邮箱—设置–账户—POP3/IMAP/SMTP/Exchange服务—勾选POP3/SMTP服务。再试
发邮件出错javamail 503 Error: need EHLO and AUTH first
5分
javamail版本多少?
发邮件出错javamail 503 Error: need EHLO and AUTH first
5分
我之前也做了一个javamail,上传都我的资源里,自己看吧,包含数据库的;
发邮件出错javamail 503 Error: need EHLO and AUTH first
引用 4 楼 bluemoby 的回复:

用你的代码没有有测出问题,发送成功了。只测试业务逻辑,没有使用servlet方式发送

你的Debug第4行显示的是

DEBUG SMTP: useEhlo true, useAuth true

但是我测试显示的是DEBUG SMTP: useEhlo true, useAuth false

发邮件出错javamail 503 Error: need EHLO and AUTH first
引用 5 楼 jianqiangking 的回复:

引用 4 楼 bluemoby 的回复:我也测试了是可以的,是不是你的配置出了问题

先谢谢,如果是配置错,配置会是错在哪儿?

发邮件出错javamail 503 Error: need EHLO and AUTH first
邮箱的smtp是开启的,换了sina、qq的邮箱服务器都不行啊——

怎么看javamail的版本,我在网上下的就光是个mail.jar 和activiation.jar的名字,没有版本号

发邮件出错javamail 503 Error: need EHLO and AUTH first
建议用数字的qq邮箱试下
发邮件出错javamail 503 Error: need EHLO and AUTH first
好,我试试数字邮箱
4l的朋友测试的邮件还在我邮箱里可以看见,就是说是可以发送成功的。可我自己就是发布成功。。。
javamail是1.4ea版本的
发邮件出错javamail 503 Error: need EHLO and AUTH first
数字邮箱也不行啊。
测试还是显示
DEBUG SMTP: useEhlo true, useAuth false
发邮件出错javamail 503 Error: need EHLO and AUTH first
50分
props.put(“mail.smtp.auth”, true);改为
props.put(“mail.smtp.auth”, “true”);看下
发邮件出错javamail 503 Error: need EHLO and AUTH first
成功了,问题竟然出在这儿了,天哪!双引号引发的绝望啊!
谢谢你这么耐心帮我看程序!太谢谢了!谢谢楼上各位!
发邮件出错javamail 503 Error: need EHLO and AUTH first
你先不要用servlet,直接用main方法测一下你的后台代码。如果能通,那就是你配置的问题了。先试试
发邮件出错javamail 503 Error: need EHLO and AUTH first
引用 4 楼 bluemoby 的回复:

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
SMTP邮件协议对象创建成功
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25

EHLO moby-PC
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: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
连接服务器成功
DEBUG SMTP: use8bit false
MAIL FROM:<yajuan.sun@qq.com>
250 Ok
RCPT TO:<test@qq.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   chiqingye@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: yajuan.sun@qq.com
To: test@qq.com
Message-ID: <6037166.0.1363187885900.JavaMail.moby@moby-PC>
Subject: =?UTF-8?B?5rWL6K+V?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

5Y+q5piv5LiA5Liq5rWL6K+V
.
250 Ok: queued as 
QUIT
221 Bye
邮件发送成功

用你的代码没有有测出问题,发送成功了。只测试业务逻辑,没有使用servlet方式发送

请问,大侠用的什么调试工具?
谢谢

发邮件出错javamail 503 Error: need EHLO and AUTH first
楼主,你怎么打印出这些发送状态的日志的呢,跪求:
发邮件出错javamail 503 Error: need EHLO and AUTH first
引用 20 楼 ZuoMoHuaEr 的回复:

楼主,你怎么打印出这些发送状态的日志的呢,跪求:

session.setDebug(true);

发邮件出错javamail 503 Error: need EHLO and AUTH first
16楼的是正解

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明发邮件出错javamail 503 Error: need EHLO and AUTH first
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!