[JavaMail] resend the message using a different SMTP server?

M

mrby

Hi, guys:

A problem about JavaMail, how could I resend
a message using a different SMTP server?

This is the my code snippet:
....
Properties props = new Properties();
debug.op("email.send: host=" + host );
props.put("mail.smtp.host", host );
Session session = Session.getDefaultInstance(props, null);
try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients( Message.RecipientType.TO,
InternetAddress.parse(total_to_list) );
msg.setSubject(subject);
msg.setText(total_message);
Transport.send(msg);
}
catch (MessagingException mex) {
// If exception occurs here, I want to change the SMTP
// server and resend the message. How could I do?
// Is there a nice way besides retyping those code?
}

Thanks much!
 
R

Roedy Green

A problem about JavaMail, how could I resend
a message using a different SMTP server?

Do you mean send the message to two different SMTP servers, or pick it
up from one server and relay it to another?
 
R

Roedy Green

Properties props = new Properties();
debug.op("email.send: host=" + host );
props.put("mail.smtp.host", host );
Session session = Session.getDefaultInstance(props, null);
try {
// create a message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients( Message.RecipientType.TO,
InternetAddress.parse(total_to_list) );
msg.setSubject(subject);
msg.setText(total_message);
Transport.send(msg);
You need TWO session objects. What is a big infuriating is the session
gets built into the Message. If I were designing JavaMail I would not
have a Session at all. You would simply specify the Session info to
the Transport constructor. But that is not how they did it.

So you have to put the body of the program in a method, and pass it
the session. Then call it twice with two different sessions or two
diffrent smtp hosts, and have the method create the session.
 
G

GaryM

You need TWO session objects. What is a big infuriating is the session
gets built into the Message. If I were designing JavaMail I would not
have a Session at all. You would simply specify the Session info to
the Transport constructor. But that is not how they did it.

So you have to put the body of the program in a method, and pass it
the session. Then call it twice with two different sessions or two
diffrent smtp hosts, and have the method create the session.

I tested something like this before, as IIRC you can just set the
"mail.smtp.host" property again without needed to creating another
Session object. These would assume sequential sending with the property
changing in between sends.

If that does not work, then the original poster should rememeber to do
Session.getInstance as opposed to Session.getDefaultInstance() to ensure
he/she has distinct objects.
 
M

mrby

Roedy Green said:
You need TWO session objects. What is a big infuriating is the session
gets built into the Message. If I were designing JavaMail I would not
have a Session at all. You would simply specify the Session info to
the Transport constructor. But that is not how they did it.

So you have to put the body of the program in a method, and pass it
the session. Then call it twice with two different sessions or two
diffrent smtp hosts, and have the method create the session.
Thanks much!
Now I see I should put those code into a separate method.
 
G

GaryM

mrby said:
Thanks much!
Now I see I should put those code into a separate method.

Having 2 methods will work, but try this, first:

[your code is OK to here]
Transport.send(msg); // send using mail host 1
props.put("mail.smtp.host", hostNumber2 );
Transport.send(msg); // send using mail host 2

I am fairly sure it will work fine, but I can't test it in advance for
you at the moment

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Javamail - error SMTP 554 1
Javamail smtp port number 3
Problem about JavaMail 1
Javamail problem 0
JavaMail Encoding Setting 0
javax mail smtp + proxy auth problem 0
java mail api 3
javamail send error 5

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top