码迷,mamicode.com
首页 > 编程语言 > 详细

java mail发送邮件

时间:2014-07-02 00:16:54      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:java   os   art   for   io   服务器   

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");//收件人
        
     }
        
        

}

java mail发送邮件,布布扣,bubuko.com

java mail发送邮件

标签:java   os   art   for   io   服务器   

原文地址:http://www.cnblogs.com/ansonz/p/3816349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!