`

gmail javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command fi

    博客分类:
  • java
阅读更多
下面是用gmail smtp发送邮件的
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail implements Runnable {
	private final String smtp_host = "smtp.gmail.com";  					
	private final String from_userName = "xx@gmail.com";		
	private final String from_passWord = "xx";						
	private final String show_name = "xxx";
	
	public String email_address;
	public String email_subject;
	public String email_content;	
	
	public SendMail(String email_address, String email_subject, String email_content) {
		this.email_address = email_address;
		this.email_subject = email_subject;
		this.email_content = email_content;
	}

	public void run() {
		executeMailSend(email_address, email_subject, email_content);
	}
	
	/**
	 * 
	 * @param recipients: the mail send to
	 * @param sendSubject: the mail subject
	 * @param sendText: the mail content
	 */
	public void executeMailSend(String recipients, String sendSubject, String sendText) {
		
		try {			
			Properties props = System.getProperties();
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", smtp_host);
			props.put("mail.smtp.user", from_userName);
			props.put("mail.smtp.password", from_passWord);
			props.put("mail.smtp.port", "587");  //gmail smtp port 587
			props.put("mail.smtp.auth", "true");
			

			
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(false);
			
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from_userName));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
			message.setFrom(new InternetAddress(show_name + "<" + from_userName + ">"));	
			message.setSubject(sendSubject);
			message.setContent(sendText, "text/html;charset=utf-8");			
			
			Transport transport = session.getTransport("smtp");
			transport.connect(smtp_host, from_userName, from_passWord);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			
			System.out.println("send an email to " + recipients + " success");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("failure! ");

		}
	}
	public static void main (String args[]){
		new SendMail("yy@gmail.com","test","hehe").run();
	}
}


结果老是报如下的错误
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 36sm443504yxh.67
failure! 

	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 SendMail.executeMailSend(SendMail.java:59)
	at SendMail.run(SendMail.java:25)
	at SendMail.main(SendMail.java:70)
 



折腾了个把小时,苦苦寻求后发现一个解决办法
把上述代码中的
props.put("mail.smtp.port", "587");

替换成
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
			props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
			props.setProperty("mail.smtp.port", "465"); 
			props.setProperty("mail.smtp.socketFactory.port", "465"); 


究其原因也不太清楚,网上有资料说port 587也可以,不知结果不行
分享到:
评论
3 楼 insheng1990 2012-10-27  
多谢多谢!
2 楼 noasoso 2010-11-06  
嗯 很好
不过我试上面的两个 都可以发送成功的
1 楼 keete 2010-10-29  
受益匪浅,感谢分享!

相关推荐

    javax.mail-1.5.6-API文档-中文版.zip

    赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail...

    java 发送邮件 里面包含了遇到异常情况的解决办法

    javax.mail.MessagingException: Could not connect to SMTP host: zxkaka.com, port: 25; 出现这个问题时,我在foxmail 以及 outlook都可以发送邮件的。 免得自己以后忘记,在源码里注释写的很清楚

    javax.mail-1.6.2-API文档-中英对照版.zip

    赠送jar包:javax.mail-1.6.2.jar; 赠送原API文档:javax.mail-1.6.2-javadoc.jar; 赠送源代码:javax.mail-1.6.2-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.6.2.pom; 包含翻译后的API文档:javax.mail...

    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type

    ORA-29532: Java call terminated by uncaught Java exception: javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no ...

    javax.mail-1.4.4.jar

    import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet....

    javax.mail-1.6.2_javamailjar1.6.2_javamail-1.6.2_javax.mail_java

    `javax.mail-1.6.2_javamailjar1.6.2_javamail-1.6.2_javax.mail_java` 这个标题暗示了我们正在讨论的是 JavaMail 的 1.6.2 版本,这是一个稳定且广泛使用的版本。 JavaMail API 提供了一套标准的接口,用于与邮件...

    javax.mail-1.5.6-API文档-中英对照版.zip

    赠送jar包:javax.mail-1.5.6.jar; 赠送原API文档:javax.mail-1.5.6-javadoc.jar; 赠送源代码:javax.mail-1.5.6-sources.jar; 赠送Maven依赖信息文件:javax.mail-1.5.6.pom; 包含翻译后的API文档:javax.mail...

    javax.mail-api-1.6.2.jar

    通过错误描述我们发现java.lang.NoClassDefFoundError: javax/mail/MessagingException,主要说找不到这个类。通过查询发现是缺少javax.mail-api-1.6.2.jar和activation-1.1.1.jar包。在maven上下载即可

    javax.servlet-3.0.0.v201112011016-API文档-中文版.zip

    赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...

    图片处理 javax.imageio.IIOException: Unsupported Image Type

    在Java编程环境中,图片处理是一项常见的任务,但有时可能会遇到“javax.imageio.IIOException: Unsupported Image Type”的错误。这个异常通常表示Java的`ImageIO`类库无法识别或支持所尝试读取或写入的图像格式。...

    javax.mail.AuthenticationFailedException异常

    javax.mail.AuthenticationFailedException异常处理。

    javax.servlet-3.0.0.v201112011016-API文档-中英对照版.zip

    赠送jar包:javax.servlet-3.0.0.v201112011016.jar; 赠送原API文档:javax.servlet-3.0.0.v201112011016-javadoc.jar; 赠送源代码:javax.servlet-3.0.0.v201112011016-sources.jar; 赠送Maven依赖信息文件:...

    基于Maven的javax.mail发送邮件例子程序

    基于Maven的javax.mail发送邮件例子程序 pom.xml添加依赖jar: &lt;groupId&gt;javax.mail &lt;artifactId&gt;mail ${javamail.version} &lt;/dependency&gt; &lt;javamail.version&gt;1.4.1&lt;/javamail.version&gt; /* 功能:...

    javax.annotation-api-1.3.2-API文档-中文版.zip

    赠送jar包:javax.annotation-api-1.3.2.jar; 赠送原API文档:javax.annotation-api-1.3.2-javadoc.jar; 赠送源代码:javax.annotation-api-1.3.2-sources.jar; 赠送Maven依赖信息文件:javax.annotation-api-...

    javax.mail-1.6.0.jar包及移入maven仓库语句

    在Java开发中,`javax.mail`库是一个非常重要的组件,主要用于处理电子邮件的发送与接收。这个库提供了丰富的API,使得开发者能够轻松地实现邮件服务的功能。`javax.mail-1.6.0.jar`是该库的一个特定版本,包含了...

    javax.mail.jar下载

    在Java编程环境中,`javax.mail.jar` 是一个非常重要的库,它提供了用于发送和接收电子邮件的API。这个库是JavaMail API的一部分,主要用于处理SMTP(简单邮件传输协议)、POP3(邮局协议版本3)和IMAP(因特网消息...

    javax.naming.NamingException: Cannot create resource instance

    javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。

    javax.mail.jar-Java发送邮件API

    javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/

    javax.mail.SendFailedException: Sending failed问题原因

    在给出的示例中,异常堆栈跟踪显示了`nested exception is: class javax.mail.MessagingException: 554 MI:STC 0,smtp10,DsCowLC7LwPACzRN7qcQAA--.2883S2 1295256512`,这是一个特定的SMTP响应代码,表示邮件服务器...

    javax.servlet-api-3.1.0.jar中文文档.zip

    javax.servlet-api-***.jar中文文档.zip,java,javax.servlet-api-***.jar,javax.servlet,javax.servlet-api,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,javax,servlet,api,中文API文档,手册,开发...

Global site tag (gtag.js) - Google Analytics