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

下面是用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也可以,不知结果不行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值