Java 与 邮件发送

本文介绍如何在QQ邮箱中获取授权码,并通过Java程序发送带有授权码的邮件。包括设置SMTP服务器、验证账号密码、构造邮件内容等关键步骤。

在QQ邮箱设置中获取授权码


main

Map<String, String> map = new HashMap<String, String>();
        // 发送邮箱地址 授权码
        SendMail mail = new SendMail("发送@qq.com", "授权码");

        map.put("mail.smtp.host", "smtp.qq.com");

        // 暂时未成功,需要调试
        /*
         * SendMail mail = new SendMail("14789****@sina.cn","***miya");
         * map.put("mail.smtp.host", "smtp.sina.com");
         */
        map.put("mail.smtp.auth", "true");
        map.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        map.put("mail.smtp.port", "465");
        map.put("mail.smtp.socketFactory.port", "465");
        mail.setPros(map);
        mail.initMessage();
        /*
         * 添加收件人有三种方法: 1,单人添加(单人发送)调用setRecipient(str);发送String类型
         * 2,多人添加(群发)调用setRecipients(list);发送list集合类型
         * 3,多人添加(群发)调用setRecipients(sb);发送StringBuffer类型
         */

        List<String> list = new ArrayList<String>();
        list.add("接受邮箱");
        // list.add("***92@sina.cn");
        // list.add("****@163.com");
        // 添加接受邮箱

        try {
            mail.setRecipients(list);
            /*
             * String defaultStr =
             * "283942930@qq.com,429353942@qq.com,2355663819@qq.com,381766286@qq.com;
             * StringBuffer sb = new StringBuffer(); sb.append(defaultStr);
             * sb.append(",316121113@qq.com"); mail.setRecipients(sb);
             */

            mail.setSubject("评论");// 标题
            // mail.setText("谢谢合作");
            mail.setDate(new Date());// 时间
            mail.setFrom(user);// 发送人昵称
            // mail.setMultipart("D:a.txt");
            mail.setContent("发送的内容", "text/html; charset=UTF-8");
            /*
             * List<String> fileList = new ArrayList<String>();
             * fileList.add("D:1.jpg"); fileList.add("D:activation.zip");
             * fileList.add("D:dstz.sql"); fileList.add("D:软件配置要求.doc");
             * mail.setMultiparts(fileList);
             */
            System.out.println(mail.sendMessage());
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

工具类

public class SendMail {
    private String username = null;
    private String password = null;
    private Authenticator auth = null;
    private MimeMessage mimeMessage = null;
    private Properties pros = null;
    private Multipart multipart = null;
    private BodyPart bodypart = null;

    /**
     * 初始化账号密码并验证 创建MimeMessage对象 发送邮件必须的步骤:1
     * 
     * @param username
     * @param password
     */
    public SendMail(String username, String password) {
        this.username = username;
        this.password = password;
    }

    /**
     * 初始化MimeMessage对象 发送邮件必须的步骤:3
     */
    public void initMessage() {
        this.auth = new Email_Autherticator();
        Session session = Session.getDefaultInstance(pros, auth);
        session.setDebug(true); // 设置获取 debug 信息
        mimeMessage = new MimeMessage(session);
    }

    /**
     * 设置email系统参数 接收一个map集合key为string类型,值为String 发送邮件必须的步骤:2
     * 
     * @param map
     */
    public void setPros(Map<String, String> map) {
        pros = new Properties();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            pros.setProperty(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 验证账号密码 发送邮件必须的步骤
     * 
     * @author Administrator
     * 
     */
    public class Email_Autherticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }

    /**
     * 设置发送邮件的基本参数(去除繁琐的邮件设置)
     * 
     * @param sub
     *            设置邮件主题
     * @param text
     *            设置邮件文本内容
     * @param rec
     *            设置邮件接收人
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public void setDefaultMessagePros(String sub, String text, String rec)
            throws MessagingException, UnsupportedEncodingException {
        mimeMessage.setSubject(sub);
        mimeMessage.setText(text);
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
                rec));
        mimeMessage.setSentDate(new Date());
        mimeMessage.setFrom(new InternetAddress(username, username));
    }

    /**
     * 设置主题
     * 
     * @param subject
     * @throws MessagingException
     */
    public void setSubject(String subject) throws MessagingException {
        mimeMessage.setSubject(subject);
    }

    /**
     * 设置日期
     * 
     * @param date
     * @throws MessagingException
     */
    public void setDate(Date date) throws MessagingException {
        mimeMessage.setSentDate(new Date());
    }

    /**
     * 设置邮件文本内容
     * 
     * @param text
     * @throws MessagingException
     */
    public void setText(String text) throws MessagingException {
        mimeMessage.setText(text);
    }

    /**
     * 设置邮件头部
     * 
     * @param arg0
     * @param arg1
     * @throws MessagingException
     */
    public void setHeader(String arg0, String arg1) throws MessagingException {
        mimeMessage.setHeader(arg0, arg1);
    }

    /**
     * 设置邮件接收人地址 <单人发送>
     * 
     * @param recipient
     * @throws MessagingException
     */
    public void setRecipient(String recipient) throws MessagingException {
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
                recipient));
    }

    /**
     * 设置邮件接收人地址 <多人发送>
     * 
     * @param list
     * @throws MessagingException
     * @throws AddressException
     */
    public String setRecipients(List<String> recs) throws AddressException,
            MessagingException {
        if (recs.isEmpty()) {
            return "接收人地址为空!";
        }
        for (String str : recs) {
            mimeMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(str));
        }
        return "加入接收人地址成功!";
    }

    /**
     * 设置邮件接收人地址 <多人发送>
     * 
     * @param StringBuffer
     *            <parms,parms2,parms.....>
     * @throws MessagingException
     * @throws AddressException
     */
    @SuppressWarnings("static-access")
    public String setRecipients(StringBuffer sb) throws AddressException,
            MessagingException {
        if (sb == null || "".equals(sb)) {
            return "字符串数据为空!";
        }
        Address[] address = new InternetAddress().parse(sb.toString());
        mimeMessage.addRecipients(Message.RecipientType.TO, address);
        return "收件人加入成功";
    }

    /**
     * 设置邮件发送人的名字
     * 
     * @param from
     * @throws UnsupportedEncodingException
     * @throws MessagingException
     */
    public void setFrom(String from) throws UnsupportedEncodingException,
            MessagingException {
        mimeMessage.setFrom(new InternetAddress(username, from));
    }

    /**
     * 发送邮件<单人发送> return 是否发送成功
     * 
     * @throws MessagingException
     */
    public String sendMessage() throws MessagingException {
        Transport.send(mimeMessage);
        return "success";
    }

    /**
     * 设置附件
     * 
     * @param file
     *            发送文件的路径
     */
    public void setMultipart(String file) throws MessagingException,
            IOException {
        if (multipart == null) {
            multipart = new MimeMultipart();
        }
        multipart.addBodyPart(writeFiles(file));
        mimeMessage.setContent(multipart);
    }

    /**
     * 设置附件<添加多附件>
     * 
     * @param fileList
     *            <接收List集合>
     * @throws MessagingException
     * @throws IOException
     */
    public void setMultiparts(List<String> fileList) throws MessagingException,
            IOException {
        if (multipart == null) {
            multipart = new MimeMultipart();
        }
        for (String s : fileList) {
            multipart.addBodyPart(writeFiles(s));
        }
        mimeMessage.setContent(multipart);
    }

    /**
     * 发送文本内容,设置编码方式 <方法与发送附件配套使用> <发送普通的文本内容请使用setText()方法>
     * 
     * @param s
     * @param type
     * @throws MessagingException
     */
    public void setContent(String s, String type) throws MessagingException {
        if (multipart == null) {
            multipart = new MimeMultipart();
        }
        bodypart = new MimeBodyPart();
        bodypart.setContent(s, type);
        multipart.addBodyPart(bodypart);
        mimeMessage.setContent(multipart);
        mimeMessage.saveChanges();
    }

    /**
     * 读取附件
     * 
     * @param filePath
     * @return
     * @throws IOException
     * @throws MessagingException
     */
    public BodyPart writeFiles(String filePath) throws IOException,
            MessagingException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new IOException("文件不存在!请确定文件路径是否正确");
        }
        bodypart = new MimeBodyPart();
        DataSource dataSource = new FileDataSource(file);
        bodypart.setDataHandler(new DataHandler(dataSource));
        // 文件名要加入编码,不然出现乱码
        bodypart.setFileName(MimeUtility.encodeText(file.getName()));
        return bodypart;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值