Java发送邮件

/**
 * 
 */
package javaSocket;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author Acheron
 *
 */
public class EmailJ {
    private String from; 
    private String to;
    private String subject;
    private String body;
    private String password; //目前使用明文,此demo中不做加密处理
    /**
     * @return from
     */
    public String getFrom() {
        return from;
    }
    /**
     * @param from 要设置的 from
     */
    public void setFrom(String from) {
        this.from = from;
    }
    /**
     * @return to
     */
    public String getTo() {
        return to;
    }
    /**
     * @param to 要设置的 to
     */
    public void setTo(String to) {
        this.to = to;
    }
    /**
     * @return subject
     */
    public String getSubject() {
        return subject;
    }
    /**
     * @param subject 要设置的 subject
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }
    /**
     * @return body
     */
    public String getBody() {
        return body;
    }
    /**
     * @param body 要设置的 body
     */
    public void setBody(String body) {
        this.body = body;
    }
    
    
    
    /**
     * @return password
     */
    public String getPassword() {
        return password;
    }
    /**
     * @param password 要设置的 password
     */
    public void setPassword(String password) {
        this.password = password;
    }
    public boolean sendEmail(){
        if ( getTo() == null || getFrom() == null || getBody() == null || getSubject() == null ) {
            return false;
        }
        
        
        try{
            
            Properties props = new Properties();
            props.put("username", getFrom()); //邮箱账户
            props.put("password",getPassword()); //邮箱密码
            props.put("mail.transport.protocol","smtp" ); //邮件协议
            props.put("mail.smtp.host", "smtp.163.com"); //协议主机
            props.put("mail.smtp.port","25"); //端口号
            
            Session mailSession = Session.getDefaultInstance(props);
            Message msg = new MimeMessage(mailSession);
            
            msg.setFrom( new InternetAddress(getFrom()));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(getTo()));
            msg.setSentDate(new Date());
            msg.setSubject(getSubject());
            msg.setText(getBody());
            msg.saveChanges();
            
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("username"), props.getProperty("password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
            
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    //测试邮件发送功能
    public static void main(String[] args) {
        EmailJ email = new EmailJ();
        email.setTo("****@163.com"); //设置接收方邮箱地址
        email.setFrom("****@163.com"); //设置发送方邮箱地址
        email.setPassword("****"); //发送方邮箱密码
        email.setSubject("北京夏天的胡思乱想"); //邮件的主题
        email.setBody("那里有个班干部,快跑"); //邮件的主体内容
        email.sendEmail();
    }
}

测试结果:

 

posted @ 2017-08-21 18:08  起床oO  阅读(175)  评论(0编辑  收藏  举报