java发送邮件

在做java发送邮件需要两个jar包.

activation.jar

mail.jar

 mailUtils.java


9 import java.io.IOException; 10 11 import java.util.List; 12 13 import java.util.Properties; 14 15 16 17 import javax.mail.Authenticator; 18 19 import javax.mail.Message.RecipientType; 20 21 import javax.mail.MessagingException; 22 23 import javax.mail.PasswordAuthentication; 24 25 import javax.mail.Session; 26 27 import javax.mail.Transport; 28 29 import javax.mail.internet.InternetAddress; 30 31 import javax.mail.internet.MimeBodyPart; 32 33 import javax.mail.internet.MimeMessage; 34 35 import javax.mail.internet.MimeMultipart; 36 37 import javax.mail.internet.MimeUtility; 38 39 40 41 /** 42 43 * 44 45 * @author 本类只有这么一个方法,用来发邮件! 46 47 */ 48 49 public class MailUtils { 50 51 public static Session createSession(String host, final String username, final String password) { 52 53 Properties prop = new Properties(); 54 55 prop.setProperty("mail.host", host);// 指定主机 56 57 prop.setProperty("mail.smtp.auth", "true");// 指定验证为true 58 59 60 61 // 创建验证器 62 63 Authenticator auth = new Authenticator() { 64 65 public PasswordAuthentication getPasswordAuthentication() { 66 67 return new PasswordAuthentication(username, password); 68 69 } 70 71 }; 72 73 // 获取session对象 74 75 return Session.getInstance(prop, auth); 76 77 } 78 79 /** 80 81 * 发送指定的邮件 82 83 * 84 85 * @param mail 86 87 */ 88 89 public static void send(Session session, final Mail mail) throws MessagingException, 90 91 IOException { 92 93 94 95 MimeMessage msg = new MimeMessage(session);// 创建邮件对象 96 97 msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人 98 99 msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人 100 101 102 103 // 设置抄送 104 105 String cc = mail.getCcAddress(); 106 107 if (!cc.isEmpty()) { 108 109 msg.addRecipients(RecipientType.CC, cc); 110 111 } 112 113 114 115 // 设置暗送 116 117 String bcc = mail.getBccAddress(); 118 119 if (!bcc.isEmpty()) { 120 121 msg.addRecipients(RecipientType.BCC, bcc); 122 123 } 124 125 126 127 msg.setSubject(mail.getSubject());// 设置主题 128 129 130 131 MimeMultipart parts = new MimeMultipart();// 创建部件集对象 132 133 134 135 MimeBodyPart part = new MimeBodyPart();// 创建一个部件 136 137 part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容 138 139 parts.addBodyPart(part);// 把部件添加到部件集中 140 141 /////////////////////////////////////////// 142 143 144 145 // 添加附件 146 147 List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件 148 149 if (attachBeanList != null) { 150 151 for (AttachBean attach : attachBeanList) { 152 153 MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件 154 155 attachPart.attachFile(attach.getFile());// 设置附件文件 156 157 attachPart.setFileName(MimeUtility.encodeText(attach 158 159 .getFileName()));// 设置附件文件名 160 161 String cid = attach.getCid(); 162 163 if(cid != null) { 164 165 attachPart.setContentID(cid); 166 167 } 168 169 parts.addBodyPart(attachPart); 170 171 } 172 173 } 174 175 176 177 msg.setContent(parts);// 给邮件设置内容 178 179 Transport.send(msg);// 发邮件 180 181     } 182 183 } 184 185

mailUtilTest.java

mailUtilTest.java

/*

 * 1. 登录邮件服务器

 *   MailUtils.createSession(服务器地址, 登录名, 密码);

 * 2. 创建邮件对象

 *   发件人

 *   收件人

 *   主题

 *   正文

 * 3. 发

 *   需要第一步得到的session、和第二步的邮件对象

 */





Session session = MailUtils.createSession(服务器地址, 登录名, 密码);

Mail mail = new Mail("发件人", "收件人", "主题", "正文");

//发送

MailUtils.send(session, mail);
你配不上自己的野心 也辜负了所受的苦难
原文地址:https://www.cnblogs.com/wdnnccey/p/5854353.html