Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine JAVA:在Google app engine上使用JAVA mail api时,如何在发送的邮件中嵌入html?_Java_Google App Engine_Jakarta Mail - Fatal编程技术网

Google app engine JAVA:在Google app engine上使用JAVA mail api时,如何在发送的邮件中嵌入html?

Google app engine JAVA:在Google app engine上使用JAVA mail api时,如何在发送的邮件中嵌入html?,java,google-app-engine,jakarta-mail,Java,Google App Engine,Jakarta Mail,这是我用来发送邮件的工作代码,但是如果我在setText()方法的string参数中包含html内容,那么对用户来说,它只显示为string,没有html效果 Message msg = new MimeMessage(session1); msg.setFrom(new InternetAddress("abc@xyz.com", "Team Application")); msg.addRecipient(Message.RecipientTy

这是我用来发送邮件的工作代码,但是如果我在setText()方法的string参数中包含html内容,那么对用户来说,它只显示为string,没有html效果

        Message msg = new MimeMessage(session1);
        msg.setFrom(new InternetAddress("abc@xyz.com", "Team Application"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, "Dear "+name1+"."));
        msg.setSubject("Registration confirmation mail");
        msg.setText("Dear "+name1+",\nThanks for registering with us.");
        Transport.send(msg);

尝试使用setContent而不是setText
因此,对于您的代码示例:

    Message msg = new MimeMessage(session1);
    msg.setFrom(new InternetAddress("abc@xyz.com", "Team Application"));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, "Dear "+name1+"."));
    msg.setSubject("Registration confirmation mail");
    msg.setContent("Dear <i>"+name1+"</i>,<br>Thanks for registering with us.", "text/html");
    Transport.send(msg);
Message msg=new mimessage(会话1);
msg.setFrom(新的InternetAddress(“abc@xyz.com“,”团队申请“);
msg.addRecipient(Message.RecipientType.TO,新的Internet地址(电子邮件,“亲爱的”+name1+”);
msg.setSubject(“注册确认邮件”);
msg.setContent(“亲爱的”+name1+”,
感谢您向我们注册。”,“text/html”); 发送(msg);
就个人而言,我使用文本和html版本的多部分消息。这是我自己代码的一部分:

        // Unformatted text version
        final MimeBodyPart textPart = new MimeBodyPart();
        textPart.setText("plain content");
        // HTML version
        final MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("<b>html content</b>", "text/html");
        // Create the Multipart.  Add BodyParts to it.
        final Multipart mp = new MimeMultipart();
        mp.addBodyPart(textPart);
        mp.addBodyPart(htmlPart);
        // Set Multipart as the message's content
        msg.setContent(mp);
//未格式化文本版本
最终MimeBodyPart textPart=新MimeBodyPart();
textPart.setText(“普通内容”);
//HTML版本
最终MimeBodyPart htmlPart=新的MimeBodyPart();
setContent(“html内容”、“text/html”);
//创建多部分。添加车身部件。
最终多部分mp=新的mimultipart();
mp.addBodyPart(文本部分);
mp.addBodyPart(htmlPart);
//将Multipart设置为消息的内容
msg.setContent(mp);
检查Mime消息时,可以在重载签名中指定字符集和Mime子类型:


您应该使用来自低级API的MailService.Message和MailService。例如:

Message msg = new Message();
    msg.setSender(_sender);
    msg.setTo(_recipient);
    msg.setSubject(_msgSubject);
    msg.setHtmlBody("<h1 style="height:1200px;">THIS IS RUSSIA!!!</h1>");
    MailService service = MailServiceFactory.getMailService();
    try {
        service.send(msg);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Message msg=new Message();
msg.setSender(_发送方);
msg.setTo(_收件人);
msg.setSubject(_msgSubject);
msg.setHtmlBody(“这是俄罗斯!!!”);
MailService=MailServiceFactory.getMailService();
试一试{
服务发送(msg);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

@ANSHUL JAIN:忽略它-这是POJO在发短信。getText()和getHtml()返回字符串(现在已从源代码中删除)。在我的例子中,Mozilla Thunderbird无法正确解析msg.setText(“您的html正文”、“utf-8”、“html”)。不过,邮件正文中包含的链接谢谢您,先生。我们要尊重俄国人,请不要羞辱俄国人。您应该避免使用双引号,以使这种代码的和平工作
Message msg = new Message();
    msg.setSender(_sender);
    msg.setTo(_recipient);
    msg.setSubject(_msgSubject);
    msg.setHtmlBody("<h1 style="height:1200px;">THIS IS RUSSIA!!!</h1>");
    MailService service = MailServiceFactory.getMailService();
    try {
        service.send(msg);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }