ERLEDIGT
JA
JA
ANTWORTEN
1
1
ZUGRIFFE
511
511
EMPFEHLEN
-
18.11.11 16:09 #1
Hallo Community,
ich tüftel bereits den ganzen Tag an meiner Mailsender Klasse. Ich versuche aus meinem Programm via SMTP eine Mail zu versenden.
Folgendes irritiert mich aber: Wenn ich meine Mailsender-Klasse als JUnit-Test laufen lasse, wird die Mail verschickt. TOP!
Wenn ich die Mailsender-Klasse aber in mein Hauptprogramm einbinde (exakt genau so wie im JUnit-Test), bekomm ich folgende Exception, wenn ich vorher nicht überprüfe, ob der Mailserver erreichbar ist.
Zeile 113 entspricht im Code Zeile 82.Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect> javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370) at javax.mail.Service.connect(Service.java:275) at javax.mail.Service.connect(Service.java:156) at javax.mail.Service.connect(Service.java:105) at javax.mail.Transport.send0(Transport.java:168) at javax.mail.Transport.send(Transport.java:98) at de.util.MailUtils.createAndSend(MailUtils.java:113) Caused by: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250) ... 12 more
Wenn ich nun überprüfe, ob der Mailserver verfügbar ist, bekomm ich folgende Exception:
Zeile 140 entspricht im Code 109.Code java:1 2 3 4 5 6 7
<javax.mail.NoSuchProviderException: Invalid protocol: null> javax.mail.NoSuchProviderException: Invalid protocol: null at javax.mail.Session.getProvider(Session.java:431) at javax.mail.Session.getStore(Session.java:530) at javax.mail.Session.getStore(Session.java:510) at javax.mail.Session.getStore(Session.java:496) at de.util.MailUtils.isReachable(MailUtils.java:140)
Hier ist noch mein Code den ich verwende:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/** */ public static final String SMTP = "smtp"; /** */ public static final String MAIL_SMTP_HOST = "mail.smtp.host"; /** */ public static final String MAIL_SMTP_PORT = "mail.smtp.port"; /** */ public static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol"; /** */ public static final int DEFAULT_SMTP_PORT = 25; private MailUtils() { } /** * @param mailProps * @param from * @param recipient * @param subject * @param content * @param attachment * @throws MessagingException */ public static void createAndSend(Properties mailProps, String from, String[] recipient, String subject, String content, File[] attachment) throws MessagingException { Session session = Session.getDefaultInstance(mailProps); session.setDebug(true); session.setDebugOut(System.out); Message message = new MimeMessage(session); // ---------------------------------------------------------------------- // Meta-Data // ---------------------------------------------------------------------- // From message.setFrom(new InternetAddress(from)); // Recipients InternetAddress[] recipients = new InternetAddress[recipient.length]; for(int i = 0; i < recipient.length; i++) { recipients[i] = new InternetAddress(recipient[i]); } message.setRecipients(Message.RecipientType.TO, recipients); // Subjectline message.setSubject(subject); // ---------------------------------------------------------------------- // Content // ---------------------------------------------------------------------- MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart contentBody = new MimeBodyPart(); contentBody.setText(content); contentBody.setDisposition(MimeBodyPart.INLINE); mimeMultipart.addBodyPart(contentBody); // ---------------------------------------------------------------------- // Attachments // ---------------------------------------------------------------------- for(File file : attachment) { if(file != null && file.exists() && file.canRead()) { MimeBodyPart attachmentBody = new MimeBodyPart(); attachmentBody.setDataHandler(new DataHandler(new FileDataSource(file))); attachmentBody.setFileName(file.getName()); attachmentBody.setDisposition(MimeBodyPart.ATTACHMENT); mimeMultipart.addBodyPart(attachmentBody); } } // --- // save message.setContent(mimeMultipart); message.saveChanges(); // send Transport.send(message); } /** * @param smtpHost * @param port * @return the Properties with the Transportprotocol and SMTP-Host */ public static Properties createSmtpMailProperties(String smtpHost, int port) { Properties mailProps = new Properties(); mailProps.put(MAIL_TRANSPORT_PROTOCOL, SMTP); mailProps.put(MAIL_SMTP_HOST, smtpHost); mailProps.put(MAIL_SMTP_PORT, String.valueOf(port)); return mailProps; } /** * @param host * @param port * @return true if the MailRelay is reachable. */ public static boolean isReachable(String host, int port) { Properties properties = createSmtpMailProperties(host, port); Session s = Session.getDefaultInstance(properties); try { Transport transport = s.getTransport(); transport.connect(); return true; } catch(MessagingException e) { e.printStackTrace(); } return false; }
Die Debugausgabe hat folgendes ergeben:
Code java:1 2 3 4
DEBUG: setDebug: JavaMail version 1.4ea DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
Ich hoffe, dass mir bei dem Problem jemand auf die Sprünge helfen kann.
Gruß
FabioBitte die Code-Tags verwenden. Bei Java-Code: [java]...[/java]
Tutorials:
Automatisches erzeugen eines Inhaltsverzeichnisses (Javascript)
JAnimationPanel - Animationen für Swing/AWT
SWTRatingBar (Bewertungs-Composite) selbst programmieren
____________________________________________________________________________
Über eine Bewertung (Stern links unter dem Beitrag) oder ein Danke freue ich mich sehr.
-
21.11.11 10:31 #2
Problem gelöst!
Es lag an folgender Ursache: Da in dem Programm verschiedene SMTP-Verbindungen sind, darf man im Code nicht schreibensondernCode java:1
Session.getDetaulfIntsance(properties, null)
Dann werden nicht die globalen SMTP-Einstellungen verwendet, sondern die Properties, die übergeben werden.Code java:1
Session.getInstance(properties, null)
Gruß
FabioBitte die Code-Tags verwenden. Bei Java-Code: [java]...[/java]
Tutorials:
Automatisches erzeugen eines Inhaltsverzeichnisses (Javascript)
JAnimationPanel - Animationen für Swing/AWT
SWTRatingBar (Bewertungs-Composite) selbst programmieren
____________________________________________________________________________
Über eine Bewertung (Stern links unter dem Beitrag) oder ein Danke freue ich mich sehr.
Ähnliche Themen
-
E-Mail versenden mit Anhang per SMTP
Von Xing_bimbo im Forum PHPAntworten: 1Letzter Beitrag: 10.06.10, 13:52 -
Mail via SMTP versenden
Von Avedo im Forum PHPAntworten: 19Letzter Beitrag: 14.08.08, 07:58 -
e-Mail versenden über POP/SMTP
Von liquidbeats im Forum PHPAntworten: 12Letzter Beitrag: 20.08.05, 15:27 -
VB6.0 mail versenden smtp
Von itsme123 im Forum Visual Basic 6.0Antworten: 1Letzter Beitrag: 04.03.05, 17:22 -
Camspy SMTP --- Kein E-Mail Upload auf SMTP Server
Von wighlander im Forum Internet, DSL & FlatrateAntworten: 1Letzter Beitrag: 24.02.05, 03:47





Zitieren
Login





