javaMail实现发送邮件

来源:互联网 发布:程序员考试资料 编辑:程序博客网 时间:2024/06/05 11:52

邮件发送协议为:smtp

接收协议为:pop3

所以邮件必须满足这个两个协议才能进行接收和发送

代码如下

package com.wem.Dome;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.*;import javax.mail.internet.*;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.List;import java.util.Properties;/** * Created by Administrator on 2016-08-18. */public class SendMail {    private String userName;//用户名    private String password; // 邮箱密码或者口令    private String to;  //目标邮箱    private String subject; //主题    private String content; //内容    private List<String> attachmentsList; //附件    private String smtpType; //smtp类型    public SendMail(String userName,String password,String to,String subject,String content,List<String> attachmentsList,String smtpType){        this.userName =userName;        this.password = password;        this.to = to;        this.subject = subject;        this.content = content;        this.attachmentsList = attachmentsList;        this.smtpType = smtpType;    }    public void send(){        Properties props = new Properties();        props.put("mail.smtp.host", smtpType);        props.put("mail.smtp.auth", "true");        //QQ邮箱端口号        if("smtp.qq.com".equals(smtpType)) {            props.put("mail.smtp.port", "587");        }        //创建Session对象        Session session = Session.getInstance(props                //以匿名内部类的形式创建登录服务器的认证对象                //password 就我目前测试而言,QQ邮箱和163邮箱,密码都为口令,并不是登录密码或者邮箱密码                , new Authenticator() {                    public PasswordAuthentication getPasswordAuthentication() {                        return new PasswordAuthentication(userName, password);                    }                });        try {            //构造MimeMessage并设置相关属性值            MimeMessage msg = new MimeMessage(session);            //设置发件人            msg.setFrom(new InternetAddress(userName));            //设置收件人            InternetAddress[] addresses = {new InternetAddress(to)};            msg.setRecipients(Message.RecipientType.TO, addresses);            //设置邮件主题     防止乱码            try {                msg.setSubject(MimeUtility.encodeText(subject, MimeUtility.mimeCharset("gb2312"), null));            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }            //构造Multipart            Multipart mp = new MimeMultipart();            //向Multipart添加正文            MimeBodyPart mbpContent = new MimeBodyPart();            //mbpContent.setText(content);            mbpContent.setContent(content, "text/html;charset=gb2312");            //将BodyPart添加到MultiPart中            mp.addBodyPart(mbpContent);            //向Multipart添加附件            //遍历附件列表,将所有文件添加到邮件消息里            for (String efile : attachmentsList) {                MimeBodyPart mbpFile = new MimeBodyPart();                //以文件名创建FileDataSource对象                FileDataSource fds = new FileDataSource(efile);                //处理附件                mbpFile.setDataHandler(new DataHandler(fds));                try {                    if (fds.getName() != null && !("".equals(fds.getName()))) {                        // 解决中文附件名乱码问题                        mbpFile.setFileName(MimeUtility.encodeText(new String(fds.getName().getBytes(), "utf-8"), "utf-8", null));                    } else {                        mbpFile.setFileName(fds.getName());                    }                } catch (UnsupportedEncodingException e) {                    e.printStackTrace();                }                //将BodyPart添加到MultiPart中                mp.addBodyPart(mbpFile);            }            //清空附件列表            attachmentsList.clear();            //向Multipart添加MimeMessage            msg.setContent(mp, "text/html;charset=utf-8");            //设置发送日期            msg.setSentDate(new Date());            //发送邮件            Transport.send(msg);        } catch (Exception e) {            e.printStackTrace();        }    }}
测试邮件发送
public static void main(String[] arge) throws Exception {         //用户名        String userName = "xxxxxxxxxxxx@163.com";        // 邮箱密码或者口令        String password = "xxxxxxxxxxxxxxxxx";        //主题        String subject = "测试";        //目标邮箱        String to = "xxxx@163.com";        //内容        String content = "发送成功";        List arrayList = new ArrayList();        arrayList.add("D:\\1111.png");        String stmpType = "smtp.163.com";        SendMail sendMail = new SendMail(userName,password,to,subject,content,arrayList,stmpType);        sendMail.send();    }


0 0
原创粉丝点击