send HTML mail with JavaMail.
2011-12-21 16:44
429 查看
http://www.rgagnon.com/javadetails/java-0504.html
It's easy to send HTML mail with JavaMail. Simply set the content type to "text/html".
One approach to include images in the mail body is to use the IMG tag and make the images available on a server.
The browser accesses these images just as if it were displaying an image in a Web page. Unfortunately, spammers have used this mechanism as a sneaky way to record who visits their site (and mark your email as valid). To protect your privacy, many Web-based (and other) email clients don't display images in HTML emails.
An alternative to placing absolute URLs to images in your HTML is to include the images as attachments to the email. The HTML can reference the image in an attachment by using the protocol prefix cid: plus the content-id of the attachment.
It's easy to send HTML mail with JavaMail. Simply set the content type to "text/html".
import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; class SimpleMail { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.setContent("<h1>Hello world</h1>", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
One approach to include images in the mail body is to use the IMG tag and make the images available on a server.
import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; class SimpleMail1 { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.setContent ("<h1>This is a test</h1>" + "<img src=\"http://www.rgagnon.com/images/jht.gif\">", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
The browser accesses these images just as if it were displaying an image in a Web page. Unfortunately, spammers have used this mechanism as a sneaky way to record who visits their site (and mark your email as valid). To protect your privacy, many Web-based (and other) email clients don't display images in HTML emails.
An alternative to placing absolute URLs to images in your HTML is to include the images as attachments to the email. The HTML can reference the image in an attachment by using the protocol prefix cid: plus the content-id of the attachment.
import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.Properties; class SimpleMail2 { public static void main(String[] args) throws Exception{ System.out.println("Sending mail..."); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.mymailserver.com"); props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd"); Session mailSession = Session.getDefaultInstance(props, null); mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("HTML mail with images"); message.setFrom(new InternetAddress("me@sender.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("you@receiver.com")); // // This HTML mail have to 2 part, the BODY and the embedded image // MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\images\\jht.gif"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID","<image>"); // add it multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }
相关文章推荐
- javamail send mail with 附件
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- Python example for send mail with attachment by calling office outlook
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server
- Spring邮件服务之Html:Maven + Spring SMTP Mail With Html
- 关于javaMail的发送邮件之简便方法(邮件带有附件及html内容中携带图标)
- How to send Lotus Notes mail messages with Microsoft Visual Basic
- Send out html emails in php using the mail func...
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- Java Mail send email
- Java Send EMail with Attachment
- Java send mail
- javaMail SMTPSendFailedException: 554 DT:SPM 163 smtp4
- Use Java Mail API to Send Email
- Java + 腾讯企业邮箱 + javamail + SSL 发送邮件(转载:http://www.cnblogs.com/LUA123/p/5575134.html)
- Send mail with attachment in asp.net
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板