Discussion:
[Classpathx-javamail] sending emails with authentication
Daniel Rindt
2010-09-13 22:29:53 UTC
Permalink
Hello,

days ago i try to send emails with an external mailserver which is
requesting authentication. I followed the examples from suns
javamail-api page. But unfortunately i have no luck to get errors or an
email to the destination adress nor i got informations from
session.setDebugOut(new PrintStream(new File("/var/tmp/javamail.out")));

Maybe someone can please point me to a working example?

TIA
Daniel
Chris Burdess
2010-09-14 09:03:28 UTC
Permalink
Post by Daniel Rindt
days ago i try to send emails with an external mailserver which is
requesting authentication. I followed the examples from suns
javamail-api page. But unfortunately i have no luck to get errors or an
email to the destination adress nor i got informations from
session.setDebugOut(new PrintStream(new File("/var/tmp/javamail.out")));
Maybe someone can please point me to a working example?
Not only do you have to call Session.setDebugOut but also Session.setDebug(true).

GNU JavaMail does support both session-properties-based credentials and credentials supplied directly to Store.connect - please view the README if you are unsure.
--
Chris Burdess
Chris Burdess
2010-09-14 10:46:41 UTC
Permalink
Post by Chris Burdess
GNU JavaMail does support both session-properties-based credentials
and credentials supplied directly to Store.connect - please view the
README if you are unsure.
I tested now a different Mailserver and this is working as expected. But
how can i see what is going wrong with the mailserver i want to use? See
above, i don't get any information about whats going wrong there. Maybe
this is a TLS issue?
TLS shouldn't make any difference. We switched over the logging to use the java.util.logging framework a while ago - although setDebugOut should override that, perhaps there is a problem there. You could always try using a standard logging.properties file with the debug levels you need.
--
Chris Burdess
Daniel Rindt
2010-09-15 13:14:51 UTC
Permalink
Post by Chris Burdess
You could always try using a standard logging.properties file with the
debug levels you need.
Hello Chris,

sorry to reply again, but i can't solve the problem. The mail api is not
giving me any output. The Emailhoster (Rackspace) is very motivated to
find the problem, but unfortunately it was still not possible. I play
around with the logging, watch the logs but i can't see anything from
the mail api stuff. session.setDebugOut(new PrintStream(new
File("/var/tmp/javamail.out"))); produces only a file with zero bytes.
I have absolutely no idea how i can debug the issue. It works well with
other mailservers.

TIA
Daniel
Chris Burdess
2010-09-16 07:27:00 UTC
Permalink
Post by Daniel Rindt
sorry to reply again, but i can't solve the problem. The mail api is not
giving me any output. The Emailhoster (Rackspace) is very motivated to
find the problem, but unfortunately it was still not possible. I play
around with the logging, watch the logs but i can't see anything from
the mail api stuff. session.setDebugOut(new PrintStream(new
File("/var/tmp/javamail.out"))); produces only a file with zero bytes.
The PrintStream(File) constructor specifically says that it won't flush the underlying stream. You will need to call flush manually on that PrintStream, or use the OutputStream constructor.
--
Chris Burdess
Daniel Rindt
2010-09-19 02:33:25 UTC
Permalink
Post by Chris Burdess
The PrintStream(File) constructor specifically says that it won't
flush the underlying stream. You will need to call flush manually on
that PrintStream, or use the OutputStream constructor.
Yes. But this is also not working:
=== 8< ===
Session session = Session.getInstance(props, auth);
session.setDebug(true);
PrintStream ps = null;
try {
ps = new PrintStream("/tmp/classpathx-mail.out");
session.setDebugOut(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
recipientsAddress, false));
msg.setSubject(subject);
msg.setText(text);
msg.saveChanges();
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
ps.flush();
ps.close();
=== 8< ===
I got nothing in the file, its just zero bytes. Thanks for further help.

