1 53 54 package com.Yasna.util; 55 56 57 import com.Yasna.forum.PropertyManager; 58 59 import java.util.*; 60 import javax.mail.*; 61 import javax.mail.internet.*; 62 import javax.naming.*; 63 64 public final class MailSender extends Thread { 65 66 public static final String TEST_HOST = "hurricane"; 67 public static final String TEST_FROM = "aflatoon@test.com"; 68 public static final String TEST_TO = "aflatoon@test.com"; 69 public static final String TEST_SUBJECT = "SMTPMail Test"; 70 public static final String TEST_MESSAGE = "This is a test."; 71 72 private String host; 73 private String from; 74 private String to; 75 private String cc = null; 76 private String bcc = null; 77 private String subject; 78 private String message; 79 private String smtpusername= PropertyManager.getProperty("yazdMailSMTPServer.username"); 80 private String smtppassword= PropertyManager.getProperty("yazdMailSMTPServer.password"); 81 82 83 public MailSender ( String strHost, 84 String strFrom, 85 String strTo, 86 String strCc, 87 String strBcc, 88 String strSubject, 89 String strMessage 90 ) { 91 92 host = strHost == null ? TEST_HOST : strHost ; 93 from = strFrom == null ? TEST_FROM : strFrom ; 94 to = strTo == null ? TEST_TO : strTo ; 95 cc = strCc; 96 bcc = strBcc; 97 subject = strSubject == null ? TEST_SUBJECT : strSubject ; 98 message = strMessage == null ? TEST_MESSAGE : strMessage ; 99 } 100 101 public void run() { 102 try { 103 sendIt(); 104 } catch(Exception e) { 105 System.out.println("Yazd MailSender - Exception while sending a mail "+e); 106 } 107 } 108 109 private void sendIt() throws Exception { 110 Properties properties; 111 MimeMessage msg; 112 Session session; 113 126 properties = new Properties(); 127 properties.put("mail.smtp.host", host); 128 session = Session.getInstance(properties, null); 129 msg = new MimeMessage(session); 130 msg.addFrom(new InternetAddress[] {new InternetAddress(from)}); 131 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 132 if (cc != null) { 133 msg.addRecipient(Message.RecipientType.CC, 134 new InternetAddress(cc)); 135 } 136 if (bcc != null) { 137 msg.addRecipient(Message.RecipientType.BCC, 138 new InternetAddress(bcc)); 139 } 140 msg.setSubject(subject); 141 msg.setText(message); 142 if(smtpusername!=null && smtppassword !=null){ 145 Transport tr = session.getTransport(); 146 tr.connect(host, smtpusername, smtppassword); 147 msg.saveChanges(); tr.sendMessage(msg, msg.getAllRecipients()); 149 tr.close(); 150 System.out.println("Yazd Forum Software (MailSender.java)-- (authsmtp) Mail sent successfully to:"+to); 151 }else{ 152 Transport.send(msg); 153 System.out.println("Yazd Forum Software (MailSender.java)-- Mail sent successfully to:"+to); 154 } 155 } 156 157 public static void send( String _host, String _from, String _to, String _cc, 158 String _bcc, String _subject, String _message) 159 { 160 MailSender sender = new MailSender( _host, _from, _to, _cc, _bcc, _subject, _message); 161 sender.start(); 162 } 163 164 public static void send( String _host, String _from, String _to, 165 String _subject, String _message) 166 { 167 send(_host, _from, _to, null, null, _subject, _message); 168 } 169 170 public void sendForTest( ) throws Exception 171 { 172 sendIt(); 173 } 174 175 public static void send( String _host, String _from, String _to, String _cc, 176 String _subject, String _message) 177 { 178 send(_host, _from, _to, _cc, null, _subject, _message); 179 } 180 } 181 | Popular Tags |