Javamail with authentication

tonyhur

Mitglied
Ich habe folgende Klasse geschrieben, um ein Email zu schicken.

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.Authenticator;
import javax.mail.internet.*;

public class SendMail {
public static void send(String to, String sub, String msg, final String user, final String pass) {

//create an instance of Properties Class
Properties props = new Properties();
props.put("mail.smtp.host", "****");
// below mentioned mail.smtp.port is optional
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.debug", "true");
props.put("mail.smtp.user", "****");
props.put("mail.smtp.timeout", "5000");

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

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);

Transport.send(message);

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


Leider kommt beim Ausführen die Meldung:
........
DEBUG SMTP: need username and password for authetication
DEBUG SMTP: protocolConnect returning false, host=****, user=****, password=(null)
.....

Das verstehe ich nicht. Ich übergebe ja das "user" und "pass" an den PasswordAuthentication.

Vielen Dank für einen Hint
Tony
 

Jw456

Top Contributor
ist der user und pw auch das was du zb in outlook eingestellt hast?

Java:
        Properties properties = System.getProperties();

        // Setup mail server
        String host = "smtp.gmail.com";
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");

        // Get the Session object.// and pass
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("youremail@gmail.com", "*******");

            }
 
Zuletzt bearbeitet:

tonyhur

Mitglied
ist der user und pw auch das was du zb in outlook eingestellt hast?

Java:
        Properties properties = System.getProperties();

        // Setup mail server
        String host = "smtp.gmail.com";
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");

        // Get the Session object.// and pass
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("youremail@gmail.com", "*******");

            }
ja, klar. Ich benutze auch dasselbe in einer php Applikation. Und dort funktioniert es. (Ich benutze nicht gmail und auch nicht outlook, aber das tut wahrscheinlich nichts zur Sache).
 

LimDul

Top Contributor
Irgendwo fehlt mir die Verbindung zwischen den Transport & der Session. Ich bin kein experte im Mail-Versand, aber wenn ich mir die Beispiele anschaue, muss der Transport aus der Session erzeugt werden (z.B. Transport transport = session.getTransport();)

Das ist ja auch logisch, weil der Transport User/Password Auth vermutlich macht.

Das fehlt bei dir im Code
 

Jw456

Top Contributor
Irgendwo fehlt mir die Verbindung zwischen den Transport & der Session. Ich bin kein experte im Mail-Versand, aber wenn ich mir die Beispiele anschaue, muss der Transport aus der Session erzeugt werden (z.B. Transport transport = session.getTransport();)

Das ist ja auch logisch, weil der Transport User/Password Auth vermutlich macht.

Das fehlt bei dir im Code
Das senden ist eigentlich ok so.
Nur sein Aut wird nicht stimmen. Er bekommt ja auch genau diese Fehler Meldung.
Die Anmeldung beim SMTP Server schlägt fehl.
 

Jw456

Top Contributor
Java:
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
Ob starttls und Port 465 stimmt.
465 ist bei den meisten Servern ssl/tsl

Starttsl ist meistens ein anderer Port.



Ich würde auch ssl/tsl benutzen

So wie ich es gezeigt habe.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
D JavaMail: HTML Code einer Mail Netzwerkprogrammierung 9
D JavaMail - Mailsabrufen Problem (imap) Netzwerkprogrammierung 12
H JavaMail STARTTLS Netzwerkprogrammierung 6
S javamail und filterung bekannter nachrichten Netzwerkprogrammierung 3
T JavaMail POP Zugriff zeigt nicht alle Emails Netzwerkprogrammierung 2
S Mehrere Attachments mit JavaMail API auslesen Netzwerkprogrammierung 3
R JavaMail: Wie bekommt man rückgabecodes vom Server? Netzwerkprogrammierung 3
F Mehrere Attachments mit JavaMail API Netzwerkprogrammierung 2
H HTML-Mails mit JavaMail API Netzwerkprogrammierung 3
A JavaMail Problem - Nachricht wird nicht versendet ? Netzwerkprogrammierung 9
G JavaMail subFolders Netzwerkprogrammierung 6
S Voreinstellungen für JavaMail Netzwerkprogrammierung 2
D JavaMail Kontaktformular Umlaute Problem Netzwerkprogrammierung 2
conan2 JavaMail Netzwerkprogrammierung 10
R Problem beim Senden von Emails (Javamail) Netzwerkprogrammierung 2
J JavaMail Exception bei senden an anderen Server. Netzwerkprogrammierung 8
J JavaMail-Problem Netzwerkprogrammierung 5
G Fragen/Probleme mit JavaMail Netzwerkprogrammierung 2
G JavaMail und Session/Message Problem Netzwerkprogrammierung 2
L JavaMail: Mail in Posteingang aber nicht in Versendet Netzwerkprogrammierung 2
L JavaMail: Automatisches Email Versand (mehrere Empfänger) Netzwerkprogrammierung 4
L JavaMail: Versenden von mails funktioniert nicht Netzwerkprogrammierung 7
V JavaMail API Problem Netzwerkprogrammierung 10
P mit javamail gmx postfach prüfen Netzwerkprogrammierung 8
T Mit JavaMail API Faxe versenden? Netzwerkprogrammierung 5
P Basic Authentication Netzwerkprogrammierung 11
M HTTP HTTPClient Authentication - Quickr Server Netzwerkprogrammierung 8
TiME-SPLiNTER HTTP User Authentication: Java Client -> Webserver Netzwerkprogrammierung 7
G Server Client + Authentication Netzwerkprogrammierung 3
AlexSpritze Authentication Problem bei WebServices Netzwerkprogrammierung 4
gieser HTTP SSL Client Authentication Netzwerkprogrammierung 1
KNeuhaus (Windows) Authentication VON einer Java-App aus. Netzwerkprogrammierung 2
A HTTP-Authentication Netzwerkprogrammierung 2

Ähnliche Java Themen

Neue Themen


Oben