使用javamail api发送电子邮件smtp错误空内容gmail

时间:2016-03-31 07:24:23

标签: email gmail javamail html-email

我在工作项目:jsf,richfaces。我刚刚在https://java.net/projects/javamail/pages/Home更新了javamail api最新版本1.5.5 当我测试从gmail发送电子邮件到我的Gmail时, 主题:主题测试。 内容:内容测试。并配置: smtp.gmail.com 465 SSL 。 我的收件箱中只有主题和无内容接收gmail:

enter image description here

并记录:

enter image description here

我的代码:

           try {
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true); 
                final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
                // Set the host smtp address
                props.put("mail.smtp.host", SMTP_HOST_NAME);
                props.put("mail.smtp.port", SMTP_PORT);
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
                props.put("mail.smtp.socketFactory.port", SMTP_PORT);
                props.put("mail.smtp.ssl.trust", "*");
                props.put("mail.smtp.ssl.socketFactory", sf);
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }
            .......
            .......
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            // create a message
            MimeMessage msg = new MimeMessage(session);

            msg.setText("UTF8");
            // set the from and to address
            InternetAddress addressFrom = new InternetAddress(SMTP_AUTH_USER);
            msg.setFrom(addressFrom);
            System.out.println(addressFrom);
            msg.setSubject(subject, "utf-8");
            ......
            ......
                try {
                    String content = replaceCharacterInEmail(cus, null,null, message);

                    MimeBodyPart mbp1 = new MimeBodyPart();
                    mbp1.setContent(content, "text/html; charset=utf-8");
                    // attach the file to the message
                    Multipart mp = new MimeMultipart();
                    int count = 0;
                    if(files != null){
                        while (count < files.size()) {
                            MimeBodyPart mbdp = new MimeBodyPart();
                            mbdp.attachFile(files.get(count));
                            mp.addBodyPart(mbdp);
                            count++;
                        }
                    }
                    // create the Multipart and add its parts to it
                    mp.addBodyPart(mbp1);
                    // add the Multipart to the message
                    msg.setContent(mp);
                    Transport.send(msg);
                } catch (Exception e) {
                    invalidAddress += (", " + address.trim());
                    e.printStackTrace();
                }finally{
                    if (!"".equals(emailFails)) {
                        emailFails = emailFails.substring(1);
                    }
                }

但是,当我在另一个项目测试(java应用程序)上面用lib java java测试上面的代码时。没关系 : enter image description here

我不认为lib javamail版本存在问题。我不知道2项目之间有什么不同,因为部分发送电子邮件是相似的。我该如何解决这个错误?

2 个答案:

答案 0 :(得分:0)

请遵守此电子邮件结构: - multipart / mixed    - mutlipart / alternative      - text / plain      - text / html    - application / octet-stream(或任何其他mimetypes)

答案 1 :(得分:0)

我发现问题是jboss服务器的lib和app.ear(deploy)的lib的冲突库。