1 package dinamica; 2 3 import java.util.Properties ; 4 import javax.mail.*; 5 import javax.mail.internet.*; 6 7 18 public class SimpleMail 19 { 20 21 30 public void send(String host, String from, String fromName, String to, String subject, String body) 31 { 32 Thread t = new Thread (new BackgroundSender(host, from, fromName, to, subject, body)); 33 t.start(); 34 } 35 36 44 class BackgroundSender implements Runnable 45 { 46 47 String host = null; 48 String from = null; 49 String fromName = null; 50 String to = null; 51 String subject = null; 52 String body = null; 53 54 public BackgroundSender(String host, String from, String fromName, String to, String subject, String 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 Properties props = System.getProperties(); 71 props.put( "mail.smtp.host", host ); 72 Session session = Session.getDefaultInstance( props, null ); 73 session.setDebug( false ); 74 75 InternetAddress[] toAddrs = null; 77 toAddrs = InternetAddress.parse( to, false ); 78 79 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 Transport.send( msg ); 88 } 89 catch (Throwable e) 90 { 91 try 92 { 93 String d = StringUtil.formatDate(new java.util.Date (), "yyyy-MM-dd HH:mm:ss"); 94 System.err.println("ERROR [dinamica.SimpleMail.BackgroundSender.run@" + d + "]: " + e.getMessage()); 95 } 96 catch (Throwable e1) 97 { 98 } 99 } 100 101 } 102 103 } 104 105 } 106 | Popular Tags |