Email.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
package com.gaurangjadia.java.base; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Email { private Properties prop; private String host; private int port; private String user; private String password; public Email(String strHost, int intPort, String strUser, String strPassword) { this.setHost(strHost); this.setPort(intPort); this.setUser(strUser); this.setPassword(strPassword); this.setProp(new Properties()); this.getProp().put("mail.smtp.user", this.getUser()); this.getProp().put("mail.smtp.host", this.getHost()); this.getProp().put("mail.smtp.port", this.getPort()); this.getProp().put("mail.smtp.auth", "true"); if (this.getPort() != 25) { this.getProp().put("mail.smtp.starttls.enable","true"); this.getProp().put("mail.smtp.socketFactory.port", this.getPort()); this.getProp().put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); this.getProp().put("mail.smtp.socketFactory.fallback", "true"); } } public Boolean send (String strSubject, String strBody, String strTo) { try{ Authenticator auth = new SMTPAuthenticator(this.getUser(), this.getPassword()); Session session = Session.getInstance(this.getProp(), auth); MimeMessage msg = new MimeMessage(session); msg.setSubject(strSubject); msg.setFrom(new InternetAddress(this.getUser())); msg.setContent(strBody, "text/html"); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(strTo)); Transport.send(msg); return true; } catch (Exception mex){ mex.printStackTrace(); return false; } } public Properties getProp() { return this.prop; } public void setProp(Properties prop) { this.prop = prop; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
SMTPAuthenticator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.gaurangjadia.java.base; import javax.mail.PasswordAuthentication; public class SMTPAuthenticator extends javax.mail.Authenticator { String user; String password; public SMTPAuthenticator (String strUser, String strPassword) { this.setUser(strUser); this.setPassword(strPassword); } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.getUser(), this.getPassword()); } } |