【问题标题】:Error empty content gmail using javamail api send email smtp错误空内容 gmail 使用 javamail api 发送电子邮件 smtp
【发布时间】:2016-07-19 12:21:54
【问题描述】:

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

并记录:

还有我的代码:

           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);
                    }
                }

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

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

【问题讨论】:

    标签: email gmail jakarta-mail html-email


    【解决方案1】:

    请注意此电子邮件结构: -- 多部分/混合 -- 多部分/替代 -- 文本/纯文本 -- 文本/html -- application/octet-stream(或任何其他 mimetypes)

    【讨论】:

    • 对不起,我的问题不清楚。我发现问题是 jboss 服务器库和 app.ear 库(部署)的冲突库。
    【解决方案2】:

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

    【讨论】:

    • 很高兴你知道了。请注意,您应该能够摆脱程序中的所有套接字工厂属性和变量;你不应该需要它们中的任何一个。
    猜你喜欢
    • 2022-08-14
    • 2012-10-16
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多