Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SMTP主机中的连接错误:SMTP.outlook.com,端口:25_Java_Email_Outlook - Fatal编程技术网

Java SMTP主机中的连接错误:SMTP.outlook.com,端口:25

Java SMTP主机中的连接错误:SMTP.outlook.com,端口:25,java,email,outlook,Java,Email,Outlook,我用gmail尝试了一个成功运行的示例程序,并尝试修改outlook的示例程序,主要是更改服务器和端口名。得到如下错误: package mailsend; import javax.mail.Authenticator; import java.util.Properties; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import java

我用gmail尝试了一个成功运行的示例程序,并尝试修改outlook的示例程序,主要是更改服务器和端口名。得到如下错误:

package mailsend;

import javax.mail.Authenticator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Outlook {
    final String senderEmailID = "xxx";
    final String senderPassword = "xxx";
    final String emailSMTPserver = "smtp.outlook.com";
    //final String emailSMTPserver = "Smtp.live.com";
    final String emailServerPort = "25";
    String receiverEmailID = null;
    static String emailSubject = "Test Mail";
    static String emailBody = ":)";

    //mail.smtp.ssl.enable = "true";
    public Outlook(String receiverEmailID, String Subject, String Body) {
        this.receiverEmailID = receiverEmailID;
        this.emailSubject = Subject;
        this.emailBody = Body;
        Properties props = new Properties();
        //System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
        props.put("mail.smtp.user", senderEmailID);
        props.put("mail.smtp.host", emailSMTPserver);
        props.put("mail.smtp.port", emailServerPort);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", emailServerPort);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.enable", "true");
        /*prop key="mail.smtp.starttls.enable"   */
        SecurityManager security = System.getSecurityManager();
        //mail.smtp.ssl.enable "true"

        try {
            // SMTPAuthenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, new SMTPAuthenticator("xxx", xxx"));
            MimeMessage msg = new MimeMessage(session);
            msg.setText(emailBody);
            msg.setSubject(emailSubject);
            msg.setFrom(new InternetAddress(senderEmailID));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmailID));
            Transport.send(msg);
            //sendMessage(msg, msg.getAllRecipients());
            System.out.println("Message send Successfully:)");
        }

        catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    public class SMTPAuthenticator extends Authenticator {
        String user;
        String pwd;

        SMTPAuthenticator(String senderEmailID, String senderPassword) {

            super();
            this.user = senderEmailID;
            this.pwd = senderPassword;

        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pwd);
        }
    }

    public static void main(String[] args) {

        Outlook obj1 = new Outlook("xxx", "hi", "test");

    }
}
javax.mail.MessagineException:无法连接到SMTP主机:SMTP.outlook.com,端口:25;嵌套异常是: javax.net.ssl.SSLException:无法识别的ssl消息,纯文本连接

以下是我的代码:

package mailsend;

import javax.mail.Authenticator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Outlook {
    final String senderEmailID = "xxx";
    final String senderPassword = "xxx";
    final String emailSMTPserver = "smtp.outlook.com";
    //final String emailSMTPserver = "Smtp.live.com";
    final String emailServerPort = "25";
    String receiverEmailID = null;
    static String emailSubject = "Test Mail";
    static String emailBody = ":)";

    //mail.smtp.ssl.enable = "true";
    public Outlook(String receiverEmailID, String Subject, String Body) {
        this.receiverEmailID = receiverEmailID;
        this.emailSubject = Subject;
        this.emailBody = Body;
        Properties props = new Properties();
        //System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
        props.put("mail.smtp.user", senderEmailID);
        props.put("mail.smtp.host", emailSMTPserver);
        props.put("mail.smtp.port", emailServerPort);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", emailServerPort);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.enable", "true");
        /*prop key="mail.smtp.starttls.enable"   */
        SecurityManager security = System.getSecurityManager();
        //mail.smtp.ssl.enable "true"

        try {
            // SMTPAuthenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, new SMTPAuthenticator("xxx", xxx"));
            MimeMessage msg = new MimeMessage(session);
            msg.setText(emailBody);
            msg.setSubject(emailSubject);
            msg.setFrom(new InternetAddress(senderEmailID));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmailID));
            Transport.send(msg);
            //sendMessage(msg, msg.getAllRecipients());
            System.out.println("Message send Successfully:)");
        }

        catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    public class SMTPAuthenticator extends Authenticator {
        String user;
        String pwd;

        SMTPAuthenticator(String senderEmailID, String senderPassword) {

            super();
            this.user = senderEmailID;
            this.pwd = senderPassword;

        }

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pwd);
        }
    }

    public static void main(String[] args) {

        Outlook obj1 = new Outlook("xxx", "hi", "test");

    }
}

您必须使用端口587上的TLS连接到smtp mail.outlook.com。

您必须使用端口587上的TLS连接到smtp-mail.outlook.com