Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.CommandMap;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.activation.MailcapCommandMap;
- import javax.mail.BodyPart;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- /**
- * Created by Jesus on 24/11/2017.
- */
- public class Mail extends javax.mail.Authenticator {
- // Variables
- private String user = ""; // username
- private String pass = ""; // password
- private String[] to;
- private String from = user; // correo electrónico enviado desde
- private String port = "465"; // default puerto smtp
- private String sport = "465"; // default puerto socketfactory
- private String host = "alpha.validns.com"; //"smtp.gmail.com"; // default servidor smtp
- private String subject = ""; // email asunto
- private boolean auth = true; // smtp authentication - default on
- private boolean debuggable = false; // debug mode on or off - default off
- private Multipart multipart;
- // Constructor
- public Mail() {
- multipart = new MimeMultipart();
- // Hay algo mal con MailCap, javamail no puede encontrar un
- // controlador para multipart/mixed part, por lo que este bit debe agregarse.
- MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
- mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
- mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
- mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
- mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
- mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
- CommandMap.setDefaultCommandMap(mc);
- }
- public Mail(String user, String pass) {
- this();
- this.setUser(user);
- this.setPass(pass);
- }
- /**
- * Envia el correo.
- *
- * @throws Exception
- */
- public void send() throws Exception {
- throwIfEmpty(user, "Especifica el remitente");
- throwIfEmpty(pass, "Se requiere de password");
- throwIfEmpty(from, "Especifica el remitente");
- //throwIfEmpty(subject, "user == null");
- throwIfEmpty(to, "Especifica al menos un destinatario.");
- //throwIfEmpty(body, "body == null");
- Properties props = getProperties();
- Session session = Session.getInstance(props, this);
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(from));
- InternetAddress[] addressTo = new InternetAddress[to.length];
- for (int i = 0; i < to.length; i++) {
- addressTo[i] = new InternetAddress(to[i]);
- }
- msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
- msg.setSubject(subject);
- msg.setSentDate(new Date());
- msg.setHeader("X-Priority", "1");
- // Poner partes en mensaje
- msg.setContent(multipart);
- // Envia el email
- Transport.send(msg);
- }
- /**
- * Email cuerpo de correo
- *
- * @param body Cuerpo del mensaje
- */
- public void addBody(Object body) {
- addBody(body, "text/html");
- }
- /**
- * Email cuerpo de correo
- *
- * @param body Cuerpo del mensaje
- * @param contetType typo de contenido
- */
- public void addBody(Object body, String contetType) {
- try {
- BodyPart messageBodyPart = new MimeBodyPart();
- messageBodyPart.setContent(body, contetType);
- multipart.addBodyPart(messageBodyPart);
- } catch (MessagingException ignore) {
- }
- }
- /**
- * Metodo para añadir un adjunto.
- * @param filename ruta del archivo a subir.
- */
- public void addAttachment(String filename) {
- addAttachment(new File(filename));
- }
- /**
- * Metodo para añadir un archivo.
- * @param file archivo a subir.
- */
- public void addAttachment(java.io.File file) {
- try {
- BodyPart messageBodyPart = new MimeBodyPart();
- DataSource source = new FileDataSource(file);
- messageBodyPart.setDataHandler(new DataHandler(source));
- messageBodyPart.setFileName(file.getName());
- multipart.addBodyPart(messageBodyPart);
- } catch (MessagingException ignore) {
- }
- }
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, pass);
- }
- private Properties getProperties() {
- Properties props = new Properties();
- props.put("mail.smtp.host", host);
- if (debuggable) {
- props.put("mail.debug", "true");
- }
- if (auth) {
- props.put("mail.smtp.auth", "true");
- }
- props.put("mail.smtp.port", port);
- props.put("mail.smtp.socketFactory.port", sport);
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "false");
- return props;
- }
- private static void throwIfEmpty(String value, String msg) {
- if (value == null || value.isEmpty()) throw new RuntimeException(msg);
- }
- private static void throwIfEmpty(String[] to, String msg) {
- if (to == null || to.length == 0) throw new RuntimeException(msg);
- }
- // getters and setters
- public String getUser() {
- return user;
- }
- public void setUser(String _user) {
- this.user = _user;
- setFrom(_user);
- }
- public String getPass() {
- return pass;
- }
- public void setPass(String _pass) {
- this.pass = _pass;
- }
- public String[] getTo() {
- return to;
- }
- public void setTo(String... _to) {
- this.to = _to;
- }
- public String getFrom() {
- return from;
- }
- public void setFrom(String _from) {
- this.from = _from;
- }
- public String getPort() {
- return port;
- }
- public void setPort(String _port) {
- this.port = _port;
- }
- public String getSport() {
- return sport;
- }
- public void setSport(String _sport) {
- this.sport = _sport;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String _host) {
- this.host = _host;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String _subject) {
- this.subject = _subject;
- }
- public boolean isAuth() {
- return auth;
- }
- public void setAuth(boolean _auth) {
- this.auth = _auth;
- }
- public boolean isDebuggable() {
- return debuggable;
- }
- public void setDebuggable(boolean _debuggable) {
- this.debuggable = _debuggable;
- }
- public Multipart getMultipart() {
- return multipart;
- }
- public void setMultipart(Multipart _multipart) {
- this.multipart = _multipart;
- }
- }
Add Comment
Please, Sign In to add comment