用javaMail发送邮件

java 代码
  1.   
  2. public class JavaMail {   
  3.     //  发送邮件函数   
  4.     public boolean sendMail(String mailTo, String mailSubject, String mailBody) {   
  5.         //发送email   
  6.         try {   
  7.             //default account information   
  8.             String smtpServer = "smtp.163.com";   
  9.             String smtpAuth = "true";   
  10.             String smtpUser = "xxxx";   
  11.             String smtpPassword = "xxxx";   
  12.             String From = "[email protected]";   
  13.             String To = mailTo;   
  14.             String Subject = mailSubject;   
  15.             String Text = mailBody;   
  16.             //        java.util.ResourceBundle resBundle;   
  17.             //        resBundle = java.util.ResourceBundle.getBundle("mailinfo",Locale.SIMPLIFIED_CHINESE);   
  18.             //        if (resBundle != null) {   
  19.             //          smtpServer = resBundle.getString("mail.smtp.host");   
  20.             //          smtpAuth = resBundle.getString("mail.smtp.auth");   
  21.             //          smtpUser = resBundle.getString("mail.smtp.user");   
  22.             //          smtpPassword = resBundle.getString("mail.smtp.password");   
  23.             //          From = resBundle.getString("mail.smtp.from");   
  24.             //        }   
  25.             Properties props = new Properties();   
  26.             Session sendMailSession;   
  27.             Transport transport;   
  28.             props.put("mail.smtp.host", smtpServer);   
  29.             props.put("mail.smtp.auth", smtpAuth);   
  30.             if ("true".equals(smtpAuth)) {   
  31.                 //smtp服务器需要验证,用MyAuthertiactor来创建mail session   
  32.                 MyAuthenticator myauth = new MyAuthenticator(smtpUser,   
  33.                         smtpPassword);   
  34.                 sendMailSession = Session.getInstance(props, myauth);   
  35.             } else {   
  36.                 sendMailSession = Session.getInstance(props);   
  37.             }   
  38.             //Debug   
  39.             sendMailSession.setDebug(true);   
  40.             Message newMessage = new MimeMessage(sendMailSession);   
  41.             newMessage.setFrom(new InternetAddress(From));   
  42.             newMessage.setRecipient(Message.RecipientType.TO,   
  43.                     new InternetAddress(mailTo));   
  44.             newMessage.setSubject(Subject);   
  45.             newMessage.setSentDate(new Date());   
  46.             newMessage.setText(Text);   
  47.             newMessage.saveChanges();   
  48.             transport = sendMailSession.getTransport("smtp");   
  49.             transport.send(newMessage, newMessage.getAllRecipients());   
  50.             transport.close();   
  51.         } catch (Exception mailEx) {   
  52.             System.err.println("Send Mail Error:" + mailEx.getMessage());   
  53.             return false;   
  54.         }   
  55.         return true;   
  56.     }   
  57.   
  58.     //smtp需要验证时候的验证类   
  59.     class MyAuthenticator extends javax.mail.Authenticator {   
  60.         private String strUser;   
  61.   
  62.         private String strPwd;   
  63.   
  64.         public MyAuthenticator(String user, String password) {   
  65.             this.strUser = user;   
  66.             this.strPwd = password;   
  67.         }   
  68.   
  69.         protected PasswordAuthentication getPasswordAuthentication() {   
  70.             return new PasswordAuthentication(strUser, strPwd);   
  71.         }   
  72.     }   
  73.   
  74. }  

你可能感兴趣的:(用javamail发送邮件)