【工程截图】
【代码实现】
【Mail.java】
package com.Higgin.Utils; import java.util.ArrayList; import java.util.List; /** * 表示邮件类,你需要设置:账户名和密码、收件人、抄送(可选)、暗送(可选)、主题、内容,以及附件(可选) * * 在创建了Mail对象之后 * 可以调用它的setSubject()、setContent(),设置主题和正文 * 也可以调用setFrom()和 addToAddress(),设置发件人,和添加收件人。 * 也可以调用addAttch()添加附件 * 创建AttachBean:new AttachBean(new File("..."), "fileName"); */ public class Mail { private String from; //发件人 private StringBuilder toAddress = new StringBuilder(); //收件人 private StringBuilder ccAddress = new StringBuilder(); //抄送 private StringBuilder bccAddress = new StringBuilder(); //暗送 private String subject;//主题 private String content;//正文 // 附件列表 private List<AttachBean> attachList = new ArrayList<AttachBean>(); public Mail() {} public Mail(String from, String to) { this(from, to, null, null); } public Mail(String from, String to, String subject, String content) { this.from = from; this.toAddress.append(to); this.subject = subject; this.content = content; } /** * 返回发件人 * @return */ public void setFrom(String from) { this.from = from; } /** * 返回发件人 * @return */ public String getFrom() { return from; } /** * 返回主题 */ public String getSubject() { return subject; } /** * 设置主题 */ public void setSubject(String subject) { this.subject = subject; } /** * 获取主题内容 */ public String getContent() { return content; } /** * 设置主题内容 * @param content */ public void setContent(String content) { this.content = content; } /** * 获取收件人 * @return */ public String getToAddress() { return toAddress.toString(); } /** * 获取抄送 * @return */ public String getCcAddress() { return ccAddress.toString(); } /** * 获取暗送 * @return */ public String getBccAddress() { return bccAddress.toString(); } /** * 添加收件人,可以是多个收件人 * @param to */ public void addToAddress(String to) { if(this.toAddress.length() > 0) { this.toAddress.append(","); } this.toAddress.append(to); } /** * 添加抄送人,可以是多个抄送人 * @param cc */ public void addCcAddress(String cc) { if(this.ccAddress.length() > 0) { this.ccAddress.append(","); } this.ccAddress.append(cc); } /** * 添加暗送人,可以是多个暗送人 * @param bcc */ public void addBccAddress(String bcc) { if(this.bccAddress.length() > 0) { this.bccAddress.append(","); } this.bccAddress.append(bcc); } /** * 添加附件,可以添加多个附件 * @param attachBean */ public void addAttach(AttachBean attachBean) { this.attachList.add(attachBean); } /** * 获取所有附件 * @return */ public List<AttachBean> getAttachs() { return this.attachList; } }
【AttachBean.java】
package com.Higgin.Mail.Utils; import java.io.File; /** * 发送附件用 */ public class AttachBean { private String cid; private File file; private String fileName; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public AttachBean() { } public AttachBean(File file, String fileName) { super(); this.file = file; this.fileName = fileName; } }
【MailUtils.java】
package com.Higgin.Utils; import java.io.IOException; import java.util.List; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; 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; /** * 邮件工具类 * @author YH * */ public class MailUtils { public static Session createSession(String host, final String username, final String password) { Properties prop = new Properties(); prop.setProperty("mail.host", host); // 指定主机 prop.setProperty("mail.smtp.auth", "true"); // 指定验证为true // 创建验证器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }; // 获取session对象 return Session.getInstance(prop, auth); } /** * 发送指定的邮件 * * @param mail */ public static void send(Session session, final Mail mail) throws MessagingException, IOException { MimeMessage msg = new MimeMessage(session);// 创建邮件对象 msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人 msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人 // 设置抄送 String cc = mail.getCcAddress(); if (!cc.isEmpty()) { msg.addRecipients(RecipientType.CC, cc); } // 设置暗送 String bcc = mail.getBccAddress(); if (!bcc.isEmpty()) { msg.addRecipients(RecipientType.BCC, bcc); } msg.setSubject(mail.getSubject());// 设置主题 MimeMultipart parts = new MimeMultipart();// 创建部件集对象 MimeBodyPart part = new MimeBodyPart();// 创建一个部件 part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容 parts.addBodyPart(part);// 把部件添加到部件集中 /////////////////////////////////////////// // 添加附件 List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件 if (attachBeanList != null) { for (AttachBean attach : attachBeanList) { MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件 attachPart.attachFile(attach.getFile());// 设置附件文件 attachPart.setFileName(MimeUtility.encodeText(attach .getFileName()));// 设置附件文件名 String cid = attach.getCid(); if(cid != null) { attachPart.setContentID(cid); } parts.addBodyPart(attachPart); } } msg.setContent(parts);// 给邮件设置内容 Transport.send(msg);// 发邮件 } }
【MailUtilsTest.java】测试类
package com.Higgin.Mail.Demo; import java.io.IOException; import javax.mail.MessagingException; import javax.mail.Session; import org.junit.Test; import com.Higgin.Mail.Utils.Mail; import com.Higgin.Mail.Utils.MailUtils; public class MailUtilsTest { /*** 发邮件 * @throws IOException * @throws MessagingException ***/ @Test public void sendMail() throws MessagingException, IOException{ /** * 1.登录邮件服务器 * MailUtils.createSession(服务器地址,登录名,密码); * 2.创建邮件对象 * 发件人 * 收件人 * 主题 * 正文 * 3.发邮件 * 需要第1步得到的session 以及 第2步的邮件对象 */ Session session=MailUtils.createSession("smtp.163.com", "cuixinjie1993", "cuixinjie520"); //public Mail(String from, String to, String subject, String content) Mail mail=new Mail("[email protected]","[email protected]","测试邮件标题啊","邮件内容啊"); //发送 MailUtils.send(session, mail); } }
时间: 2024-10-10 15:42:15