java 发邮件 用org.apache.commons.mail (jar包为commons-email-1.2.jar,mail.jar)

时间:2021-02-23 09:13:34

 

package mail;

import org.apache.commons.mail.EmailException;    
import org.apache.commons.mail.SimpleEmail;    
   
public class Send     
{    
    public Send(){}    
    //需要的jar包4个:commons-email-1.2.jar  commons-email-1.2-javadoc.jar
    //commons-email-1.2-sources.jar     mail.jar

    public static void main(String[] args)    
    {    
        send();    
    }    
    //QQ邮箱对应的pop3和smtp服务器地址为:pop3:pop.qq.com;
    //smtp:smtp.qq.com,请参考
  

    public static void send()    
    {    
        SimpleEmail email = new SimpleEmail();       
        email.setTLS(true);            
        email.setHostName("smtp.qq.com");          
        email.setAuthentication("
449111111@qq.com", "qq密码");       
            
        try     
        {     

            email.addTo("
luss@yahoo.com.cn","rec");    //接收方
            email.setFrom("
449111111@qq.com");       //发送方       
                
            email.setSubject("Java发送邮件测试");                 //标题       
                
            email.setCharset("GB2312");                     //设置Charset    
                
            email.setMsg("这是一封Java程序发出的测试邮件。");     //内容       
                
            email.send();     
                
        } catch (EmailException e) {    
            e.printStackTrace();    
        }     
    }    
}    
/*
 * 有的邮箱需要开启POP3/SMTP服务,例如本例的qq邮箱,在测试时弹出需要开启此服务的
 * 提示(qq邮箱发来的),进入邮箱的设置,开启就行了
 */