简体   繁体   English

HTML有时不显示在电子邮件中

[英]html sometimes not displayed in email

I have written a java code to send html emails. 我已经编写了一个Java代码来发送HTML电子邮件。 When I send the emails to a group of recipients at once, the email is displayed as it should be. 当我一次将电子邮件发送给一组收件人时,该电子邮件将按原样显示。 Here's the code for the same: 这是相同的代码:

String content="<html><p><h1>This is my first java mail with an image but there is a difference.</h1></p><p><img src=\"http://***/ImageLoader2.php\"></p></html>";
m.setSubject(subject);
m.setContent(content,"text/html");

InternetAddress[] toAdd=new InternetAddress[to.length];

    for(int i=0;i<to.length;i++)
        toAdd[i]=new InternetAddress(to[i]);

    for (InternetAddress toAdd1 : toAdd)
                m.addRecipient(Message.RecipientType.TO, toAdd1);
            Transport t=s.getTransport("smtp");
            t.connect(host, user, pass);
            t.sendMessage(m, m.getAllRecipients());
            System.out.println("MESAAGES SENT");
    t.close();

And here's the output: 这是输出:

第一 Now here's the code where I send the emails individually: 现在,这里是我分别发送电子邮件的代码:

m.setSubject(subject);
Transport t=s.getTransport("smtp");
    t.connect(host, user, pass);

            for(int i=0;i<to.length;i++){
                content="<html><p><h1>This is my first java mail with an image but there is a difference.</h1></p><p><img src=\"http://***/ImageLoader2.php?uid="+to[i]+"\"></p></html>";
                InternetAddress toAdd=new InternetAddress(to[i]);
                m.setRecipient(Message.RecipientType.TO, toAdd);
                m.setContent(content,"text/html");
                t.sendMessage(m, m.getAllRecipients());
            }           

            System.out.println("MESAAGES SENT");
    t.close();
        }

And here's the output for this code: 这是此代码的输出:

第二 Why is this happening? 为什么会这样呢? I know css often breaks on web mail systems but I am not using any. 我知道CSS通常会在网络邮件系统上中断,但我没有使用任何CSS。 Google wasn't of any help either. Google也没有任何帮助。

Thanks 谢谢

EDIT 编辑

In the broken mail, these fields are missing in the headers: 在损坏的邮件中,这些字段在标题中丢失:

Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Other than that, there's no other signifiacnt difference. 除此之外,没有其他显着差异。

EDIT 编辑

I also tried setting the headers using m.setHeader() but that didn't work. 我也尝试使用m.setHeader()设置标头,但这没有用。

I think the problem is that you're reusing the same MimeMessage object for each recipient. 我认为问题在于您正在为每个收件人重复使用相同的MimeMessage对象。 Create a new MimeMessage object each time and I suspect it will work better. 每次创建一个新的MimeMessage对象,我怀疑它会更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM