Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2010
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3. import javax.mail.Session;
  4. import javax.mail.Transport;
  5. import javax.mail.Message.RecipientType;
  6. import javax.mail.internet.InternetAddress;
  7. import javax.mail.internet.MimeMessage;
  8.  
  9. public class MailSender {
  10. private static String host = "";
  11. private static String user = "";
  12. private static String password = "";
  13. private static int port = 465;
  14. private static String from = "";
  15. private static String to = "";
  16.  
  17. private static String auth = "true";
  18. private static String debug = "false";
  19. private static String socket_factory = "javax.net.ssl.SSLSocketFactory";
  20. private static String subject = "";
  21. private static String text = "";
  22.  
  23. /**
  24. * Constructor. Fields - parameters for send mail.
  25. *
  26. * @param host - mail server.
  27. * @param user - user
  28. * @param password - login
  29. * @param port - port
  30. * @param from - mail from address
  31. * @param to - mail to address
  32. * @param subject - subject
  33. * @param text - text of mail
  34. */
  35. public MailSender(String host, String user, String password, int port,
  36. String from, String to, String subject, String text) {
  37.  
  38. if (!host.isEmpty())
  39. setHost(host);
  40. if (!user.isEmpty())
  41. setUser(user);
  42. if (!password.isEmpty())
  43. setPassword(password);
  44. if (port == 0)
  45. setPort(port);
  46. if (!from.isEmpty())
  47. setFrom(from);
  48. if (!to.isEmpty())
  49. setTo(to);
  50. if (!subject.isEmpty())
  51. setSubject(subject);
  52. if (!text.isEmpty())
  53. setText(text);
  54. }
  55.  
  56. /**
  57. * Send mail with parameters from constructor.
  58. */
  59.  
  60. public void send() {
  61. // Use Properties object to set environment properties
  62. Properties props = new Properties();
  63.  
  64. props.setProperty("mail.transport.protocol", "smtp");
  65. props.put("mail.smtp.host", getHost());
  66. props.put("mail.smtp.port", getPort());
  67. props.put("mail.smtp.user", getUser());
  68.  
  69. props.put("mail.smtp.auth", getAuth());
  70. props.put("mail.smtp.starttls.enable","true");//for gmail?
  71. props.put("mail.smtp.debug", getDebug());
  72.  
  73. props.put("mail.smtp.socketFactory.port", getPort());
  74. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");//for gmail?
  75. props.put("mail.smtp.socketFactory.fallback", "false");
  76.  
  77. try {
  78.  
  79. // Obtain the default mail session
  80. Session session = Session.getDefaultInstance(props, null);
  81. session.setDebug(new Boolean(getDebug()));
  82.  
  83. // Construct the mail message
  84. MimeMessage message = new MimeMessage(session);
  85. message.setText(getText());
  86. message.setSubject(getSubject());
  87. message.setFrom(new InternetAddress(getFrom()));
  88. message
  89. .addRecipient(RecipientType.TO,
  90. new InternetAddress(getTo()));
  91. message.saveChanges();
  92.  
  93. // Use Transport to deliver the message
  94. Transport transport = session.getTransport("smtp");
  95. transport.connect(getHost(), getUser(), getPassword());
  96. transport.sendMessage(message, message.getAllRecipients());
  97. transport.close();
  98.  
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102.  
  103. }
  104.  
  105. public static String getHost() {
  106. return host;
  107. }
  108.  
  109. public static void setHost(String host) {
  110. MailSender.host = host;
  111. }
  112.  
  113. public static String getUser() {
  114. return user;
  115. }
  116.  
  117. public static void setUser(String user) {
  118. MailSender.user = user;
  119. }
  120.  
  121. public static String getPassword() {
  122. return password;
  123. }
  124.  
  125. public static void setPassword(String password) {
  126. MailSender.password = password;
  127. }
  128.  
  129. public static int getPort() {
  130. return port;
  131. }
  132.  
  133. public static void setPort(int port) {
  134. MailSender.port = port;
  135. }
  136.  
  137. public static String getFrom() {
  138. return from;
  139. }
  140.  
  141. public static void setFrom(String from) {
  142. MailSender.from = from;
  143. }
  144.  
  145. public static String getTo() {
  146. return to;
  147. }
  148.  
  149. public static void setTo(String to) {
  150. MailSender.to = to;
  151. }
  152.  
  153. public static String getAuth() {
  154. return auth;
  155. }
  156.  
  157. public static void setAuth(String auth) {
  158. MailSender.auth = auth;
  159. }
  160.  
  161. public static String getDebug() {
  162. return debug;
  163. }
  164.  
  165. public static void setDebug(String debug) {
  166. MailSender.debug = debug;
  167. }
  168.  
  169. public static String getSocket_factory() {
  170. return socket_factory;
  171. }
  172.  
  173. public static void setSocket_factory(String socketFactory) {
  174. socket_factory = socketFactory;
  175. }
  176.  
  177. public static String getSubject() {
  178. return subject;
  179. }
  180.  
  181. public static void setSubject(String subject) {
  182. MailSender.subject = subject;
  183. }
  184.  
  185. public static String getText() {
  186. return text;
  187. }
  188.  
  189. public static void setText(String text) {
  190. MailSender.text = text;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement