当前页面: 开发资料首页 → J2EE 专题 → javamail发送消息的格式问题
javamail发送消息的格式问题
摘要: javamail发送消息的格式问题
使用javamail发送一段HTML格式的文本,例如:<table><tr><td>aaaaa</td></tr></table>。如何设置才能使接收方查看邮件时看到的时一个显示出来的tabel而不是单纯的字符串文本呢?
我这里有一个例子:
public class SendAnEmail_Html {
/** Creates a new instance of SendAnEmail */
public SendAnEmail_Html() {
}
public static void main(String[] args) {
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props);
String smtpServer = "smtp.163.com";
String from = "someone@163.com";
String to="someone@sohu.com";
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
Message newMessage = new MimeMessage(sendMailSession);
try {
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject("我测试");
newMessage.setSentDate(new Date());
BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent("
我测试HTML文本
","text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
newMessage.setContent(mm);//把mm作为消息对象的内容
transport = sendMailSession.getTransport("smtp");
transport.connect(smtpServer,"someone","somepassword");
newMessage.saveChanges();
transport.sendMessage(newMessage,newMessage.getAllRecipients());
transport.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}