使用javax.mail通过yahoo在java中发送邮件

时间:2015-12-15 11:11:19

标签: java javax.mail

我正在尝试使用mail.smtp.yahoo.com在java中发送邮件

以下是我发送邮件的代码:

    final String Username = "*****************";
    final String Password = "*****************";
    String to = "*****************";
    String host = "smtp.mail.yahoo.com";
    Properties properties = System.getProperties();
    // Setup mail server
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.user", Username);
    properties.put("mail.smtp.password", Password);
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(Username));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect(host, Username, Password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }

我收到此错误:

avax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at MailHelper.main(MailHelper.java:51)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    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:1672)
    ... 4 more

我尝试使用端口25,465发送它,但无济于事。  我也尝试使用smtp.gmail.com和gmail userid发送它并传递但得到不同的错误。

请帮助

4 个答案:

答案 0 :(得分:0)

尝试添加以下属性:

properties.put("mail.smtp.ssl.enable","true");

答案 1 :(得分:0)

使用这些设置

SMTP SERVER : smtp.mail.yahoo.com
PORT Number : 465

答案 2 :(得分:0)

使用此代码:

public class SendMail {

String host, port, emailid,username, password;
Properties props = System.getProperties();
Session l_session = null;

public SendMail() {
    host = "smtp.mail.yahoo.com";
    port = "587";
    emailid = "a@yahoo.com";
    username = "a";
    password = "pwd";

    emailSettings();
    createSession();
    sendMessage("test@yahoo.com", "test@gmail.com","Test","test Mail");
}

public void emailSettings() {
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "false");
    props.put("mail.smtp.port", port);
}

public void createSession() {

    l_session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });



}

public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
    //System.out.println("Inside sendMessage 2 :: >> ");
    try {

        MimeMessage message = new MimeMessage(l_session);
        emailid = emailFromUser;


        message.setFrom(new InternetAddress(this.emailid));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
        message.setSubject(subject);
        message.setContent(msg, "text/html");


        Transport.send(message);
        System.out.println("Message Sent");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }//end catch block
    return true;
}

}

答案 3 :(得分:0)

尝试添加此properties.put("mail.smtp.socketFactory.port", "465"); 您使用587作为用于TLS连接的端口号。