JAVAX实现SMTP协议发送邮件
写在前面:
关于邮件协议:SMTP/POP3/IMAP
SMTP负责发邮件,POP3和IMAP负责收邮件,POP3的操作不会影响服务器上的邮件,IMAP的一些操作可以同步至邮件服务器。
JAVAX 可以实现复杂内容的邮件发送,文本+图片+附件,本案例不涉及正文添加图片,仅涉及 文本+附件。
import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; 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; /** * JAVAX实现SMTP协议发送邮件<br> * JDK1.8 QQ邮箱测试通过<br> * * <pre> * <dependency> * <groupId>javax.mail</groupId> * <artifactId>mail</artifactId> * <version>1.5.0-b01</version> * </dependency> * * <dependency> * <groupId>javax.activation</groupId> * <artifactId>activation</artifactId> * <version>1.1.1</version> * </dependency> * </pre> * * @author dianchao.zhang * */ public class JavaxEmailUtils { public static void main(String[] args) { // 邮件服务器信息 String host = "smtp.qq.com"; // String port = "465"; // boolean useSSL = true; // // 发件账号信息 String user = "sender@qq.com"; // String password = "password"; // String nickName = "nickname"; // // 邮件主体信息 String toAddr = "receiver1@qq.com,receiver2@qq.com"; // String ccAddr = ""; // String bccAddr = ""; // String subject = "subject111"; // String content = "content111" + "<br>" + "content222"; // Vector<File> attachments = null;// send(host, port, useSSL, user, password, nickName, toAddr, ccAddr, bccAddr, subject, content, attachments); } static { System.setProperty("mail.mine.splitlongparameters", "false"); System.setProperty("mail.mine.charset", "UTF-8"); } /** * SMTP协议发送邮件 * * @param host 发件服务器地址 * @param port 发件服务器端口 * @param useSSL 是否启用SSL * @param user 发件人地址 * @param password 发件人密码 * @param nickName 发件人昵称 * @param toAddr 收件人地址(多个用英文逗号分隔) * @param ccAddr 抄送人地址(多个用英文逗号分隔) * @param bccAddr 密送人地址(多个用英文逗号分隔) * @param subject 邮件主题 * @param content 邮件正文 * @param attachments 邮件附件 * @return */ public static boolean send( // // 邮件服务器信息 String host, // String port, // boolean useSSL, // // 发件账号信息 String user, // String password, // String nickName, // // 邮件主体信息 String toAddr, // String ccAddr, // String bccAddr, // String subject, // String content, // Vector<File> attachments// ) { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.host", host); props.setProperty("mail.smtp.port", port); if (useSSL) { // props.setProperty("mail.smtp.ssl.enable", "true"); // props.put("mail.smtp.ssl.socketFactory", new MailSSLSocketFactory()); props.setProperty("mail.smtp.socketFactory.class", "java.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.port", port); } Transport transport = null; try { // 1,创建 session Session session = Session.getInstance(props); // 若开启debug模式,则可以看到程序发送Email的运行状态 session.setDebug(true); // 2,通过session 获取 transport 对象 transport = session.getTransport(); // 3,连接发件服务器 transport.connect(user, password); // transport.connect(host, port, user, password); // 4,创建并组装邮件主体 MimeMessage mimeMessage = new MimeMessage(session); // 4.1,发件人 // mimeMessage.setFrom(new InternetAddress("xx@qq.com")); // mimeMessage.setFrom(new InternetAddress("nickName<xx@qq.com>")); // String fromAddr = new String (nickName.getBytes("UTF-8"), "GB2312") + "<xx@qq.com>"; // mimeMessage.setFrom(new InternetAddress(fromAddr)); mimeMessage.setFrom(new InternetAddress(user, nickName, "UTF-8")); // 4.2,收件人 // mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("xx@qq.com")); mimeMessage.setRecipients(MimeMessage.RecipientType.TO, toAddr); // 4.3,抄送人 mimeMessage.setRecipients(MimeMessage.RecipientType.CC, ccAddr); // 4.4,密送人 mimeMessage.setRecipients(MimeMessage.RecipientType.BCC, bccAddr); // 4.5,设置邮件主题 mimeMessage.setSubject(subject); // mimeMessage.setText("简单正文"); // mimeMessage.setContent("简单正文", "text/html;charset=UTF-8"); // 5,添加正文和附件 // 5.1,创建混合正文 MimeMultipart mimeMultipart = new MimeMultipart(); mimeMultipart.setSubType("mixed"); // 5.2,创建文本正文部分,并添加到混合正文 MimeBodyPart mimeBodyPartText = new MimeBodyPart(); mimeBodyPartText.setContent(content, "text/html;charset=UTF-8");// 避免正文乱码 mimeMultipart.addBodyPart(mimeBodyPartText); // 5.3,创建附件正文部分,并添加到混合正文 if (attachments != null) { for (File attachment : attachments) { MimeBodyPart mimeBodyPartAttachment = new MimeBodyPart(); DataHandler dataHandler = new DataHandler(new FileDataSource(attachment)); String filename = MimeUtility.encodeText(dataHandler.getName()); mimeBodyPartAttachment.setDataHandler(dataHandler); mimeBodyPartAttachment.setFileName(filename); mimeMultipart.addBodyPart(mimeBodyPartAttachment); } } // 将混合正文主体 添加到邮件主体 mimeMessage.setContent(mimeMultipart); mimeMessage.saveChanges(); // 发送邮件 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); return true; } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } return false; } }