使用JavaMail发送SMTP认证的邮件给多个收信人


/*
* SMTPAuth.java
*
* Created on 2005年11月17日, 下午11:30
*/

package  com.jointforce;

import  javax.mail. * ;
import  javax.mail.internet. * ;
import  java.io. * ;
import  javax.activation. * ;
import  java.util. * ;

/**
*
@author   eric.cookie
*/
public   class  SMTPAuth {
    
    
/**  Creates a new instance of SMTPAuth  */
    
public  SMTPAuth() {
    }
    
    
/**
     * 
@param  args the command line arguments
     
*/
    
public   static   void  main(String[] args) {
        
//  TODO code application logic here
        String to1  =   " [email protected] " ;
        String to2 
=   " [email protected] " ;
        String to3 
=   " [email protected] " ;
        String to4 
=   " [email protected] " ;
        String to5 
=   " [email protected] " ;
        String to6 
=   " [email protected] " ;
        
        String from 
=   " [email protected] " ;
        String subject 
=   " Test JavaMail " ;
        
        Properties props 
=  System.getProperties();
        props.put(
" mail.smtp.auth " " true " );
        props.put(
" mail.smtp.host " " smtp.jointforce.com.cn " );
        
        SmtpAuthenticator sa 
=   new  SmtpAuthenticator();
        Session sess 
=  Session.getInstance(props, sa);
        sess.setDebug(
true );
        
        
try  {
            Message msg 
=   new  MimeMessage(sess);
            msg.setFrom(
new  InternetAddress(from));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to1, 
false ));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to2, 
false ));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to3, 
false ));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to4, 
false ));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to5, 
false ));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to6, 
false ));
            msg.setSubject(subject);
            msg.setSentDate(
new  Date());
            msg.setText(
" Hello World!\nSecond Line text\nend " );
            Transport.send(msg);
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }
    
}

class  SmtpAuthenticator  extends  Authenticator {
    
protected  PasswordAuthentication getPasswordAuthentication() {
        
return   new  PasswordAuthentication( " username_to_login_mailbox " " password_to_login_mailbox " );
    }
}

 

你可能感兴趣的:(javamail)