java mail发送邮件

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailUtility {
    
     private static int port = 25;
     private String server = "mail.163.com";//邮件服务器
     private String from = "vivk";//发送者,显示的发件人名字
     private String user = "";//发送者邮箱地址
     private String password = "";//密码

       
     /**
      * Send emails
      * @param email
      * @param subject
      * @param body
      * @throws UnsupportedEncodingException
      */
     public void sendEmail(String [] email, String subject, String body) throws UnsupportedEncodingException {
            
         try
         {
            
             Properties props = new Properties();
             props.put("mail.smtp.host", server);                
             props.put("mail.smtp.port", String.valueOf(port));
             props.put("mail.smtp.auth", "false");
             props.put("mail.smtp.starttls.enable", "true");
             Transport transport = null;
             Session session = Session.getInstance(props, new MailAuthenticator(from, password));
             transport = session.getTransport("smtp");
             transport.connect(server, user, password);
             MimeMessage msg = new MimeMessage(session);
             msg.setSentDate(new Date());
             InternetAddress fromAddress = new InternetAddress(user,from,"UTF-8");
             msg.setFrom(fromAddress);
             InternetAddress[] toAddress = new InternetAddress[email.length];    
             
             for (int i=0;i<email.length;i++)
             {
                 toAddress[i] = new InternetAddress(email[i]);
             }             
             
             msg.setRecipients(Message.RecipientType.TO, toAddress);
             msg.setSubject(subject, "UTF-8");   
             msg.setText(body, "UTF-8");
             msg.saveChanges();
             transport.sendMessage(msg, msg.getAllRecipients());
             System.out.println("Complete sent emai!!!");
             LogUtil.debug("Complete sent emai!!!");
             
         }
         catch (NoSuchProviderException e) {
             e.printStackTrace();
            } catch (MessagingException e) {
             e.printStackTrace();
            }
        }
     /**
      * 需要认证账号和密码
      * @author zhaohongbing
      *
      */
     class MailAuthenticator extends Authenticator {
         String user;
         String pw;
        
         public MailAuthenticator (String username, String password){
             super();
             this.user = username;
             this.pw = password;
         }
         public PasswordAuthentication getPasswordAuthentication(){
             return new PasswordAuthentication(user, pw);
         }
     }
    
    
     public static void main(String args[]) throws UnsupportedEncodingException
     {
         EmailUtility myEmailUtility = new EmailUtility();
         String [] toAddress = {"ans@163.com"};
         myEmailUtility.sendEmail(toAddress,"邮件测试","测试邮件,收到请回复我一下,thanks");//收件人
        
     }
        
        

}

posted on 2014-06-30 11:52  强巴拉  阅读(395)  评论(0编辑  收藏  举报