发送HTML电子邮件
import java.io.IOException; import java.util.Properties; /* 来 自 n o w j a v a . c o m*/ import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class Main { public static void main(String[] args) { String host = "smtp.gmail.com";// 来自 N o w J a v a . c o m String username = "mymailusername"; String password = "mygmailpassword"; String from = "mygmailusername@gmail.com"; String to = "someuser@somewhere.com"; Properties properties = new Properties(); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "465"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.user", username); properties.put("mail.smtp.password", password); Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Subject Test"); // Create Mime Content MimeBodyPart messageBodyPart = new MimeBodyPart(); String html = "<H1>Important Message</H1>" + "<b>This is an important message...</b>" + "<br/><br/>" + "<i>Be sure to code your Java today!</i>" + "<H2>It is the right thing to do!</H2>"; messageBodyPart.setContent(html, "text/html; charset=utf-8"); MimeBodyPart fileBodyPart = new MimeBodyPart(); fileBodyPart.attachFile("<path-to-attachment>/attach.txt"); MimeBodyPart fileBodyP