Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java-Mail - Mehrere Sendeversuche

1 view
Skip to first unread message

Hannes Mattsen

unread,
Jun 24, 2006, 7:53:26 AM6/24/06
to
Hallo,

ich habe folgende simple Klasse, um Mails aus einer Java-Anwendung
heraus zu versenden.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailNotification {

public MailNotification() {
super();
}

public void sendMail(String recipient, String subject, String message,
String from) throws AddressException, MessagingException {

int countTries = 0;
int maxTries = 5;

while (countTries < maxTries) {
countTries++;
System.out.println("try " + countTries);

try {

Properties props = new Properties();
String smtpServer = "test";
props.put("mail.smtp.host", smtpServer);

Session session = Session.getDefaultInstance(props);
Message msg = new MimeMessage(session);

InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress addressTo = new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);

msg.setSubject(subject);
msg.setContent(message, "text/plain");

Transport.send(msg);
countTries = maxTries;

} catch (AddressException e) {
if (countTries == maxTries) {

throw e;
}
} catch (MessagingException e) {
if (countTries == maxTries) {

System.out.println("countTries " + countTries);
System.out.println("maxTries " + maxTries);
throw e;
}
}
}


}
}


Und die dazugehörige Test-Klasse:

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

public class TestMailNotification {

/**
* @param args
*/
public static void main(String[] args) {
MailNotification mailNotification = new MailNotification();
try {
mailNotification.sendMail("hans....@gmx.de", "Subject", "Der
Inhalt...", "Absender");
} catch (AddressException e) {
System.out.println("AddressException occured");
e.printStackTrace();
} catch (MessagingException e) {
System.out.println("MessagingException occured");
e.printStackTrace();
}

}

}


Wichtig ist mir in meinem Programm, dass es bei einer Fehlermeldung
insgesamt 5 Mal versucht, die Mails zu versenden. Erst wenn das nicht
geklappt hat, soll eine Exception geworfen werden.

Wenn ich das jetzt teste sieht die Ausgabe aber immer unterschiedlich
aus, z.B. so:


try 1
try 2
javax.mail.MessagingException: Unknown SMTP host: test;
nested exception is:
java.net.UnknownHostException: test
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1211)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at MailNotification.sendMail(MailNotification.java:54)
at TestMailNotification.main(TestMailNotification.java:12)
try 3
try 4
try 5
countTries 5
maxTries 5
MessagingException occured


Oder so:

try 1
try 2
try 3
javax.mail.MessagingException: Unknown SMTP host: test;
nested exception is:
java.net.UnknownHostException: test
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1211)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at MailNotification.sendMail(MailNotification.java:54)
at TestMailNotification.main(TestMailNotification.java:12)
try 4
try 5
countTries 5
maxTries 5
MessagingException occured


Eigentlich würde ich erwarten, dass die Exception aber immer erst nach
dem letzten Versuch geworfen wird... Was passiert hier?

Oder wird die Exception tatsächlich erst nach dem 5. Versuch geworfen
(die Ausgabe ,,MessagingException occurred" erscheint ja immer auch
richtigerweise am Ende)?

Ist die Umsetzung ,,gut", um mehrere Sendeversuche durchzuführen?

Gruss

Hannes

Achim Peters

unread,
Jun 24, 2006, 8:33:50 AM6/24/06
to
Hannes Mattsen wrote:

> ich habe folgende simple Klasse, um Mails aus einer Java-Anwendung
> heraus zu versenden.
>

> System.out.println("try " + countTries);

> e.printStackTrace();

> Wenn ich das jetzt teste sieht die Ausgabe aber immer unterschiedlich
> aus, z.B. so:
>
>
> try 1
> try 2
> javax.mail.MessagingException: Unknown SMTP host: test;
> nested exception is:
> java.net.UnknownHostException: test
> at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1211)
> at
> com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
> at javax.mail.Service.connect(Service.java:233)
> at javax.mail.Service.connect(Service.java:134)
> at javax.mail.Service.connect(Service.java:86)
> at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
> at javax.mail.Transport.send0(Transport.java:150)
> at javax.mail.Transport.send(Transport.java:80)
> at MailNotification.sendMail(MailNotification.java:54)
> at TestMailNotification.main(TestMailNotification.java:12)
> try 3
> try 4
> try 5
> countTries 5
> maxTries 5
> MessagingException occured

> Eigentlich würde ich erwarten, dass die Exception aber immer erst nach


> dem letzten Versuch geworfen wird...

Wird sie auch.

> Was passiert hier?

System.out schreibt defaultmäßig nach stdout, e.printStackTrace() aber
nach stderr. Das sind zwei unterschiedliche Streams mit eigenen Puffern.
Wenn beide auf dasselbe Ausgabemedium (Konsole) gehen, ist die
Reihenfolge, in der sie auftauchen, nicht deterministisch. Du könntest
nach jedem System.out oder vor dem e.println() noch ein flush() auf
System.out machen, oder e.println auch nach stdout schreiben lassen,
wenn das wichtig ist.

> Oder wird die Exception tatsächlich erst nach dem 5. Versuch geworfen
> (die Ausgabe ,,MessagingException occurred" erscheint ja immer auch
> richtigerweise am Ende)?

Genau.

Bye
Achim

Timo Stamm

unread,
Jun 24, 2006, 8:34:31 AM6/24/06
to
Hannes Mattsen schrieb:
> [...]

> Wichtig ist mir in meinem Programm, dass es bei einer Fehlermeldung
> insgesamt 5 Mal versucht, die Mails zu versenden. Erst wenn das nicht
> geklappt hat, soll eine Exception geworfen werden.
>
> Wenn ich das jetzt teste sieht die Ausgabe aber immer unterschiedlich
> aus [...]

Ich habe in deinem Code nichts gesehen, was für diese Ausgabe
verantwortlich sein könnte.

Als ich deinen Code getestet habe, habe ich so eine Ausgabe auch nicht
bekommen. Eine Exception wurde immer erst nach dem 5. Versuch geworfen.

Du hast anscheinend etwas anderes gepostet als du getestet hast.


> Ist die Umsetzung ,,gut", um mehrere Sendeversuche durchzuführen?

Nein, das macht so keinen Sinn. Wie du in der Dokumentation zu
javax.mail nachlesen kannst, versendest du keine E-Mails, sondern
übergibst sie an einen MTA (Mail Transfer Agent) wie Sendmail.

Der MTA ist dafür verantwortlich, mehrere Zustellversuche zu machen.

Für die Übergabe der E-Mails an den MTA wird auch schon ein Prokoll
eingesetzt das mit Fehlern und Latenzen umgehen kann, mehrere Versuche
sind hier also auch überflüssig.

Was du machst ist vergleichbar damit, dass du versuchst eine
Konfigurationsdatei mehrmals hintereinander einzulesen, obwohl du schon
beim erstenmal eine FileNotFoundException bekommst.

Wenn eine MessagingException kommt, fordere den Benutzer auf seine
Angaben für den SMTP-Server zu überprüfen. Eine AddressException beim
Versand solltest du vermeiden, indem du die E-Mail-Adresse bei der
Eingabe überprüfst. Wenn trotzdem eine AddressException zustande kommt,
benachrichtige den Benutzer dass der Versand der E-Mail x fehlgeschlagen
ist weil y keine gültige E-Mail Adresse ist.


Timo

Timo Stamm

unread,
Jun 24, 2006, 9:05:39 AM6/24/06
to
Timo Stamm schrieb:

> Du hast anscheinend etwas anderes gepostet als du getestet hast.

Oder auch nicht, siehe Achims Post.

0 new messages