• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Mail API Problem

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I am using java mail API now to send emails from my project.
I create a transport object and send messages.
But they are sent if I automatically close the transport after sending.
Smth like this
//creating transport........
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

If I comment the last line the message is sent only after I exit the program which
frees all the resources and threrefore implicitly closes the transport object.
But I need to create one transport and hold it open unless the main JFrame is closed.
Waiting for reply........
Thank you!
Fedai!
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use Transport.send instead of sendMessage with no problem.

Also note from the javadocs of sendMessage, it says "Unlike the static send method, the sendMessage method does not call the saveChanges method on the message; the caller should do so."
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is like the following

newRec = new InternetAddress(Recipient);
mailMsg.setText(Message);
mailMsg.setRecipient(MimeMessage.RecipientType.TO, newRec);
mailMsg.saveChanges();
mailTransp.sendMessage(mailMsg, mailMsg.getAllRecipients());
//mailTransp.close();
If I do not close mailTransp it's not sent, I mean I call saveChanges().
Is there a way of doing what I want not by means of Transport but
transport = mailSession.getTransport("smtp")?
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still waiting for help!
 
Sheriff
Posts: 26773
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your debug output look like in the two cases?
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both cases the debug is the same except for this.
In case of closing the trasport object the last output is this
..................................................
250 Mail queued for delivery.
QUIT
221 Closing connection. Good bye.
In case of not closing just this
................................................
250 Mail queued for delivery.
In my project I start sending hundreds of messages
upon one button click.
This Transport.send(message) as the doc. says makes a new connection
each time i.e. in creates a new transport object, connects to the server,
sends the message.
And I have a Retry button. But I dont want to connect to the server
each time a user clicks the Retry button or sends again. This is ineffective.
Waiting for reply........
Thank you very much!
 
Paul Clapham
Sheriff
Posts: 26773
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fedai gandjaliyev:
And I have a Retry button. But I dont want to connect to the server each time a user clicks the Retry button or sends again. This is ineffective.

I don't know what "ineffective" might mean there. So I can't say what to do differently. But if you mean there is a pause while the messages are sent, then why don't you have a separate thread that connects to the server and sends the messages? Have your GUI put the messages on the back of a queue and have that separate thread read them from the front of the queue.
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand there is no way of creating one transport object,
keeping it open for sending messages and closing it when neccesary.
Right?
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I need to do is the following.
Connect to the SMTP server once and send messages over different periods of time and disconnect from it only when I exit the application.
Transport.send(message) can be used to accomplish it. But according to the documentation for each it's "send" method it creates a separate connection.
That means it is comparetively slow.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic