0

メールの送信に問題があります。SMTP とポートは適切ですが、次のエラーのためにメッセージを送信できません:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

コードは次のとおりです。

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        final String username = "stefanrafaa@gmail.com";
        final String password = "my password";
        Properties props = System.getProperties();
        props.put("mail.smtp.auth.", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        Session session;
        session = Session.getInstance(props,
                new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress("stefanrafa0@gmail.com"));
            // Set To: header field of the header.
            message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress("stefanrafaa@gmail.com"));
            //subject message
            message.setSubject("Help Needed");
            // Now set the actual message
            message.setText("I need help please help me with this program");
            message.setContent("<h:body style=background-color :white;font-family:verdana;color:#0066CC;>"
                    + "If you are seeing this its OK !!!<br/><br/>"
                    + "</body>", "text/html; charset=utf-8");
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully...");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
            //System.out.println("MessagingException: "+mex.getMessage());
            //JOptionPane.showMessageDialog(null, "[!]Cant connect to the database.");
        }
    }                                        

私がdaniwebで尋ねたことがわかっている場合は返信してください。しかし、迅速な回答はありません...ありがとう

4

2 に答える 2

1

私もこの問題を抱えていました。それは無効化を解決avastし、コードにいくつかの変更を加えました。これは次のようになります。

public void simpleEmail2(String to, String subject, String message){
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

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

        try {

            Message mmessage = new MimeMessage(session);
            mmessage.setFrom(new InternetAddress(username));
            mmessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            mmessage.setSubject("Testing Subject");
            mmessage.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(mmessage);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
于 2013-03-23T22:45:27.823 に答える
1

あなたはこれをしたいかもしれません:

Properties props = new Properties();
props.put("mail.smtp.auth", "true");

それ以外の:

Properties props = System.getProperties();
props.put("mail.smtp.auth.", "true");

最後にドットがあり、mail.smtp.authすべてのシステム プロパティを使用するのは悪い考えのようです。

ここに一例があります http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

于 2013-03-12T22:23:46.610 に答える