我正在尝试使用以下代码使用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
答案 0 :(得分:3)
您可能需要在进行身份验证之前添加props.put("mail.smtp.starttls.enable","true");
。
答案 1 :(得分:0)
此代码似乎可以使用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.");
}
}
答案 2 :(得分:0)
我遇到了和你描述的问题相同的问题。在我的情况下,替换
Session session = Session.getDefaultInstance(props);
与
Session session = Session.getInstance(props);
做了这个伎俩。我希望它会有所帮助。
答案 3 :(得分:0)
这项工作对我而言。 设置以下标记。它会起作用。
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");
答案 4 :(得分:0)
mail.jar最高1.4 v不需要props.put("mail.smtp.starttls.enable","true");
更高的版本来进行设置。