TIA
Daniel
Chris Burdess
2010-09-20 07:40:24 UTC
Permalink
Post by Daniel Rindt
Post by Chris Burdess
The PrintStream(File) constructor specifically says that it won't
flush the underlying stream. You will need to call flush manually on
that PrintStream, or use the OutputStream constructor.
=== 8< ===
Session session = Session.getInstance(props, auth);
session.setDebug(true);
PrintStream ps = null;
try {
ps = new PrintStream("/tmp/classpathx-mail.out");
session.setDebugOut(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
recipientsAddress, false));
msg.setSubject(subject);
msg.setText(text);
msg.saveChanges();
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
ps.flush();
ps.close();
=== 8< ===
I got nothing in the file, its just zero bytes. Thanks for further help.
I amended your code and got it to work:

Session session = Session.getInstance(props, auth);
session.setDebug(true);
OutputStream fos = null;
try {
fos = new FileOutputStream("classpathx-mail.out");
PrintStream ps = new PrintStream(fos);
session.setDebugOut(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
recipientsAddress, false));
msg.setSubject(subject);
msg.setText(text);
msg.saveChanges();
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
//fos.close();

Note that if you close the FileOutputStream at the end you won't get any output. This is down to some weirdness in exactly why/how/when PrintStreams get flushed, which I'm afraid I don't understand myself, but it's not a problem in the logging as far as I can see (which as I said simply uses the java.util.logging mechanism. If you find any way to shed any light on what's happening there please share it.

Regards
--
Chris Burdess
Daniel Rindt
2010-09-21 01:47:29 UTC
Permalink
Hi Chris!

i don't understand it. I used exactly your code, and i still got again a
file with zero size. No information, nothing more.
You have suggestions for me how to get working the logging stuff? That
would be nice if you have some informations for me. I tested now
different mailservers, and they all work fine. But not with the
preferred mailserver from rackspace.

TIA
Daniel
Chris Burdess
2010-09-21 07:47:21 UTC
Permalink
Post by Daniel Rindt
Hi Chris!
i don't understand it. I used exactly your code, and i still got again a
file with zero size. No information, nothing more.
You have suggestions for me how to get working the logging stuff? That
would be nice if you have some informations for me. I tested now
different mailservers, and they all work fine. But not with the
preferred mailserver from rackspace.
If this is purely to debug an issue rather than a long-term logging solution, just call Session.setDebug(true) and don't call Session.setDebugOut and it will log to the console. As I said I don't know myself why its behaviour is so unpredictable when using PrintStreams connected to files.
--
Chris Burdess
Daniel Rindt
2010-09-21 07:53:05 UTC
Permalink
Post by Chris Burdess
If you find any way to shed any light on what's happening there please
share it.
Hey Chris,

i recorded the communication between my host and the mailserver during
the act of the mail api. It's attached. Maybe you can open it with some
hex editor and can see what's going wrong there. I have also to mention,
change only the credentials and mailserver to a different one is
working.
It looks like he is trying various authentication mechanisms, because i
havent specified one with "mail.smtp.auth.mechanisms". I definitely know
that "LOGIN" is supported, because my "Evolution" is using it.

TIA
Daniel
Chris Burdess
2010-09-27 14:39:20 UTC
Permalink
thanks for your efforts. This example is connecting via imaps to the
mailserver properly seems. But covers not my use case, i want sending a
email.
=== 8< ===
Transport transport = session.getTransport(url);
=== 8< ===
I couldn't use imaps i got an exeption that no Provider is available
for. So i use just imap for testing. But when i wan't the transport
=== 8< ===
javax.mail.NoSuchProviderException: invalid provider
=== 8< ===
So i would like to ask you if you can provide please a working example
for sending emails with that server? Thanks.
IMAP is only a mail retrieval protocol, you cannot get a transport for this provider. You need a transport protocol like SMTP:

Transport transport = session.getTransport("smtp");

For how to send a message, see the testSendMessage method of test/src/TransportTest.java in the GNUMail distribution.
--
Chris Burdess
Daniel Rindt
2010-09-28 00:47:11 UTC
Permalink
Post by Chris Burdess
For how to send a message, see the testSendMessage method of
test/src/TransportTest.java in the GNUMail distribution.
Hello Chris,

sorry for replying again to you. I played now for hours, read
documentation and followed your example. But i have no luck to get an
email out.
My Code is attached, maybe you can find what i have oversight so that it
work (or maybe i am to stupid). I am very thankful for your support and
help.

Have a great week, Thanks
--
Daniel Rindt <***@visetics.com>
Visetics
Continue reading on narkive:
Loading...