0%

Java 发送邮件

这里用apache的commons-email(http://commons.apache.org/proper/commons-email/download_email.cgi)和java mail(http://www.oracle.com/technetwork/java/index-138643.html)发送邮件. 我的需求仅仅是在系统异常时发个报警邮件,所以,不涉及到附件之类的东西,代码很简单:

public static void sendMail(String receiverAddress,String subject,String message)
    {
    MultiPartEmail email = new MultiPartEmail();  
        try {  
            //smtp server:  
            email.setHostName("smtp.163.com");  
            // Charset  
            email.setCharset("utf-8");  
            // receiver's address            
            email.addTo(receiverAddress);  
            // 发件人邮箱
            email.setFrom("***@163.com");  
            //帐号密码
            email.setAuthentication("***", "***"); 
            //主题
            email.setSubject(subject);  
            //信息 
            email.setMsg(message);  
            // 发送  
            email.send();  
        } catch (EmailException e) {  
            e.printStackTrace();  
        }  
    }