[搬家前]Java Mail

要求使用Java编写一个发送Mail的程序,以完成Bugfree的附加功能。

 

首先是一些概念(【转】http://echo715.javaeye.com/blog/650203

SMTP:简单邮件传输协议(Simple Mail Transfer Protocol,SMTP)由RFC821定义,它定义了发送邮件的机制,在JavaMail环境中,基于JavaMail的程序将和因特网服务供应商 ISP(internet Service Provider   ’   s)SMTP服务器通信.SMTP服务器会中转消息给接收方SMTP服务器以便最终让用户经由POP或者 IMAP获得.

MIME:是因特网邮件扩展标准(Multipurpose Internet Mail Extensions).它不是邮件传输协议,但是对于传输的内容的消息,附件以及其他的内容定义了格式.可以理解成一个定义合适的标准.


其次是一个发送邮件的实例(【转】http://www.javaeye.com/topic/195323

 

public class MailSender {

   
    public void sendMail() throws Exception{

        Properties props = new Properties();//创建属性对象
        props.put("mail.smtp.host", getHost());//设置smtp服务器地址
        props.put("mail.smtp.auth", "true");//设置服务器smtp需要验证
        Session session = Session.getInstance(props, null);//创建新邮件并群发
        //Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);
        MimeMessage message = new MimeMessage(session);//创建过程对象
        message.setFrom(new InternetAddress(getFromAddr()));
      
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
        message.setSubject(getTitle());//设置主题
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//设置信件内容
        multipart. addBodyPart(contentPart);
        if(getAttachPath() != null && getAttachName() != null){
            BodyPart attachmentPart= new MimeBodyPart();
            DataSource source = new FileDataSource(getAttachPath());           
            attachmentPart.setDataHandler(new DataHandler(source));
           BASE64Encoder enc = new BASE64Encoder();
            attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
            multipart.addBodyPart(attachmentPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, getUsername(), getPassword());
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
   
    public void sendMails() throws Exception{
   
        Properties props = new Properties();//创建属性对象
        props.put("mail.smtp.host", getHost());//设置smtp服务器地址
        props.put("mail.smtp.auth", "true");//设置服务器smtp需要验证
        Session session = Session.getInstance(props, null);//创建新邮件并群发
        //Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);
        MimeMessage message = new MimeMessage(session);//创建过程对象
        message.setFrom(new InternetAddress(getFromAddr()));//设置发送邮件地址
        message.setSentDate(new Date());//设置时间
        InternetAddress[] address = new InternetAddress[this.getMutliTo().length]; //群发地址
        for(int i = 0; i < this.getMutliTo().length; i++) { //循环发送
        address[i] = new InternetAddress((this.getMutliTo())[i]);
       }
     message.setRecipients(Message.RecipientType.TO, address);
       // message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
        message.setSubject(getTitle());//设置主题
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//设置信件内容
        multipart. addBodyPart(contentPart);
        if(getAttachPath() != null && getAttachName() != null){
            BodyPart attachmentPart= new MimeBodyPart();
            DataSource source = new FileDataSource(getAttachPath());
            attachmentPart.setDataHandler(new DataHandler(source));
           BASE64Encoder enc = new BASE64Encoder();
            attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
            multipart.addBodyPart(attachmentPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, getUsername(), getPassword());
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private String host = null; //邮件服务器
    private String fromAddr = null; //发送邮件地址
    private String toAddr = null; //接收邮件地址
    private String username = null; //发送邮件用户名
    private String password = null; //发送邮件密码
    private String title = null; //邮件标题
    private String attachPath = null; //邮件附件路径
    private String attachName = null; //邮件附件名
    private String sendtext = null; //邮件内容
    private String[] MutliTo = null; //群发用户
    public String[] getMutliTo() {
return MutliTo;
}

public void setMutliTo(String[] mutliTo) {
MutliTo = mutliTo;
}

public String getSendtext() {
return sendtext;
}

public void setSendtext(String sendtext) {
this.sendtext = sendtext;
}

public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getFromAddr() {
        return fromAddr;
    }

    public void setFromAddr(String fromAddr) {
        this.fromAddr = fromAddr;
    }

    public String getToAddr() {
        return toAddr;
    }

    public void setToAddr(String toAddr) {
        this.toAddr = toAddr;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAttachPath() {
        return attachPath;
    }

    public void setAttachPath(String attachPath) {
        this.attachPath = attachPath;
    }

    public String getAttachName() {
        return attachName;
    }

    public void setAttachName(String attachName) {
        this.attachName = attachName;
    }
}

折射配置文件的内容
#/u914d/u7f6eemail/u53d1/u9001/u6587/u4ef6
host=smtp.163.com
username=*****
password=*****
fromadd=*****@163.com

 

总结,理解好mail.jar提供的一些API,以及它们的作用,就很好做了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值