有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用java邮件API发送邮件,但出现错误。我能成功。建议修复错误或提供正确的代码

代码:

import java.util.*;  
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 SendEmail  
{  
public static void main(String [] args)
{
    String host="chnmail.hcl.com";
    final String user="allwinjayson.m@hcl.com";
    final String password="*********";
    String to="allwinjayson.m@hcl.com";

    //Get the session object  
    Properties props = new Properties();  
    props.put("mail.smtp.host",host);  
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");


    //new
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() 
    {  
            protected PasswordAuthentication getPasswordAuthentication() 
            {  
                return new PasswordAuthentication(user,password);  
            }  
    });  


     //Compose the message  
    try 
    {  
        MimeMessage message = new MimeMessage(session);  
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.setSubject("Testing");
        message.setText("This is simple program of sending email using JavaMail API");

        //send the message  
         Transport.send(message);  
         System.out.println("message sent successfully...");  
    } 
    catch (MessagingException e) 
    {
        e.printStackTrace();
    }  
}
}  

错误:

javax.mail.MessagingException: Could not connect to SMTP host: chnmail.hcl.com, port: 25;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at SendingClass.SendEmail.main(SendEmail.java:55)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:694)
    at sun.security.ssl.InputRecord.read(InputRecord.java:527)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 7 more

共 (1) 个答案

  1. # 1 楼答案

    不要指定插座工厂,它会工作的。端口25上的SMTP最初将是明文,然后starttls将协商TLS进行加密

    请注意,这需要JavaMail 1.4.5或更高版本(current latest是1.5.6)