使用SpringFramework3制作新闻简报(HTML) [英] making newsletter(HTML) with SpringFramework3
问题描述
private void sendMail(Map< String,Object> mailInfo)throws异常{
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(smtp.myhost.com);
mailSender.setPort(587);
mailSender.setUsername(email@email.com);
mailSender.setPassword(12345);
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper mHelper = new MimeMessageHelper(msg,true,UTF-8);
mHelper.setFrom(new InternetAddress(
mailInfo.get(send_mail)。toString(),mailInfo.get(send_name)。toString()));
mHelper.setTo(new InternetAddress(
mailInfo.get(recv_mail)。toString(),mailInfo.get(recv_name)。toString()));
mHelper.setText(mailInfo.get(mail_desc)。toString(),true);
mHelper.setSubject(mailInfo.get(mail_title)。toString());
mailSender.send(msg);
在我的情况下,值 mail_desc
是一个HTML(它具有CSS和其他资源)。 Mail很顺利,但它的CSS和所有图片都被破坏了。
我在JSP中添加了如下所示的所有src值
函数getDomain(){
var DNS = location.href;
DNS = DNS.split('//');
DNS ='http://'+ DNS [1] .substr(0,DNS [1] .indexOf(/));
返回DNS;
$ b因此,当我在浏览器控制台中打印时,它会返回本地主机:8080 /对myApp / {image_src}
。
但是,当我用gmail打开时,它看起来完全不同。它看起来像... ...
< img src =https://ci5.googleusercontent.com/proxy/FVJ1IBTWmX0l0KPlNQVY_AkDsCL02O2Y_kZS7KACQlnXgfgNvNQvjBKpn9zIdPH84N_r- ulunXvzlMCVUOWsMG1WCjfYUFVX7VpjJ5OV5RdpV2ReZFjM9Yw = s0-d-e1-ft#http:// localhost:8080 / resources / gtl_portal / images / newsletter / ci.pngalt =ciclass =CToWUd>
现在我的问题如下:
- 如何在正常情况下实施简报?我在哪里可以找到一些示例或引用?(我认为这可以解决很多问题)
-
如何更改值的东西看起来像。它是非常棘手的,因为它嵌入在style属性中。:
< td height =50pxstyle =background :url('/ resources / images / newsletter / top_bg.png')repeat-x 0 0; padding:15px>
非常感谢:D bb
解决方案您不能像通常那样包含外部CSS,但您可以更喜欢以内联方式包装样式的方式(< head>
标签)。所以像这样的东西,
< style>
.bigFont {
font-size:14px;
}
< style>
< body>
< p class ='bigFont'>我更大< / p>
< / body>
所以这看起来是分开的,而不是添加样式属性到您的标签,您还可以通过退回来避免某些代码。
AFAIK,用于添加内嵌图片 Spring框架有非常好的文档。它受到邮件客户端的广泛支持,例如,
FileSystemResource res = new FileSystemResource(new File(c:/ Sample。 JPG));
helper.addInline(identifier1234,res);
以便您可以简单地将它用作< img src ='cid :identifier1234'>
。
对于高级模板选项,您可以将您的web应用程序与 Apache速度,一个模板库
I am sending newsletter like below with Springframework 3.
private void sendMail(Map<String,Object> mailInfo) throws Exception{
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.myhost.com");
mailSender.setPort(587);
mailSender.setUsername("email@email.com");
mailSender.setPassword("12345");
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper mHelper = new MimeMessageHelper(msg, true, "UTF-8");
mHelper.setFrom(new InternetAddress(
mailInfo.get("send_mail").toString(), mailInfo.get("send_name").toString()));
mHelper.setTo(new InternetAddress(
mailInfo.get("recv_mail").toString(), mailInfo.get("recv_name").toString()));
mHelper.setText(mailInfo.get("mail_desc").toString(), true);
mHelper.setSubject(mailInfo.get("mail_title").toString());
mailSender.send(msg);
}
In my case value of mail_desc
is an HTML(it has css and other resources). Mail goes well, but its CSS and all of images are broken.
I appended to all of src value like below in JSP
function getDomain(){
var DNS = location.href;
DNS = DNS.split('//');
DNS = 'http://' + DNS[1].substr(0,DNS[1].indexOf("/"));
return DNS;
}
So When I print this in browser console it returns localhost:8080/myApp/{image_src}
.
However, When I open with gmail it looks quite different. it looks like...
<img src="https://ci5.googleusercontent.com/proxy/FVJ1IBTWmX0l0KPlNQVY_AkDsCL02O2Y_kZS7KACQlnXgfgNvNQvjBKpn9zIdPH84N_r-ulunXvzlMCVUOWsMG1WCjfYUFVX7VpjJ5OV5RdpV2ReZFjM9Yw=s0-d-e1-ft#http://localhost:8080/resources/gtl_portal/images/newsletter/ci.png" alt="ci" class="CToWUd">
Now I got questions like below :
- How to implement newsletter in Normal? Where can I find some examples or references?(I think this can solve lots of problem here)
How to change value things looks like. it is quite tricky, since it is embedded in style attribute.:
<td height="50px" style="background:url('/resources/images/newsletter/top_bg.png') repeat-x 0 0;padding:15px">
Thanks a lot :D bb
解决方案 You cant include your external css like you do normally , but you can prefer the way of wrapping the styles in the inline way (in <head>
tag). So something like this,
<style>
.bigFont{
font-size:14px;
}
<style>
<body>
<p class='bigFont' >Hi , i am bigger </p>
</body>
so this looks separate instead adding style attribute to your tags , you can also avoid some code by resusing .
AFAIK , for adding inline images Spring framework has very good documentation. It is supported widely by mail clients, an example,
FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
helper.addInline("identifier1234", res);
so that you can simply use it as <img src='cid:identifier1234'>
.
For advanced templating options you can integrate your web app with Apache velocity, a templating library
这篇关于使用SpringFramework3制作新闻简报(HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!