Senden von E-Mails durch java in der Google Mail-Konto mit zwei-Wege-Authentifizierung

Ich möchte eine Funktion, die per E-Mail an alle angegebenen Empfänger(gmail). Das problem, das ich bin vor ist meine Authentifizierung schlägt fehl, wenn ich versuche, geben Sie die Anmeldeinformationen, die verwendet zwei-Wege-Authentifizierung bei gmail. Mit Konto ohne zwei-Wege-Authentifizierung funktioniert es einwandfrei. Also, was muss ich tun, um die Dinge passieren, mit zwei-Wege-Authentifizierung aktiviert?

Folgende code ist der code, die ich benutze, um E-Mail senden.

public static boolean sendMail(String fromMail, String fromPassword, String toMail, String message) {
        try {
            final String user = fromMail, password = fromPassword;
            Properties prop = new Properties();
            prop.setProperty("mail.smtp.host", "smtp.gmail.com");
            prop.setProperty("mail.smtp.port", "465");
            prop.setProperty("mail.smtp.auth", "true");
            prop.setProperty("mail.smtp.ssl.enable", "true");
//           prop.put("mail.debug", "true");

//           prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

            Session sess = Session.getDefaultInstance(prop, new Authenticator() {

                @Override
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(user, password);
                }
            });

//           Session sess=Session.getDefaultInstance(prop);

            sess.setDebug(true);

            Message msg = new MimeMessage(sess);

            msg.setFrom(new InternetAddress(fromMail));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
            msg.setText(message);
            msg.setContent(message, "text/html");


            Transport.send(msg);
            return true;
        } catch (MessagingException msgEx) {
            msgEx.printStackTrace();
            return false;
        }
    }
InformationsquelleAutor trapaank | 2011-12-23
Schreibe einen Kommentar