/* Some SMTP servers require a username and password authentication before you can use their Server for Sending mail. This is most common with couple of ISP's who provide SMTP Address to Send Mail. This Program gives any example on how to do SMTP Authentication (User and Password verification) This is a free source code and is provided as it is without any warranties and it can be used in any your code for free. Author : Sudhir Ancha */ import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.*; import java.io.*; /* To use this program, change values for the following three constants, SMTP_HOST_NAME -- Has your SMTP Host Name SMTP_AUTH_USER -- Has your SMTP Authentication UserName SMTP_AUTH_PWD -- Has your SMTP Authentication Password Next change values for fields emailMsgTxt -- Message Text for the Email emailSubjectTxt -- Subject for email emailFromAddress -- Email Address whose name will appears as "from" address Next change value for "emailList". This String array has List of all Email Addresses to Email Email needs to be sent to. Next to run the program, execute it as follows, SendMailUsingAuthentication authProg = new SendMailUsingAuthentication(); */ /** * Class that let the user send mail through SMTP servers using * authentication methods. */ public class SendMailUsingAuthentication { private String SMTP_HOST_NAME; private String SMTP_AUTH_USER; private String SMTP_AUTH_PWD; private String emailMsgTxt; private static final String emailSubjectTxt = "Su mensaje"; /*private static final String emailFromAddress = "mensaje@javaPostIt.org";*/ private static final String emailFromAddress = "mensaje@java.sun.com"; private String localFileName; private static final String attachName = "Mensaje Voz - JPostIt"; private boolean isRec; // Add List of Email address to who email needs to be sent to private String[] emailList; //Puede ser lista separada por coma. /** * To be used when mailing with an attachement * @param host Hostname * @param user Username * @param pass Password * @param mssg Mail message * @param dir String array with mailing addresses * @param fn Filename to attach */ public SendMailUsingAuthentication(String host, String user, String pass, String mssg, String[] dir, String fn) { SMTP_HOST_NAME = host; SMTP_AUTH_USER = user; SMTP_AUTH_PWD = pass; emailMsgTxt = mssg; emailList = dir; localFileName = fn; isRec = true; } /** * To be used when mailing without an attachement * @param host Hostname * @param user Username * @param pass Password * @param mssg Mail message * @param dir String array with mailing addresses */ public SendMailUsingAuthentication(String host, String user, String pass, String mssg, String[] dir) { SMTP_HOST_NAME = host; SMTP_AUTH_USER = user; SMTP_AUTH_PWD = pass; emailMsgTxt = mssg; emailList = dir; localFileName = null; isRec = false; } /** * Composes the message with the given data and mails it */ public void postMail() throws MessagingException { boolean debug = false; //Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getDefaultInstance(props, auth); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(emailFromAddress); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[emailList.length]; for (int i = 0; i < emailList.length; i++) { addressTo[i] = new InternetAddress(emailList[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(emailSubjectTxt); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText(emailMsgTxt); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment if (isRec) { messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(localFileName); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachName); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); Transport.send(msg); } } /** * SimpleAuthenticator is used to do simple authentication * when the SMTP server requires it. */ private class SMTPAuthenticator extends javax.mail.Authenticator { /** * Returns the Authentication required * @return The PasswordAuthentication object */ public PasswordAuthentication getPasswordAuthentication() { String username = SMTP_AUTH_USER; String password = SMTP_AUTH_PWD; return new PasswordAuthentication(username, password); } } }