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

 

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("
[email protected]", "qq密码");       
            
        try     
        {     

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

你可能感兴趣的:(java,qq,String,测试,jar,email)