KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > SimpleMail


1 package dinamica;
2
3 import java.util.Properties JavaDoc;
4 import javax.mail.*;
5 import javax.mail.internet.*;
6
7 /**
8  * Utility class to send mail using SMTP (JavaMail).<br>
9  * Provides very simple methods for sending text based emails.
10  *
11  * <br>
12  * Creation date: jan/14/2004<br>
13  * Last Update: Oct/10/2004<br>
14  * (c) 2004 Martin Cordova<br>
15  * This code is released under the LGPL license<br>
16  * @author Martin Cordova
17  * */

18 public class SimpleMail
19 {
20     
21     /**
22      * Send a simple text message using a background thread
23      * @param host Mail server address
24      * @param from valid email address
25      * @param fromName Descriptive from name
26      * @param to valid email address
27      * @param subject Email subject
28      * @param body Message body (text only)
29      */

30     public void send(String JavaDoc host, String JavaDoc from, String JavaDoc fromName, String JavaDoc to, String JavaDoc subject, String JavaDoc body)
31     {
32         Thread JavaDoc t = new Thread JavaDoc(new BackgroundSender(host, from, fromName, to, subject, body));
33         t.start();
34     }
35
36     /**
37      * BackgroundSender<br>
38      * Send email using a separate Thread to avoid blocking
39      * Creation date: 29/07/2004<br>
40      * (c) 2004 Martin Cordova y Asociados<br>
41      * http://www.martincordova.com<br>
42      * @author Martin Cordova dinamica@martincordova.com
43      */

44     class BackgroundSender implements Runnable JavaDoc
45     {
46
47         String JavaDoc host = null;
48         String JavaDoc from = null;
49         String JavaDoc fromName = null;
50         String JavaDoc to = null;
51         String JavaDoc subject = null;
52         String JavaDoc body = null;
53         
54         public BackgroundSender(String JavaDoc host, String JavaDoc from, String JavaDoc fromName, String JavaDoc to, String JavaDoc subject, String JavaDoc body)
55         {
56             this.host = host;
57             this.from = from;
58             this.fromName = fromName;
59             this.to = to;
60             this.subject = subject;
61             this.body = body;
62         }
63         
64         public void run()
65         {
66             
67             try
68             {
69                 //init email system
70
Properties JavaDoc props = System.getProperties();
71                 props.put( "mail.smtp.host", host );
72                 Session session = Session.getDefaultInstance( props, null );
73                 session.setDebug( false );
74                 
75                 //recipients
76
InternetAddress[] toAddrs = null;
77                 toAddrs = InternetAddress.parse( to, false );
78                 
79                 //create message
80
Message msg = new MimeMessage( session );
81                 msg.setRecipients( Message.RecipientType.TO, toAddrs );
82                 msg.setSubject( subject );
83                 msg.setFrom( new InternetAddress( from, fromName ) );
84                 msg.setText( body );
85                 
86                 //send
87
Transport.send( msg );
88             }
89             catch (Throwable JavaDoc e)
90             {
91                 try
92                 {
93                     String JavaDoc d = StringUtil.formatDate(new java.util.Date JavaDoc(), "yyyy-MM-dd HH:mm:ss");
94                     System.err.println("ERROR [dinamica.SimpleMail.BackgroundSender.run@" + d + "]: " + e.getMessage());
95                 }
96                 catch (Throwable JavaDoc e1)
97                 {
98                 }
99             }
100             
101         }
102         
103     }
104
105 }
106
Popular Tags