JavaMail发送邮件

引入了一个jar:javax.mail.jar

直接上代码:

InitMail:

package com.hi.test;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class InitMail {

    private String smtp;// SMTP地址
    private String from;// 发件人
    private String to;// 收件人
    private String copyto;// 抄送
    private String subject;// 主题
    private String content;// 内容
    private String username;// 用户名
    private String password;// 密码
    private String filename;// 附件地址,如: S:\download\xxx.txt

    // =====================//
    private MimeMessage mimeMsg; // MIME邮件对象
    private Session session; // 邮件会话对象
    private Properties props; // 系统属性
    private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象

    // ======================//

    @SuppressWarnings("unused")
    private InitMail() {

    }

    /*
     * 抄送和附件可以为空,其他不能为空
     */
    public InitMail(String smtp, String from, String to, String copyto,
            String subject, String content, String username, String password,
            String filename) {

        this.smtp = smtp;
        this.from = from;
        this.to = to;
        this.copyto = copyto;
        this.subject = subject;
        this.content = content;
        this.username = username;
        this.password = password;
        this.filename = filename;

    }

    public boolean sendMail() {
        boolean result = false;
        try {
            props = System.getProperties(); // 获得系统属性对象
            session = Session.getDefaultInstance(props); // 获得邮件会话对象    
            mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
            
            mp = new MimeMultipart();
            
            
            mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
            
            
            mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));//设置收件人
            
            mimeMsg.setSubject(""+subject);//设置主题
            
            //如果抄送人不为空或null,则设置抄送人
            if(!(null==copyto||"".equals(copyto))){
                mimeMsg.setRecipients(Message.RecipientType.CC,(Address[]) InternetAddress.parse(copyto));
            }
            
            BodyPart bp = new MimeBodyPart();
            bp.setContent("" + content, "text/html;charset=GBK");
            mp.addBodyPart(bp);
            
            
            //如果附件不为空或null,则设置附件
            if(!(null==filename||"".equals(filename))){
                BodyPart bp2 = new MimeBodyPart();
                FileDataSource fileds = new FileDataSource(filename);
                bp2.setDataHandler(new DataHandler(fileds));
                bp2.setFileName(fileds.getName());
                mp.addBodyPart(bp2);
            }

            
            
            mimeMsg.setContent(mp);
            mimeMsg.saveChanges();
            
            
            Transport transport = session.getTransport("smtp");
            transport.connect(smtp, username,password);
            transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
            
            //如果抄送人不为空或null,则发送给抄送人
            if(!(null==copyto||"".equals(copyto))){
            transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC));
            }
            
            transport.close();
            result=true;        
            System.out.println("发送成功");

        } catch (Exception e) {
            System.out.println("出错,邮件发送失败");
            e.printStackTrace();
            
        }

        return result;
    }
    



    
}

SendMail:

package com.hi.test;

public class SendMail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String smtp = "smtp.qq.com";  
        String from = "xxx@qq.com";  
        String to = "xxx@hotmail.com";   
        String subject = "test";  
        String content = "this is java mail test ";  
        String username="xxx";  
        String password="xxx";  
        String filename="S:\\download\\javax.mail.jar";
     
        
        InitMail mail=new InitMail(smtp, from, to, null,
                subject,content, username, password,
                filename);
        mail.sendMail();

    }

}

main中账号密码换成自己的就可以了,如果没有抄送和附件,传入null就可以了。具体的可以看代码,没什么太多需要说的。

 

posted @ 2014-08-10 12:04 oh~NO! 阅读(...) 评论(...) 编辑 收藏