![](/img/trans.png)
[英]Using Gmail SMTP servers with JavaMail: Must issue a STARTTLS command first (JavaMail version 1.4.1)
[英]javamail error: must issue starttls command first
我正在尝试使用以下代码使用 javamail api 发送邮件:当我编译类文件时,我收到以下错误,提示“必须首先发出 starttls 命令”我在下面提到了错误。 还有getProvider()
函数错误,我认为是这样...我不知道这些错误是什么意思。
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import javax.mail.Authenticator;
import java.net.*;
import java.util.Properties;
public class mailexample
{
public static void main (String args[]) throws Exception {
String from = args[0];
String to = args[1];
try
{
Properties props=new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
{
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("123@gmail.com", "pass");
}
};
Session sess=Session.getDefaultInstance(props,authenticator);
sess.setDebug (true);
Transport transport =sess.getTransport ("smtp");
Message msg=new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Hello JavaMail");
msg.setText("Welcome to JavaMail");
transport.connect();
transport.send(msg);
}
catch(Exception e)
{
System.out.println("err"+e);
}
}
}
错误:
C:\Users\bobby\Desktop>java mailexample abc@gmail.com abc@gmail. com DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s mtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20 DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 DEBUG SMTP SENT: EHLO bobby-PC DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 250-SIZE 35651584 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s mtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16 DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 DEBUG SMTP SENT: EHLO bobby-PC DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 250-SIZE 35651584 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES DEBUG SMTP SENT: MAIL FROM: DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb .16 DEBUG SMTP SENT: QUIT errjavax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f irst. l29sm12930755rvb.16
可能,你需要把props.put("mail.smtp.starttls.enable","true");
在您进行身份验证之前。
使用 Gmail SMTP 服务器发送电子邮件时,此代码似乎可以正常工作。 注意 - 这没有附件。
package org.ssb.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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 MailClient {
/**
* Entry method
*
* @param args
* String[]
*/
public static void main(String[] args) {
MailClient client = new MailClient();
try {
client.sendMail();
} catch (AddressException ae) {
ae.printStackTrace();
} catch (MessagingException me) {
me.printStackTrace();
}
}
/**
* Sends an email
*
* @param none
*/
private void sendMail() throws AddressException, MessagingException {
// Get a Properties object
Properties props = System.getProperties();
// ******************** FOR PROXY ******************
// props.setProperty("proxySet","true");
// props.setProperty("socksProxyHost","9.10.11.12");
// props.setProperty("socksProxyPort","80");
// props.setProperty("socksProxyVersion","5");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
// ******************** FOR SSL ******************
//final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
//props.setProperty("mail.smtp.socketFactory.fallback", "false");
//props.setProperty("mail.smtp.port", "465");
//props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
final String username = "sender-username";
final String password = "sender-password";
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// -- Create a new message --
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("sender@gmail.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipient@yahoo.com", false));
msg.setSubject("Hello");
msg.setSentDate(new Date());
// **************** Without Attachments ******************
msg.setText("How are you");
Transport.send(msg);
System.out.println("Message sent.");
}
}
我遇到了与您描述的相同的问题。 就我而言,更换
Session session = Session.getDefaultInstance(props);
和
Session session = Session.getInstance(props);
成功了。 我希望它会有所帮助。
这对我有用。 设置以下标签。 它会起作用。
props.put("mail.smtp.user","username");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.EnableSSL.enable","true");
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");
mail.jar props.put("mail.smtp.starttls.enable","true");
1.4 v 不需要props.put("mail.smtp.starttls.enable","true");
以后的版本需要设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.