Link to home
Create AccountLog in
Avatar of kerzner
kerznerFlag for United States of America

asked on

JavaMail - keeping session alive

When emailing using this java code,

Session session = Session.getInstance(props, new MyAuthenticator(username, password));
.... settings
tr = session.getTransport("smtp");
tr.send(msg);

after some period of inactivity, I get this error

550-currently not permitted to relay through this server. Perhaps you have not
550-logged into the pop/imap server in the last 30 minutes or do not have SMTP
550 Authentication turned on in your email client.

I I run an email client, (i.e., Evolution) which check email every 10 minutes, my code always works. But this does not seem like a clean solution. How do I keep my session alive?

Thank you,
Mark
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
You can of course implement that with a Timer. You'd then have something like this in your send method:

if (needNewSession) {
    getNewSession();
}
// Carry on with sending
Avatar of kerzner

ASKER

But I always get new session!.

I mean, I am not using this old connection; every time I need to send an email, I execute this code above. So it is always a new connection, or should be.

Nevertheless, every mail client beats me. I had this error in windows, and now I am having it in Linux. What does mail client to different from me?
Avatar of kerzner

ASKER

I saw a similar question on the Ruby forum and the solution was

Net::SMTP.start('localhost', 25, 'localhost.localdomain', 'mickey', 'cheese', :login) do |smtp|

how do you do the same thing in java? In other words, how do you login rather than grab an already existing session?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
:-)