Retrieve from POP3 and send via SMTP

Hello!

I'm working on a POP3 mail fetcher: a program which downloads messages from a POP3 server and delivers them to an SMTP one.

I found a problem trying to send the message I get from a POP3 folder, like:

Message[] messages = folder.getMessages();

for(Message message : messages)

Transport.send(message);

I always got an error about POP3Message being read only.

The solution I found is:

Message[] messages = folder.getMessages();

for(Message message : messages) {

MimeMessage myMessage = new MimeMessage((MimeMessage)message);

Transport.send(myMessage);

}

Is it correct? Is there a better way?

Thanks

[695 byte] By [LuCa@iTaLya] at [2007-11-27 10:31:43]
# 1

Transport.send is going to call Message.saveChanges to make sure the

Message-ID for the message is updated. If you want to send the message

using the same Message-ID and the same recipients (in which case isn't it

just going to go back to the same mailbox you fetched it from?), you'll need

to use the Transport.sendMessage method, which avoids the call to saveChanges.

See the JavaMail FAQ for how to use that method.

Let me know if that doesn't work.

bshannona at 2007-7-28 18:11:35 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hello!

Thanks: using the method you suggested, it works perfectly!

transport = session.getTransport("smtp");

transport.connect();

transport.sendMessage(message, message.getAllRecipients());

transport.close();

As I change SMTP server (using the internal one) in my session properties, the message is delivered to a different server.

bye

LuCa@iTaLya at 2007-7-28 18:11:35 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...