You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Daniel Perry <d....@netcase.co.uk> on 2004/05/18 19:22:02 UTC

Sandbox-Email Background sending of mail

Hi,
I've just started using the sandbox-email classes, and was wondering if
anyone has written a class to send mail in a background thread?

If not then i will probably write some code to do this.

Thanks,

Daniel.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: Sandbox-Email Background sending of mail (code included)

Posted by Daniel Perry <d....@netcase.co.uk>.
duh... attachment seemed to get lost....
BackgroundSender.java follows:

/*
 * Created on 19-May-2004
 *
 */
package org.apache.commons.mail;

import javax.mail.MessagingException;

/**
 * A class to send messages in the background. Compose the mail as normal,
then
 * use the static method: BackgroundSender.send(Email mail) to send the
email.
 *
 * @author Daniel Perry
 */
public class BackgroundSender extends Thread {

    Email theMail;

    /**
     * Constructor takes an Email object, and sends it from a background
thread.
     */
    private BackgroundSender(Email mail) {
        super();
        this.theMail = mail;
    }

    /**
     * Static method to send a org.apache.commons.mail.Email message in the
     * background. Note that if the message fails to get sent there is NO
     * notification!
     *
     * @param mail
     */
    public static void send(Email mail) {
        new BackgroundSender(mail).start();
    }

    /**
     * The run method of the thread that actually sends the mail.
     */
    public void run() {
        try {
            theMail.send();
        } catch (MessagingException ex) {
            // do nothing here as this is running in a background thread,
and
            // noone will do anything about it.
        }
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: Sandbox-Email Background sending of mail (code included)

Posted by Daniel Perry <d....@netcase.co.uk>.
I'm sending email from a webapp.  Certain events cause several emails to be
sent off.  As smtp servers can be a bit on the slow side, i thought it
prudent to send them in a background process to stop users going for the
retry/stop buttons. (note that the retry would be ignored anyway due to
tokens)

Two ways i thought of - queue them and have a sending thread (more reliable,
but also much more complex) or just send each mail in a new thread (not
guarenteed delivery but very simple)

Anyway, i've attached the code for my solution.

To use it, simple put a mail message together as normal, then send it using:

BackgroundSender.send(myMailMessage);

And it will do the rest for you.

Sending 5 mails using msg.send() takes my app about 10 seconds.
Sending 5 mails using the above method takes <1 second (to the user anyway!)

Daniel.

> -----Original Message-----
> From: Joe Germuska [mailto:Joe@Germuska.com]
> Sent: 18 May 2004 18:52
> To: Jakarta Commons Developers List
> Subject: Re: Sandbox-Email Background sending of mail
>
>
> At 6:22 PM +0100 5/18/04, Daniel Perry wrote:
> >Hi,
> >I've just started using the sandbox-email classes, and was wondering if
> >anyone has written a class to send mail in a background thread?
> >
> >If not then i will probably write some code to do this.
>
> Have you really found the process of sending messages to be so
> heavyweight as to merit such a thing?  I can imagine (and have
> written) processes which send email which take enough time to need to
> be in a separate thread, but the part that took time was assembling
> the email, not sending it.
>
> Still, if you see the need, more power to you!  The instinct to share
> is certainly appreciated!
>
> Joe
>
> --
> Joe Germuska
> Joe@Germuska.com
> http://blog.germuska.com
>        "Imagine if every Thursday your shoes exploded if you tied them
> the usual way.  This happens to us all the time with computers, and
> nobody thinks of complaining."
>              -- Jef Raskin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>


Re: Sandbox-Email Background sending of mail

Posted by Joe Germuska <Jo...@Germuska.com>.
At 6:22 PM +0100 5/18/04, Daniel Perry wrote:
>Hi,
>I've just started using the sandbox-email classes, and was wondering if
>anyone has written a class to send mail in a background thread?
>
>If not then i will probably write some code to do this.

Have you really found the process of sending messages to be so 
heavyweight as to merit such a thing?  I can imagine (and have 
written) processes which send email which take enough time to need to 
be in a separate thread, but the part that took time was assembling 
the email, not sending it.

Still, if you see the need, more power to you!  The instinct to share 
is certainly appreciated!

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org