Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import javax.activation.DataHandler;
  2. import javax.activation.DataSource;
  3. import javax.mail.Message;
  4. import javax.mail.PasswordAuthentication;
  5. import javax.mail.Session;
  6. import javax.mail.Transport;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.OutputStream;
  13. import java.security.Security;
  14. import java.util.Properties;
  15.  
  16. public class GMailSender extends javax.mail.Authenticator {
  17. private String mailhost = "smtp.gmail.com";
  18. private String user;
  19. private String password;
  20. private Session session;
  21.  
  22. static {
  23. Security.addProvider(new com.provider.JSSEProvider());
  24. }
  25.  
  26. public GMailSender(String user, String password) {
  27. this.user = user;
  28. this.password = password;
  29.  
  30. Properties props = new Properties();
  31. props.setProperty("mail.transport.protocol", "smtp");
  32. props.setProperty("mail.host", mailhost);
  33. props.put("mail.smtp.auth", "true");
  34. props.put("mail.smtp.port", "465");
  35. props.put("mail.smtp.socketFactory.port", "465");
  36. props.put("mail.smtp.socketFactory.class",
  37. "javax.net.ssl.SSLSocketFactory");
  38. props.put("mail.smtp.socketFactory.fallback", "false");
  39. props.setProperty("mail.smtp.quitwait", "false");
  40.  
  41. session = Session.getDefaultInstance(props, this);
  42. }
  43.  
  44. protected PasswordAuthentication getPasswordAuthentication() {
  45. return new PasswordAuthentication(user, password);
  46. }
  47.  
  48. public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
  49. try{
  50. MimeMessage message = new MimeMessage(session);
  51. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  52. message.setSender(new InternetAddress(sender));
  53. message.setSubject(subject);
  54. message.setDataHandler(handler);
  55. if (recipients.indexOf(',') > 0)
  56. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  57. else
  58. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  59. Transport.send(message);
  60. }catch(Exception e){
  61.  
  62. }
  63. }
  64.  
  65. public class ByteArrayDataSource implements DataSource {
  66. private byte[] data;
  67. private String type;
  68.  
  69. public ByteArrayDataSource(byte[] data, String type) {
  70. super();
  71. this.data = data;
  72. this.type = type;
  73. }
  74.  
  75. public ByteArrayDataSource(byte[] data) {
  76. super();
  77. this.data = data;
  78. }
  79.  
  80. public void setType(String type) {
  81. this.type = type;
  82. }
  83.  
  84. public String getContentType() {
  85. if (type == null)
  86. return "application/octet-stream";
  87. else
  88. return type;
  89. }
  90.  
  91. public InputStream getInputStream() throws IOException {
  92. return new ByteArrayInputStream(data);
  93. }
  94.  
  95. public String getName() {
  96. return "ByteArrayDataSource";
  97. }
  98.  
  99. public OutputStream getOutputStream() throws IOException {
  100. throw new IOException("Not Supported");
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement