00001
00002 import java.awt.Color;
00003 import java.awt.Dimension;
00004 import java.awt.Font;
00005 import java.awt.Toolkit;
00006 import java.awt.event.ActionEvent;
00007 import java.awt.event.ActionListener;
00008 import java.awt.event.WindowEvent;
00009 import java.text.SimpleDateFormat;
00010 import java.util.Calendar;
00011
00012 import javax.mail.MessagingException;
00013 import javax.swing.GroupLayout;
00014 import javax.swing.JButton;
00015 import javax.swing.JFrame;
00016 import javax.swing.JLabel;
00017 import javax.swing.JOptionPane;
00018 import javax.swing.JPanel;
00019 import javax.swing.JScrollPane;
00020 import javax.swing.JTabbedPane;
00021 import javax.swing.JTextArea;
00022 import javax.swing.JTextField;
00023 import javax.swing.WindowConstants;
00024 import javax.swing.plaf.metal.MetalBorders;
00025
00026
00027
00028
00029
00030
00031
00032 public class SendMailApp extends JFrame
00033 implements ActionListener, MailTransport.Context
00034 {
00035
00036
00037
00038 private static final long serialVersionUID = 6679954030573701419L;
00039
00040
00041
00042
00043 private final static String appTitle = "IP1-6.1: SMTP Send Mail";
00044
00045
00046
00047
00048 private JButton buttonEnqueue;
00049 private JButton buttonSend;
00050
00051 private JLabel labelServer;
00052 private JTextField fieldServer;
00053 private JLabel labelFrom;
00054 private JTextField fieldFrom;
00055 private JLabel labelTo;
00056 private JTextField fieldTo;
00057 private JLabel labelSubject;
00058 private JTextField fieldSubject;
00059 private JScrollPane messageScrollPane;
00060 private JTextArea messageContents;
00061
00062 private JTabbedPane tabbedPane;
00063
00064 private JScrollPane logPane;
00065 private JTextArea logArea;
00066
00067
00068
00069
00070 private MailTransport mailTransport = null;
00071
00072
00073
00074
00075 public SendMailApp ()
00076 {
00077 super( appTitle + ", idle" );
00078
00079 setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
00080
00081
00082
00083 addWindowListener( new java.awt.event.WindowAdapter () {
00084 public void windowClosing( java.awt.event.WindowEvent evt ) {
00085 formWindowClosing( evt );
00086 }
00087 });
00088
00089 Font btnFont = new Font( Font.SANS_SERIF, Font.BOLD, 12 );
00090 Font textFont = new Font( Font.SANS_SERIF, Font.PLAIN, 15 );
00091 Font logFont = new Font( Font.MONOSPACED, Font.PLAIN, 14 );
00092
00093 buttonEnqueue = new JButton ();
00094 buttonSend = new JButton ();
00095 labelServer = new JLabel ();
00096 fieldServer = new JTextField ();
00097 labelFrom = new JLabel ();
00098 fieldFrom = new JTextField ();
00099 labelTo = new JLabel ();
00100 fieldTo = new JTextField ();
00101 labelSubject = new JLabel ();
00102 fieldSubject = new JTextField ();
00103
00104 labelServer.setText( "STMP Server:" );
00105 labelFrom.setText( "From:" );
00106 labelTo.setText( "To:" );
00107 labelSubject.setText( "Subject:" );
00108
00109 fieldServer.setColumns( 40 );
00110 fieldFrom.setColumns( 40 );
00111 fieldTo.setColumns( 40 );
00112 fieldSubject.setColumns( 40 );
00113
00114 buttonEnqueue.setFont( btnFont );
00115 buttonSend.setFont( btnFont );
00116 labelServer.setFont( textFont );
00117 labelFrom.setFont( textFont );
00118 labelTo.setFont( textFont );
00119 labelSubject.setFont( textFont );
00120 fieldServer.setFont( textFont );
00121 fieldFrom.setFont( textFont );
00122 fieldTo.setFont( textFont );
00123 fieldSubject.setFont( textFont );
00124
00125 messageContents = new JTextArea ();
00126 messageContents.setLineWrap( true );
00127 messageContents.setWrapStyleWord( true );
00128 messageContents.setEditable( true );
00129 messageContents.setFont( textFont );
00130
00131 messageScrollPane = new JScrollPane ();
00132 messageScrollPane.setViewportView( messageContents );
00133
00134 logArea = new JTextArea ();
00135 logArea.setLineWrap( true );
00136 logArea.setWrapStyleWord( true );
00137 logArea.setEditable( false );
00138 logArea.setFont( logFont );
00139 logArea.setBackground( new Color( 255, 255, 240 ) );
00140 logArea.setForeground( Color.BLACK );
00141 logArea.setWrapStyleWord( true );
00142
00143 logPane = new JScrollPane ();
00144 logPane.setViewportView( logArea );
00145
00146 buttonEnqueue.setText( "Enqueue Message" );
00147 buttonEnqueue.addActionListener( this );
00148
00149 buttonSend.setText( "Send Immediatelly" );
00150 buttonSend.addActionListener( this );
00151
00152
00153
00154 JPanel sendMailPane = new JPanel ();
00155 sendMailPane.setBorder( new MetalBorders.Flush3DBorder() );
00156
00157 GroupLayout layout = new GroupLayout( sendMailPane );
00158 sendMailPane.setLayout( layout );
00159 layout.setAutoCreateContainerGaps( true );
00160 layout.setAutoCreateGaps( true );
00161
00162 layout.setHorizontalGroup
00163 (
00164 layout
00165 .createParallelGroup( GroupLayout.Alignment.LEADING )
00166 .addGroup
00167 (
00168 layout
00169 .createSequentialGroup ()
00170 .addGroup
00171 (
00172 layout
00173 .createParallelGroup( GroupLayout.Alignment.TRAILING )
00174 .addComponent( messageScrollPane )
00175 .addGroup
00176 (
00177 GroupLayout.Alignment.LEADING,
00178 layout
00179 .createSequentialGroup ()
00180 .addContainerGap( 10, 10 )
00181 .addGroup
00182 (
00183 layout
00184 .createParallelGroup( GroupLayout.Alignment.LEADING )
00185 .addComponent( labelServer )
00186 .addComponent( labelFrom )
00187 .addComponent( labelTo )
00188 .addComponent( labelSubject )
00189 )
00190 .addGroup
00191 (
00192 layout
00193 .createParallelGroup( GroupLayout.Alignment.LEADING, false )
00194 .addComponent( fieldServer )
00195 .addComponent( fieldFrom )
00196 .addComponent( fieldTo )
00197 .addComponent( fieldSubject )
00198 )
00199 )
00200 .addGroup
00201 (
00202 GroupLayout.Alignment.LEADING,
00203 layout
00204 .createSequentialGroup ()
00205 .addContainerGap( 10, 10 )
00206 .addGroup
00207 (
00208 layout
00209 .createParallelGroup( GroupLayout.Alignment.LEADING, false )
00210 .addComponent( buttonEnqueue )
00211 )
00212 .addGroup
00213 (
00214 layout
00215 .createParallelGroup( GroupLayout.Alignment.LEADING, false )
00216 .addComponent( buttonSend )
00217 )
00218 )
00219 )
00220 )
00221 );
00222
00223 layout.setVerticalGroup
00224 (
00225 layout
00226 .createParallelGroup( GroupLayout.Alignment.LEADING )
00227 .addGroup
00228 (
00229 layout
00230 .createSequentialGroup ()
00231 .addGroup
00232 (
00233 layout
00234 .createParallelGroup( GroupLayout.Alignment.BASELINE )
00235 .addComponent( labelServer )
00236 .addComponent( fieldServer )
00237 )
00238 .addGroup
00239 (
00240 layout
00241 .createParallelGroup( GroupLayout.Alignment.BASELINE )
00242 .addComponent( labelFrom )
00243 .addComponent( fieldFrom )
00244 )
00245 .addGroup
00246 (
00247 layout
00248 .createParallelGroup( GroupLayout.Alignment.BASELINE )
00249 .addComponent( labelTo )
00250 .addComponent( fieldTo )
00251 )
00252 .addGroup
00253 (
00254 layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
00255 .addComponent( labelSubject )
00256 .addComponent( fieldSubject )
00257 )
00258 .addGroup
00259 (
00260 layout.createParallelGroup( GroupLayout.Alignment.BASELINE )
00261 .addComponent( buttonEnqueue )
00262 .addComponent( buttonSend )
00263 )
00264 .addComponent( messageScrollPane )
00265 )
00266 );
00267
00268 pack ();
00269
00270
00271
00272 tabbedPane = new JTabbedPane ();
00273 getContentPane().add( tabbedPane );
00274 tabbedPane.addTab( "Compose Message", sendMailPane );
00275 tabbedPane.addTab( "MTA: Idle", logPane );
00276
00277
00278
00279
00280
00281 Dimension win = new Dimension( 695, 590 );
00282 Dimension scsz = Toolkit.getDefaultToolkit().getScreenSize();
00283 win.width = Math.min( win.width, scsz.width );
00284 win.height = Math.min( win.height, scsz.height - 40 );
00285 setMinimumSize( win );
00286 setSize( win );
00287
00288
00289
00290 setLocation( ( scsz.width - win.width )/2, ( scsz.height - 40 - win.height )/2 );
00291
00292
00293
00294 fieldServer.requestFocus ();
00295 }
00296
00297
00298
00299
00300
00301
00302 private void enqueueMessage( boolean sendImmediately )
00303 {
00304 try
00305 {
00306
00307
00308
00309
00310
00311
00312 synchronized( this )
00313 {
00314 if ( mailTransport == null ) {
00315 mailTransport = new MailTransport( this, fieldServer.getText(), 25 );
00316 fieldServer.setEnabled( false );
00317 }
00318 }
00319
00320
00321
00322 String from = fieldFrom.getText().trim();
00323 String[] recipients = fieldTo.getText().trim().split(",");
00324 String subject = fieldSubject.getText().trim();
00325 String contents = messageContents.getText().trim();
00326
00327
00328
00329 if ( recipients.length > 0 && ! recipients[0].isEmpty () )
00330 {
00331 mailTransport.enqueueMessage( from, recipients, subject, contents );
00332
00333 int msgCount = mailTransport.getQueueSize ();
00334 assert msgCount > 0
00335 : "Ouch!. We have enqueued message, but the queue is still empty?";
00336 if ( msgCount == 1 ) {
00337 setTitle( appTitle + ", 1 message waiting to be sent..." );
00338 } else {
00339 setTitle( appTitle + ", " + msgCount
00340 + " messages waiting to be sent..." );
00341 }
00342
00343
00344
00345 fieldTo.setText( "" );
00346 fieldSubject.setText( "" );
00347 messageContents.setText( "" );
00348 }
00349
00350
00351
00352
00353
00354 if ( sendImmediately && mailTransport.getQueueSize () > 0 )
00355 {
00356 println( "------------------------------------------------------------" );
00357
00358 tabbedPane.setForegroundAt( 1, Color.BLUE );
00359 tabbedPane.setTitleAt( 1, "MTA: Sending..." );
00360 tabbedPane.setSelectedIndex( 1 );
00361 setTitle( appTitle + ", Sending messages..." );
00362
00363 mailTransport.noMoreMessages ();
00364 mailTransport.startSending ();
00365
00366
00367
00368 mailTransport = null;
00369 fieldServer.setEnabled( true );
00370 }
00371 else if ( mailTransport.getQueueSize () == 0 )
00372 {
00373 tabbedPane.setForegroundAt( 1, Color.BLACK );
00374 tabbedPane.setTitleAt( 1, "MTA: Idle" );
00375 setTitle( appTitle + ", idle" );
00376
00377
00378
00379 mailTransport = null;
00380 fieldServer.setEnabled( true );
00381 }
00382 else
00383 {
00384 tabbedPane.setForegroundAt( 1, Color.BLACK );
00385 tabbedPane.setTitleAt( 1, "MTA: Idle" );
00386 }
00387 }
00388 catch( MessagingException e )
00389 {
00390 JOptionPane.showMessageDialog( this,
00391 "Failed to put e-mail message into the queue\n" + e.toString (),
00392 "Error while enqueing the message...", JOptionPane.ERROR_MESSAGE );
00393 }
00394 }
00395
00396
00397
00398
00399 public void clearLog ()
00400 {
00401 synchronized( logArea )
00402 {
00403 logArea.setText( "" );
00404 logArea.setRows( 0 );
00405 logArea.setCaretPosition( logArea.getText().length () );
00406 }
00407 }
00408
00409
00410
00411
00412
00413
00414 private static String now ()
00415 {
00416 Calendar cal = Calendar.getInstance ();
00417 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
00418 return sdf.format( cal.getTime() );
00419 }
00420
00421
00422
00423
00424
00425
00426 @Override
00427 public void println( String str )
00428 {
00429 synchronized( logArea )
00430 {
00431 logArea.append( now () + " " + str + "\n" );
00432 logArea.setRows( logArea.getRows () + 1 );
00433 logArea.setCaretPosition( logArea.getText().length () );
00434 }
00435 }
00436
00437
00438
00439
00440
00441
00442 public void onSendCompleted( String errorMessage )
00443 {
00444 synchronized ( this )
00445 {
00446 int msgCount = mailTransport == null ? 0 : mailTransport.getQueueSize ();
00447 if ( msgCount == 0 ) {
00448 setTitle( appTitle + ", idle" );
00449 } else if ( msgCount == 1 ) {
00450 setTitle( appTitle + ", 1 message waiting to be sent..." );
00451 } else {
00452 setTitle( appTitle + ", " + msgCount
00453 + " messages waiting to be sent..." );
00454 }
00455 }
00456
00457 if ( errorMessage != null ) {
00458
00459 tabbedPane.setForegroundAt( 1, Color.RED );
00460 tabbedPane.setTitleAt( 1, "MTA: Error!" );
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 } else {
00472 tabbedPane.setForegroundAt( 1, new Color( 0, 128, 0 ) );
00473 tabbedPane.setTitleAt( 1, "MTA: OK" );
00474 }
00475 }
00476
00477
00478
00479
00480
00481
00482 private void formWindowClosing( WindowEvent evt )
00483 {
00484
00485
00486 synchronized( this )
00487 {
00488 if ( mailTransport != null ) {
00489 mailTransport.noMoreMessages ();
00490 mailTransport = null;
00491 }
00492 }
00493
00494 System.exit( 0 );
00495 }
00496
00497
00498
00499
00500 public void actionPerformed( ActionEvent evt )
00501 {
00502 boolean sendImmediately = ( evt.getSource () == buttonSend );
00503
00504 enqueueMessage( sendImmediately );
00505 }
00506
00507
00508
00509
00510
00511
00512 public static void main( String args[] )
00513 {
00514 java.awt.EventQueue.invokeLater(
00515 new Runnable() {
00516 public void run() {
00517 new SendMailApp ().setVisible( true );
00518 }
00519 }
00520 );
00521 }
00522 }
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608