[other]Send email with Authentication

来源:互联网 发布:淘宝兽药二维码 编辑:程序博客网 时间:2024/05/22 03:41

一个发Email的小程序,使用Gmail,需要认证.

 

  1. import java.util.Properties;
  2. import javax.mail.Authenticator;
  3. import javax.mail.Message;
  4. import javax.mail.MessagingException;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.Session;
  7. import javax.mail.Transport;
  8. import javax.mail.internet.InternetAddress;
  9. import javax.mail.internet.MimeMessage;
  10. public class SendMailUsingAuthentication {
  11.     private static final String SMTP_AUTH_USER = "user name";
  12.     private static final String SMTP_AUTH_PWD = "password";
  13.     private static final String emailMsgTxt = "MsgTxt";
  14.     private static final String emailSubjectTxt = "Subject";
  15.     private static final String emailFromAddress = "userxxx@gmail.com";
  16.     private static final String[] emailList = { "userxxx@gmail.com" };
  17.     public static void main(String args[]) throws Exception {
  18.         SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
  19.         smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
  20.         System.out.println("Sucessfully Sent mail to All Users");
  21.     }
  22.     public void postMail(String recipients[], String subject, String message, String from) throws MessagingException {
  23.         boolean debug = false;
  24.         final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  25.         // Set the host smtp address
  26.         Properties props = new Properties();
  27.         props.setProperty("mail.smtp.host""smtp.gmail.com");
  28.         props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  29.         props.setProperty("mail.smtp.socketFactory.fallback""false");
  30.         props.setProperty("mail.smtp.port""465");
  31.         props.setProperty("mail.smtp.socketFactory.port""465");
  32.         props.put("mail.smtp.auth""true");
  33.         Authenticator auth = new SMTPAuthenticator();
  34.         Session session = Session.getDefaultInstance(props, auth);
  35.         session.setDebug(debug);
  36.         // create a message
  37.         Message msg = new MimeMessage(session);
  38.         // set the from and to address
  39.         InternetAddress addressFrom = new InternetAddress(from);
  40.         msg.setFrom(addressFrom);
  41.         InternetAddress[] addressTo = new InternetAddress[recipients.length];
  42.         for (int i = 0; i < recipients.length; i++) {
  43.             addressTo[i] = new InternetAddress(recipients[i]);
  44.         }
  45.         msg.setRecipients(Message.RecipientType.TO, addressTo);
  46.         // Setting the Subject and Content Type
  47.         msg.setSubject(subject);
  48.         msg.setContent(message, "text/plain");
  49.         Transport.send(msg);
  50.     }
  51.     /**
  52.      * SimpleAuthenticator is used to do simple authentication when the SMTP
  53.      * server requires it.
  54.      */
  55.     private class SMTPAuthenticator extends javax.mail.Authenticator {
  56.         public PasswordAuthentication getPasswordAuthentication() {
  57.             String username = SMTP_AUTH_USER;
  58.             String password = SMTP_AUTH_PWD;
  59.             return new PasswordAuthentication(username, password);
  60.         }
  61.     }
  62. }
原创粉丝点击