public class JavaMailServiceRImpl {
    //邮箱用户名
    private static final  String  account = "********";
    // 登录密码
    private static final String password = "********";
    // smtp服务器地址,不同邮箱不同(注意)
    private static final String host = "smtp.exmail.qq.com";
    // 端口
    private static final String port = "465";
    // 协议
    private static final String protocol = "SMTP";
    //正文
    private static final String context = "您好:<br/><br/>" +
            "    1234567894612303。<br/><br/>" +
            "                " +
            "              " +
            "              " +
            "               " +
            "123456<br/><br/>" +
            "提示:请不要往本邮箱发送邮件!";

    
    public void sendMail1(JavaMailModel javaMailModel) {
        //组装邮件体
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", protocol);
        props.setProperty("mail.host", host);
        props.setProperty("mail.smtp.port", port);
        // 指定验证为true
        props.setProperty("mail.smtp.auth", "true");
        // 使用SSL,企业邮箱必需 start 开启安全协议
        MailSSLSocketFactory mailSSLSocketFactory = null;
        try {
            mailSSLSocketFactory = new MailSSLSocketFactory();
            mailSSLSocketFactory.setTrustAllHosts(true);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        props.put("mail.smtp.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.socketFactory.port", port);

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //第一个参数为发送邮件的邮箱名
                //第二个参数为密码(注意:普通邮箱为邮箱的授权码)
                return new PasswordAuthentication(account, password);
            }
        };
        //开启session
        Session session = Session.getInstance(props, auth);
        // 2.创建一个Message,它相当于是邮件内容
        Message message = new MimeMessage(session);
        // 设置邮件接收方,多用户发送,users中的各邮箱之间用,隔开
        //Address[] internetAddressTo = new InternetAddress().parse(users);
        try{

            message.setFrom(new InternetAddress(account)); // 设置发送者

            message.setRecipient(Message.RecipientType.TO, new InternetAddress(javaMailModel.getRecipientAddress())); //设置发送方式与接收者
            //邮件标题
            message.setSubject("稿酬单:"+javaMailModel.getSubject());
            // message.setText("这是一封激活邮件,请<a href='#'>点击</a>");
            //封装正文和附件
            Multipart multipart = mesContent(context,javaMailModel.getMailAttachmentUrl());
            //设置正文内容
            message.setContent(multipart);
            //创建 Transport用于将邮件发送
            Transport.send(message);
        }catch (Exception e){
            System.out.println("邮件发送失败"+e.....);
        }
    }

    /**
     * 组装邮件正文和附件
     * @param msg
     * @param filename
     * @return
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public Multipart mesContent(String msg,String filename) throws MessagingException, UnsupportedEncodingException {
        // 创建消息部分
        BodyPart messageBodyPart = new MimeBodyPart();
        // 消息,并设置发送的格式
        messageBodyPart.setDataHandler(new DataHandler(msg,"text/html;charset=UTF-8"));
//        messageBodyPart.setText(msg);
        // 创建多重消息
        Multipart multipart = new MimeMultipart();
        // 设置文本消息部分
        multipart.addBodyPart(messageBodyPart);
        // 附件部分
        messageBodyPart = new MimeBodyPart();
        // 设置要发送附件的文件路径
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        // 处理附件名称中文(附带文件路径)乱码问题
        messageBodyPart.setFileName(MimeUtility.encodeText(filename));
        multipart.addBodyPart(messageBodyPart);
        return multipart;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.