Java实现带附件的邮件发送

作者: Helloword_Cc | 来源:发表于2019-04-03 10:41 被阅读0次

此文章主要实现带附件的邮件发送

首先需要 activation-1.1.jar mail-1.4.7.jar 这两个jar包,如果为Maven项目就注入pom文件,非Maven项目也可以通过Maven官网下载jar包。https://mvnrepository.com/search?q=

本实例以 QQ 邮件服务器为例,你需要在登录QQ邮箱后台在"设置"=》账号中开启POP3/SMTP服务 ,并获取授权码,如下图所示:

WechatIMG387.png

下面上代码块 下面指定邮件发送为qq邮件服务器,我们将 发邮箱的qq账号,获取的授权码,收件人邮箱,附件的实际路径 填入

 // 收件人电子邮箱
        String to =【收件人电子邮箱】;

        // 发件人电子邮箱
        String from = "【发件人电子邮箱】";

        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");

        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("【这里填写发送邮箱账号】", "【这里填写刚才获取的授权码】"); //发件人邮件用户名、授权码
            }
        });

        try {

            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject("This is a YunMen Email");

            // 设置消息体
            message.setText("This is a YunMen Email!");

            // 创建消息部分
            BodyPart messageBodyPart = new MimeBodyPart();

            // 消息
            messageBodyPart.setText("This is a YunMen Email!");

            // 创建多重消息
            Multipart multipart = new MimeMultipart();

            // 设置文本消息部分
            multipart.addBodyPart(messageBodyPart);

            // 附件部分
            messageBodyPart = new MimeBodyPart();
            String filePath = request.getSession().getServletContext().getRealPath("/");
            String filename = filePath + signContextPath.substring(4, signContextPath.length());
            // filename为附件的实际路径
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // 发送完整消息
            message.setContent(multipart);

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();

        }

本地测试成功


image.png

发现部署阿里云服务器会报错连接失败,因为邮箱默认连接25端口,阿里云禁掉了。
我们加入修改端口号并加入SSl验证 ,问题解决。

 final String smtpPort = "465";
        properties.setProperty("mail.smtp.port", smtpPort);
        properties.setProperty("mail.smtp.socketFactory.class", 
        "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
image.png

问题2:设置附件的文件名称如果是中文会乱码,avaMail提供了这样一个类:MimeUtility。使用这个类的encodeText方法就可以对中文进行编码。

 messageBodyPart.setFileName(MimeUtility.encodeText("中文"));

相关文章

网友评论

    本文标题:Java实现带附件的邮件发送

    本文链接:https://www.haomeiwen.com/subject/exlabqtx.html