Hi all, many developers we indispensable to implement a module for sending mail in the systems we develop. Depending on what language is the complexity or simplicity of the encoding of this and as many know, Java is not always a precursor of simplicity.
is why I want to publish and provide to all Mail.java class that allows you to send mail through Google Apps and through a mail server commonplace.
The code works by implementing the JavaMail API SUN (so download the JAR with the API but can never send messages) and provides 2 methods. One called 'enviaGoogleApp' that allows you to send mail through a Google Apps account and another called 'send' to send mail through a mail server defined.
The code of both methods is identical, only changes the way you authenticate to the server.
The Mail class methods only require the following information:
1) Issuer of the message.
2) Subject of the mail.
3) Recipient (s) of mail.
4) mail message (in HTML or ASCII)
5) Attachments.
With a slight modification and adding 2 parameters can be added:
1) Recipients who are copying the mail.
2) Recipients who are blind copy.
The code is as follows and can be downloaded from Google Group getgeek :
util.mail package;
import java.io.File;
import java.util.ArrayList;
import java.util . List;
import java.util.Properties;
javax.activation.DataHandler
import, import
javax.activation.DataSource;
javax.activation.FileDataSource import, import
javax.mail.Authenticator;
javax.mail import. Message;
javax.mail.Multipart import, import
javax.mail.PasswordAuthentication;
javax.mail.Session import, import
javax.mail.Transport;
javax.mail.internet.InternetAddress import, import
javax.mail. internet.MimeBodyPart;
javax.mail.internet.MimeMessage import, import
javax.mail.internet.MimeMultipart;
public class Mail {private class
SMTPAuthenticator extends Authenticator {private String
dEmail;
dPassword private String;
public
SMTPAuthenticator (String email, String password) {
dEmail = email;
dPassword = password;}
getPasswordAuthentication public PasswordAuthentication () {return new PasswordAuthentication
(dEmail, dPassword);}
} / ** * function & oacute
n that allows to send an electronic mail with attachments ó unique. < Br >
* The email content can be in HTML format. * @ Param issuer
Mail é qui n emits mail
* @ param subject Subject of the e-mail recipients
* @ param Post the e-mail recipients * @ param
Message of e-mail
* @ param path attachments attachments in e-mail
* @ return TRUE if the mail was sent successfully é,
FALSE otherwise * / public boolean
enviaGoogleApp (String sender, String subject, List & lt ; String > recipient, String message, String > < List attachments) {boolean
envioExitoso = true;
String emailGetCursos = "cuenta.googleapps@algo.cl";
String password = "password.de.cuenta.googleapps";
Properties props = new Properties();
props.put("mail.smtp.user", emisor);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try{
Authenticator auth = new SMTPAuthenticator(emailGetCursos, password);
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
InternetAddress[] dest = new InternetAddress[receptores.size()];
for (int i = 0; i < dest.length; i + +) {
dest [i] = new InternetAddress (receptores.get (i));}
/ / Define é qui n is the e-mail sender
message.setFrom (new InternetAddress (sender));
InternetAddress [] = new InternetAddress replyTo [1];
replyTo [0] = new InternetAddress (sender);
message.setReplyTo (replyTo)
/ / Define the intended
message.addRecipients (Message.RecipientType.TO, dest);
/ / message.addRecipients (Message.RecipientType.CC, dest);
/ / message.addRecipients (Message.RecipientType.BCC , dest);
/ / To set the subject of the e-mail
message.setSubject (issue);
/ / It sets the e-mail message
MimeBodyPart MimeBodyPart messageBodyPart = new ();
messageBodyPart.setContent ( message, "text / html");
MimeMultipart Multipart multipart = new ();
multipart.addBodyPart (messageBodyPart)
/ / files are attached to e
if (adjuntos! adjuntos.size = null & & () > 0) {
for (String rutaAdjunto: attachments) {
MimeBodyPart messageBodyPart = new ();
File f = new File (rutaAdjunto)
if (f.exists ()) {source = new DataSource
FileDataSource (rutaAdjunto)
messageBodyPart.setDataHandler (new DataHandler (source));
messageBodyPart.setFileName (f.getName ( ));
multipart.addBodyPart (messageBodyPart)
}}}
/ / message and is coupled
message.setContent attachments (multipart);
/ / env is on e-í Transport.send
mail (message);
} catch (Exception e) {
envioExitoso = false;}
finally {/ / is deleted from the server
attachments if (adjuntos! = null & & adjuntos.size () > 0) {
for (String rutaAdjunto: Attachments)
{try {File arch = new File (rutaAdjunto)
arch.delete ();
} catch (Exception e) {}}
}} return
envioExitoso;
} / ** *
n ó function that lets you send an electronic mail with attachments ó unique. < Br >
* The contents of Mail can be in HTML format. * @ Param issuer
Mail é qui n emits mail
* @ param subject Subject of the e-mail recipients
* @ param Post the e-mail recipients * @ param
Message of e-mail
* @ param path attachments attachments in e-mail
* @ return TRUE if the mail was sent successfully é,
FALSE otherwise * / public boolean
send (String sender, String subject, List & lt ; String > recipient, String message, String > < List attachments) {boolean
envioExitoso = true;
try {Properties props = System.getProperties ();
/ / Define
mail server props.put ("mail.smtp.host", "ip.servidor.de.correos");
props.put ("mail . smtp.port "," puerto.servidor.de.correo (default 25) ");
/ / SESI ó n is obtained from the mail server
Session.getInstance Session session = (props, null);
session.setDebug (true);
MimeMessage message = new MimeMessage (session);
InternetAddress [] dest = new InternetAddress [receptores.size ()];
for (int i = 0; i < dest.length; i + +) {
dest [i] = new InternetAddress (receptores.get (i));}
/ / Define é qui n e is the emitter message.setFrom
-mail (new InternetAddress (sender));
InternetAddress [] = new InternetAddress replyTo [1];
replyTo [0] = new InternetAddress (sender);
message.setReplyTo (replyTo)
/ / It define the intended
message.addRecipients (Message.RecipientType.TO, dest);
/ / message.addRecipients (Message.RecipientType.CC, dest);
/ / message.addRecipients (Message.RecipientType.BCC, dest) ;
/ / To set the subject of the e-mail
message.setSubject (issue);
/ / It sets the e-mail message
MimeBodyPart MimeBodyPart messageBodyPart = new ();
messageBodyPart.setContent (message, "text / html ");
MimeMultipart Multipart multipart = new ();
multipart.addBodyPart (messageBodyPart)
/ / files are attached to e
if (adjuntos! = null & & adjuntos.size ()> 0) {
for (String rutaAdjunto: attachments) {
MimeBodyPart messageBodyPart = new ();
File f = new File (rutaAdjunto)
if (f.exists ()) {source = new DataSource
FileDataSource (rutaAdjunto)
messageBodyPart.setDataHandler (new DataHandler (source));
messageBodyPart.setFileName (f.getName ());
multipart.addBodyPart (messageBodyPart )
}}}
/ / message and is coupled
message.setContent attachments (multipart);
/ / env is í to the e-mail
Transport.send (message);
} catch (Exception e) {
envioExitoso = false;}
finally {/ / is deleted from the server
attachments if (adjuntos! = null & & adjuntos.size () > 0) {
for (String rutaAdjunto: attachments) {try {
arch File = new File (rutaAdjunto)
arch.delete ();
} catch (Exception e) {}}
}} return
envioExitoso;
} public static void main (String [] args) {
String > < List recipients = new String > < ArrayList ();
receptores.add (mail.de.alguien @ gmail.com ");
String issuer = "test@test.cl"
String subject = "Test Mail";
String message = "This is an email from < font color = 'red' > & ; lt; b > < TEST / b > < / font > "String > <
List files = new String > < ArrayList () ;
Mail m = new Mail ();
/ / m.envia (sender, subject, recipient, message, null);
m.enviaGoogleApp (sender, subject, recipient, message, file);
}}