Properties prop = new Properties();// 配置邮件的传送协议为smtp。prop.setProperty("mail.transport.protocol", "smtp");// 配置邮件主机,如新浪的SMTP服务器:smtp.sina.comprop.setProperty("mail.host", "smtp.sina.com");// 配置开启邮件传送身份验证prop.setProperty("mail.smtp.auth", "true");3.1.2 创建Authenticator
Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 配置好发送人的用户名和密码。 // 如:belinwu@sina.com,则用户名为belinwu。 return new PasswordAuthentication("belinwu", "********"); }};3.1.3 创建Session对象
Session session = Session.getInstance(prop, authenticator);session.setDebug(true); // 开启输出控制台Debug信息3.2 创建邮件信息
Message message = new MimeMessage(session);// 设置邮件的主题message.setSubject("使用JavaMail技术发送邮件的主题!");// 设置邮件的发送人message.setFrom(new InternetAddress("belinwu@sina.com"));// 设置邮件的收件人message.setRecipient(RecipientType.TO, new InternetAddress("belinwu@qq.com"));/** RecipientType有如下类型:* RecipientType.TO:收件人* RecipientType.CC:抄送* RecipientType.BCC:秘密抄送*/// 设置邮件的正文内容message.setText("使用JavaMail技术发送邮件的正文内容!");3.3 发送邮件
Transport.send(message);4. 发送HTML文件邮件
message.setContent("5. 设置发件人和收件人的名称 5.1 使用MimeUtility类防止中文乱码使用JavaMail技术发送邮件的正文内容!
", "text/html;charset=utf-8");
MimeUtility.encodeText("吴下阿吉")5.2 设置名称
// 设置邮件的发送人message.setFrom(new InternetAddress(MimeUtility.encodeText("吴下阿吉") + "6. 发送一封复杂邮件(以 带附件和正文内容内联图片为例) 6.1 使用Multipart作为Message的Content,即:"));// 设置邮件的收件人message.setRecipient(RecipientType.TO, new InternetAddress(MimeUtility.encodeText("吴下阿林") + " "));
void javax.mail.Part.setContent(Multipart arg0) throws MessagingExceptionMultipart由若干个BodyPart组成,其中 BodyPart可以是一个Multipart,也可以由若干个BodyPart组成。如下图所示:
Multipart messageMultipart = new MimeMultipart(); Multipart contentMultipart = new MimeMultipart();// 附件一BodyPartMimeBodyPart oneBodyPart = new MimeBodyPart();oneBodyPart.setDataHandler(new DataHandler(new FileDataSource("d:/mis.sql")));oneBodyPart.setFileName(MimeUtility.encodeText("信息管理系统.sql")); // 处理乱码// 附件二BodyPartMimeBodyPart twoBodyPart = new MimeBodyPart();twoBodyPart.setDataHandler(new DataHandler(new FileDataSource("d:/movie.txt")));twoBodyPart.setFileName("movie.txt");// 正文BodyPartMimeBodyPart contentBodyPart = new MimeBodyPart();// 正文中的图片BodyPartMimeBodyPart imageBodyPart = new MimeBodyPart();imageBodyPart.setDataHandler(new DataHandler(new FileDataSource("d:/pic.gif")));// 设置CID,可以在真正的正文内容中引用该图片。imageBodyPart.setContentID("image");// 正文内容BodyPartMimeBodyPart messageBodyPart = new MimeBodyPart();messageBodyPart.setContent("6.3 邮件效果图这是一封带附件的邮件!
", "text/html;charset=utf-8");// 绑定从属关系contentMultipart.addBodyPart(imageBodyPart);contentMultipart.addBodyPart(messageBodyPart);contentBodyPart.setContent(contentMultipart);messageMultipart.addBodyPart(oneBodyPart);messageMultipart.addBodyPart(twoBodyPart);messageMultipart.addBodyPart(contentBodyPart);message.setContent(messageMultipart);
import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.Message;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;import javax.mail.internet.MimeUtility;其中需要JAF中的activation.jar包,在JDK 1.4以前需要手动导入到项目中。