使用HTML编写简单的邮件模版

转载自http://www.jb51.net/web/355537.html

编写HTML Email的窍门,就是使用15年前的网页制作方法。下面就是我整理的编写指南。

1. Doctype

目前,兼容性最好的Doctype是XHTML 1.0 Strict,事实上Gmail和Hotmail会删掉你的Doctype,换上这个Doctype。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<html xmlns="http://www.w3.org/1999/xhtml">  

 <head>  

  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  

  <title>HTML Email编写指南</title>  

  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>  

 </head>  

</html> 

使用这个Doctype,也就意味着,不能使用HTML5的语法。

 2. 布局

网页的布局(layout)必须使用表格(table)。首先,放置一个最外层的大表格,用来设置背景

<body style="margin: 0; padding: 0;">  

 <table border="1" cellpadding="0" cellspacing="0" width="100%">  

  <tr>  
   <td> Hello! </td>  
  </tr>  

 </table>  

</body>  

表格的 border 属性等于1, 是为了方便开发。正式发布的时候,再把这个属性设为0。
在内层,放置第二个表格。用来展示内容。第二个table的宽度定为600像素,防止超过客户端的显示宽度。
  

<table align="center" border="1" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">  

 <tr>  
  <td> Row 1 </td>  
 </tr>  

 <tr>  
  <td> Row 2 </td>  
 </tr>  

 <tr>  
  <td> Row 3 </td>  
 </tr>  

</table>  

邮件内容有几个部分,就设置几行(row)。

3. 图片

图片是唯一可以引用的外部资源。其他的外部资源,比如样式表文件、字体文件、视频文件等,一概不能引用。
有些客户端会给图片链接加上边框,要去除边框。

  img {outline:none; text-decoration:none; -ms-interpolation-mode: bicubic;}   

  a img {border:none;}   

  <img border="0" style="display:block;">  

需要注意的是,不少客户端默认不显示图片(比如Gmail),所以要确保即使没有图片,主要内容也能被阅读。

4. 行内样式

所有的CSS规则,最好都采用行内样式。因为放置在网页头部的样式,很可能会被客户端删除。客户端对CSS规则的支持情况,请看这里。
另外,不要采用CSS的简写形式,有些客户端不支持。比如,不要写成下面这样:

style="font: 8px/14px Arial, sans-serif;"  

如果想表达

 <p style="margin: 1em 0;">  

要写成下面这样:

<p style="margin-top: 1em; margin-bottom: 1em; margin-left: 0; margin-right: 0;">  

5. W3C校验和测试工具

要保证最终的代码,能够通过W3C的校验,因为某些客户端会把不合格属性剥离。还要使用测试工具(1, 2, 3),查看在不同客户端的显示结果。
发送HTML Email的时候,不要忘记MIME类型不能使用

Content-Type: text/plain; 

而要使用

Content-Type: Multipart/Alternative;  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 首先,需要准备好邮件模板,可以使用HTML代码编写,例如: ``` <!DOCTYPE html> <html> <head> <title>邮件模板</title> </head> <body> <h1>欢迎来到我的网站</h1> <p>尊敬的用户,您好!</p> <p>这是一封测试邮件。</p> <p>请点击下面的链接激活您的账号:</p> <p><a href="https://www.example.com/activate">激活账号</a></p> <p>谢谢您的支持!</p> </body> </html> ``` 2. 在Java中,可以使用JavaMail API来发送邮件。首先,需要创建一个Session对象,用于配置SMTP服务器和邮件发送的属性,例如: ``` Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); ``` 其中,需要替换smtp.example.com为实际的SMTP服务器地址,username和password为SMTP服务器的用户名和密码。 3. 然后,需要创建一个MimeMessage对象,用于表示邮件内容。可以设置邮件的发送者、接收者、主题等属性,例如: ``` MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("测试邮件"); ``` 其中,需要替换sender@example.com和recipient@example.com为实际的发件人和收件人邮箱地址。 4. 接下来,需要将邮件模板转换为HTML格式,并设置为MimeMultipart对象的一部分,例如: ``` String template = "<!DOCTYPE html><html>...</html>"; MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(template, "text/html; charset=utf-8"); MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(htmlPart); ``` 其中,需要将template替换为实际的邮件模板内容。 5. 最后,将MimeMultipart对象设置为MimeMessage对象的内容,并发送邮件,例如: ``` message.setContent(multipart); Transport.send(message); ``` 完整的Java代码示例: ``` import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendHtmlEmail { public static void main(String[] args) throws Exception { String template = "<!DOCTYPE html><html><head><title>邮件模板</title></head><body><h1>欢迎来到我的网站</h1><p>尊敬的用户,您好!</p><p>这是一封测试邮件。</p><p>请点击下面的链接激活您的账号:</p><p><a href=\"https://www.example.com/activate\">激活账号</a></p><p>谢谢您的支持!</p></body></html>"; Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("测试邮件"); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(template, "text/html; charset=utf-8"); MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(htmlPart); message.setContent(multipart); Transport.send(message); } } ``` 运行该程序,即可发送一封包含邮件模板HTML邮件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值