Go to the documentation of this file.00001
00002 import java.util.Properties;
00003 import java.util.concurrent.LinkedBlockingQueue;
00004
00005 import javax.mail.Message;
00006 import javax.mail.Message.RecipientType;
00007 import javax.mail.MessagingException;
00008 import javax.mail.Session;
00009 import javax.mail.Transport;
00010 import javax.mail.internet.InternetAddress;
00011 import javax.mail.internet.MimeMessage;
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 public class MailTransport implements Runnable
00045 {
00046
00047
00048
00049
00050 public interface Context
00051 {
00052
00053
00054
00055
00056
00057 public abstract void println( String str );
00058
00059
00060
00061
00062
00063
00064 public abstract void onSendCompleted( String errorMessage );
00065 }
00066
00067
00068
00069
00070 private Thread workerThread = null;
00071
00072
00073
00074
00075 private Context context = null;
00076
00077
00078
00079
00080 private Session session = null;
00081
00082
00083
00084
00085 private LinkedBlockingQueue<Message> queue = null;
00086
00087
00088
00089
00090
00091 private Message quitSignal = null;
00092
00093
00094
00095
00096
00097 public MailTransport( Context context, String smtpServer, int smtpPort )
00098 {
00099 this.context = context;
00100 this.queue = new LinkedBlockingQueue<Message> ();
00101 this.quitSignal = new MimeMessage( this.session );
00102
00103 Properties relayHost = new Properties();
00104 relayHost.put( "mail.smtp.host", smtpServer );
00105 relayHost.put( "mail.smtp.port", smtpPort );
00106
00107
00108
00109
00110 this.session = Session.getInstance( relayHost, null );
00111 }
00112
00113
00114
00115
00116 public int getQueueSize ()
00117 {
00118 return this.queue.size ();
00119 }
00120
00121
00122
00123
00124 public void startSending ()
00125 {
00126 synchronized( this )
00127 {
00128 if ( workerThread != null ) {
00129 return;
00130 }
00131
00132 this.workerThread = new Thread( this );
00133 this.workerThread.start ();
00134 }
00135 }
00136
00137
00138
00139
00140 public void noMoreMessages ()
00141 {
00142 synchronized( this )
00143 {
00144
00145
00146 try {
00147 this.queue.put( this.quitSignal );
00148 } catch( InterruptedException e ) {
00149
00150 }
00151 }
00152 }
00153
00154
00155
00156
00157 public void enqueueMessage( String from,
00158 String[] to, String subject, String textMessage
00159 ) throws MessagingException
00160 {
00161 InternetAddress[] toAddr = new InternetAddress[ to.length ];
00162 for( int i = 0; i < to.length; i++ ) {
00163 toAddr[i] = new InternetAddress( to[i] );
00164 }
00165
00166 MimeMessage mimeMsg = new MimeMessage( session );
00167 mimeMsg.setFrom( new InternetAddress( from ) );
00168 mimeMsg.setRecipients( Message.RecipientType.TO, toAddr );
00169 mimeMsg.setHeader( "X-Mailer", "IP1-MailSender" );
00170 mimeMsg.setSubject( subject );
00171 mimeMsg.setContent( textMessage, "text/plain" );
00172
00173
00174
00175 try {
00176 this.queue.put( mimeMsg );
00177 } catch( InterruptedException e ) {
00178
00179 }
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 @Override
00191 public void run ()
00192 {
00193 context.println( "MailTransport(" + workerThread.getId ()
00194 + "): Worker thread started..." );
00195
00196 context.println( "MailTransport(" + workerThread.getId ()
00197 + "): Found " + queue.size() + " messages in queue." );
00198
00199 String errorMessage = null;
00200 int curMessageNo = 0;
00201
00202 while( true )
00203 {
00204 Message msgToSend = null;
00205
00206
00207
00208
00209
00210 try {
00211 context.println( "MailTransport(" + workerThread.getId ()
00212 + "): Waiting for messsages..." );
00213 msgToSend = this.queue.take ();
00214 } catch( InterruptedException e ) {
00215 break;
00216 }
00217
00218
00219
00220
00221 if ( msgToSend == quitSignal || msgToSend == null ) {
00222 context.println( "MailTransport(" + workerThread.getId ()
00223 + "): Got signal to 'QUIT'..." );
00224 break;
00225 }
00226
00227
00228
00229 try
00230 {
00231 ++curMessageNo;
00232
00233 context.println( "MailTransport(" + workerThread.getId ()
00234 + "): Sending message #" + curMessageNo + "..." );
00235
00236 for( javax.mail.Address a : msgToSend.getFrom() ) {
00237 context.println( " From: " + a.toString () );
00238 }
00239 for( javax.mail.Address a : msgToSend.getRecipients( RecipientType.TO ) ) {
00240 context.println( " To: " + a.toString () );
00241 }
00242 context.println( " Subject: " + msgToSend.getSubject () );
00243
00244 Transport.send( msgToSend );
00245
00246 context.println( "MailTransport(" + workerThread.getId ()
00247 + "): MESSAGE SENT!" );
00248 }
00249 catch( MessagingException e )
00250 {
00251 context.println( "MailTransport(" + workerThread.getId ()
00252 + "): Error\n\n" + e.toString () + "\n" );
00253
00254 errorMessage = e.toString ();
00255
00256 break;
00257 }
00258 }
00259
00260 context.println( "MailTransport(" + workerThread.getId ()
00261 + "): Thread done." );
00262
00263 context.onSendCompleted( errorMessage );
00264 }
00265 }
00266