http:jhobby.javaeye.comblog226419

xiaoxiao2022-06-16  31

(1) Mail.java 文件。 Java代码 复制代码 1. import java.util.ArrayList; 2. import java.util.Date; 3. import java.util.List; 4. 5. /** 6. * Mail 类表示要发送的一封邮件。 7. * @author dingli 8. */ 9. public class Mail 10. { 11. private String subject = null; 12. private String from = null; 13. private List sendTo = null; 14. private List carbonCopy = null; 15. private List blindCarbonCopy = null; 16. private List attachment = null; 17. private String content = null; 18. private Date sendDate = null; 19. 20. /** 21. * Mail 的默认构造方法。 22. */ 23. public Mail() 24. { 25. this.sendTo = new ArrayList(); 26. this.carbonCopy = new ArrayList(); 27. this.blindCarbonCopy = new ArrayList(); 28. this.attachment = new ArrayList(); 29. this.sendDate = new Date(System.currentTimeMillis()); 30. } 31. 32. /** 33. * 获取邮件附件所在本地路径列表。 34. * @return 邮件附件所在本地路径列表。 35. */ 36. public List getAttachment() 37. { 38. return attachment; 39. } 40. 41. /** 42. * 设置邮件附件所在本地路径。 43. * @param path 邮件附件所在本地路径。 44. */ 45. public void setAttachment(String path) 46. { 47. this.attachment.add(path); 48. } 49. 50. /** 51. * 获取邮件暗送人列表。 52. * @return 邮件暗送人列表。 53. */ 54. public List getBlindCarbonCopy() 55. { 56. return blindCarbonCopy; 57. } 58. 59. /** 60. * 设置邮件暗送人列表。 61. * @param blindCarbonCopy 邮件暗送人列表。 62. */ 63. public void setBlindCarbonCopy(List blindCarbonCopy) 64. { 65. this.blindCarbonCopy = blindCarbonCopy; 66. } 67. 68. /** 69. * 获取邮件抄送人列表。 70. * @return 邮件抄送人列表。 71. */ 72. public List getCarbonCopy() 73. { 74. return carbonCopy; 75. } 76. 77. /** 78. * 设置邮件抄送人列表。 79. * @param carbonCopy 邮件抄送人列表。 80. */ 81. public void setCarbonCopy(List carbonCopy) 82. { 83. this.carbonCopy = carbonCopy; 84. } 85. 86. /** 87. * 获取邮件收件人列表。 88. * @return 邮件收件人列表。 89. */ 90. public List getSendTo() 91. { 92. return sendTo; 93. } 94. 95. /** 96. * 设置邮件收件人列表。 97. * @param sendTo 邮件收件人列表。 98. */ 99. public void setSendTo(List sendTo) 100. { 101. this.sendTo = sendTo; 102. } 103. 104. /** 105. * 获取邮件内容。 106. * @return 邮件内容。 107. */ 108. public String getContent() 109. { 110. return content; 111. } 112. 113. /** 114. * 设置邮件内容。 115. * @param content 邮件内容。 116. */ 117. public void setContent(String content) 118. { 119. this.content = content; 120. } 121. 122. /** 123. * 获取邮件发件人。 124. * @return 邮件发件人。 125. */ 126. public String getFrom() 127. { 128. return from; 129. } 130. 131. /** 132. * 设置邮件发件人。 133. * @param from 邮件发件人。 134. */ 135. public void setFrom(String from) 136. { 137. this.from = from; 138. } 139. 140. /** 141. * 获取邮件发送日期。 142. * @return 邮件发送日期。 143. */ 144. public Date getSendDate() 145. { 146. return sendDate; 147. } 148. 149. /** 150. * 设置邮件发送日期。 151. * @param sendDate 邮件发送日期。 152. */ 153. public void setSendDate(Date sendDate) 154. { 155. this.sendDate = sendDate; 156. } 157. 158. /** 159. * 获取邮件主题。 160. * @return 邮件主题。 161. */ 162. public String getSubject() 163. { 164. return subject; 165. } 166. 167. /** 168. * 设置邮件主题。 169. * @param subject 邮件主题。 170. */ 171. public void setSubject(String subject) 172. { 173. this.subject = subject; 174. } 175. } import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Mail 类表示要发送的一封邮件。 * @author dingli */ public class Mail { private String subject = null; private String from = null; private List sendTo = null; private List carbonCopy = null; private List blindCarbonCopy = null; private List attachment = null; private String content = null; private Date sendDate = null; /** * Mail 的默认构造方法。 */ public Mail() { this.sendTo = new ArrayList(); this.carbonCopy = new ArrayList(); this.blindCarbonCopy = new ArrayList(); this.attachment = new ArrayList(); this.sendDate = new Date(System.currentTimeMillis()); } /** * 获取邮件附件所在本地路径列表。 * @return 邮件附件所在本地路径列表。 */ public List getAttachment() { return attachment; } /** * 设置邮件附件所在本地路径。 * @param path 邮件附件所在本地路径。 */ public void setAttachment(String path) { this.attachment.add(path); } /** * 获取邮件暗送人列表。 * @return 邮件暗送人列表。 */ public List getBlindCarbonCopy() { return blindCarbonCopy; } /** * 设置邮件暗送人列表。 * @param blindCarbonCopy 邮件暗送人列表。 */ public void setBlindCarbonCopy(List blindCarbonCopy) { this.blindCarbonCopy = blindCarbonCopy; } /** * 获取邮件抄送人列表。 * @return 邮件抄送人列表。 */ public List getCarbonCopy() { return carbonCopy; } /** * 设置邮件抄送人列表。 * @param carbonCopy 邮件抄送人列表。 */ public void setCarbonCopy(List carbonCopy) { this.carbonCopy = carbonCopy; } /** * 获取邮件收件人列表。 * @return 邮件收件人列表。 */ public List getSendTo() { return sendTo; } /** * 设置邮件收件人列表。 * @param sendTo 邮件收件人列表。 */ public void setSendTo(List sendTo) { this.sendTo = sendTo; } /** * 获取邮件内容。 * @return 邮件内容。 */ public String getContent() { return content; } /** * 设置邮件内容。 * @param content 邮件内容。 */ public void setContent(String content) { this.content = content; } /** * 获取邮件发件人。 * @return 邮件发件人。 */ public String getFrom() { return from; } /** * 设置邮件发件人。 * @param from 邮件发件人。 */ public void setFrom(String from) { this.from = from; } /** * 获取邮件发送日期。 * @return 邮件发送日期。 */ public Date getSendDate() { return sendDate; } /** * 设置邮件发送日期。 * @param sendDate 邮件发送日期。 */ public void setSendDate(Date sendDate) { this.sendDate = sendDate; } /** * 获取邮件主题。 * @return 邮件主题。 */ public String getSubject() { return subject; } /** * 设置邮件主题。 * @param subject 邮件主题。 */ public void setSubject(String subject) { this.subject = subject; } } (2) MailConfig.java 文件。 Java代码 复制代码 1. /** 2. * MailConfig 表示发送或接收邮件的配置信息。 3. * @author Dingli. 4. */ 5. public class MailConfig 6. { 7. private String protocol = null; 8. private String server = null; 9. private String user = null; 10. private String password = null; 11. private int port = 0; 12. private boolean debug; 13. 14. /** 15. * MailConfig 的默认构造方法。 16. */ 17. public MailConfig() 18. { 19. this.protocol = "smtp"; 20. this.port = 25; 21. this.debug = false; 22. } 23. 24. /** 25. * 获取是否显示邮件操作的调试信息。 26. * @return 是否显示邮件操作的调试信息。 27. */ 28. public boolean isDebug() 29. { 30. return debug; 31. } 32. 33. /** 34. * 设置是否显示邮件操作的调试信息。 35. * @param debug 是否显示邮件操作的调试信息。 36. */ 37. public void setDebug(boolean debug) 38. { 39. this.debug = debug; 40. } 41. 42. /** 43. * 获取登录密码。 44. * @return 登录密码。 45. */ 46. public String getPassword() 47. { 48. return password; 49. } 50. 51. /** 52. * 设置登录密码。 53. * @param password 登录密码。 54. */ 55. public void setPassword(String password) 56. { 57. this.password = password; 58. } 59. 60. /** 61. * 获取登录用户名。 62. * @return 登录用户名。 63. */ 64. public String getUser() 65. { 66. return user; 67. } 68. 69. /** 70. * 设置登录用户名。 71. * @param user 登录用户名。 72. */ 73. public void setUser(String user) 74. { 75. this.user = user; 76. } 77. 78. /** 79. * 获取邮件服务器端口。 80. * @return 邮件服务器端口。 81. */ 82. public int getPort() 83. { 84. return port; 85. } 86. 87. /** 88. * 设置邮件服务器端口。 89. * @param port 邮件服务器端口。 90. */ 91. public void setPort(int port) 92. { 93. this.port = port; 94. } 95. 96. /** 97. * 获取发送或接收邮件的协议。 98. * @return 发送或接收邮件的协议。 99. */ 100. public String getProtocol() 101. { 102. return protocol; 103. } 104. 105. /** 106. * 设置发送或接收邮件的协议。 107. * @param protocol 发送或接收邮件的协议。 108. */ 109. public void setProtocol(String protocol) 110. { 111. this.protocol = protocol; 112. } 113. 114. /** 115. * 获取邮件服务器。 116. * @return 邮件服务器。 117. */ 118. public String getServer() 119. { 120. return server; 121. } 122. 123. /** 124. * 设置邮件服务器。 125. * @param server 邮件服务器。 126. */ 127. public void setServer(String server) 128. { 129. this.server = server; 130. } 131. } /** * MailConfig 表示发送或接收邮件的配置信息。 * @author Dingli. */ public class MailConfig { private String protocol = null; private String server = null; private String user = null; private String password = null; private int port = 0; private boolean debug; /** * MailConfig 的默认构造方法。 */ public MailConfig() { this.protocol = "smtp"; this.port = 25; this.debug = false; } /** * 获取是否显示邮件操作的调试信息。 * @return 是否显示邮件操作的调试信息。 */ public boolean isDebug() { return debug; } /** * 设置是否显示邮件操作的调试信息。 * @param debug 是否显示邮件操作的调试信息。 */ public void setDebug(boolean debug) { this.debug = debug; } /** * 获取登录密码。 * @return 登录密码。 */ public String getPassword() { return password; } /** * 设置登录密码。 * @param password 登录密码。 */ public void setPassword(String password) { this.password = password; } /** * 获取登录用户名。 * @return 登录用户名。 */ public String getUser() { return user; } /** * 设置登录用户名。 * @param user 登录用户名。 */ public void setUser(String user) { this.user = user; } /** * 获取邮件服务器端口。 * @return 邮件服务器端口。 */ public int getPort() { return port; } /** * 设置邮件服务器端口。 * @param port 邮件服务器端口。 */ public void setPort(int port) { this.port = port; } /** * 获取发送或接收邮件的协议。 * @return 发送或接收邮件的协议。 */ public String getProtocol() { return protocol; } /** * 设置发送或接收邮件的协议。 * @param protocol 发送或接收邮件的协议。 */ public void setProtocol(String protocol) { this.protocol = protocol; } /** * 获取邮件服务器。 * @return 邮件服务器。 */ public String getServer() { return server; } /** * 设置邮件服务器。 * @param server 邮件服务器。 */ public void setServer(String server) { this.server = server; } } (3) MailSender.java 文件。 Java代码 复制代码 1. import java.util.List; 2. import java.util.Properties; 3. import javax.activation.DataHandler; 4. import javax.activation.FileDataSource; 5. import javax.mail.Message; 6. import javax.mail.Session; 7. import javax.mail.Transport; 8. import javax.mail.Message.RecipientType; 9. import javax.mail.internet.InternetAddress; 10. import javax.mail.internet.MimeBodyPart; 11. import javax.mail.internet.MimeMessage; 12. import javax.mail.internet.MimeMultipart; 13. import sun.misc.BASE64Encoder; 14. 15. /** 16. * 使用 MailSender 来发送邮件。 17. * @author Dingli. 18. */ 19. public class MailSender 20. { 21. //发送或接收邮件的配置信息。 22. private MailConfig mailConfig = null; 23. //待发送的邮件对象。 24. private Mail mail = null; 25. //邮件会话对象。 26. private Session session = null; 27. //邮件发送者对象。 28. private Transport transport = null; 29. //MIME 消息对象。 30. private MimeMessage mimeMessage = null; 31. //MimeMultipart 对象。 32. private MimeMultipart mimeMultipart = null; 33. 34. /** 35. * MailSender 类的默认构造方法。 36. */ 37. public MailSender() 38. { 39. this.mimeMultipart = new MimeMultipart(); 40. } 41. 42. /** 43. * MailSender 类的构造方法。 44. * @param mailConfig MailConfig 对象。 45. */ 46. public MailSender(MailConfig mailConfig) 47. { 48. this(); 49. this.setMailConfig(mailConfig); 50. } 51. 52. /** 53. * 设置发送或接收邮件的配置信息。 54. * @param mailConfig 发送或接收邮件的配置信息。 55. */ 56. public void setMailConfig(MailConfig mailConfig) 57. { 58. //设置邮件发送配置对象。 59. this.mailConfig = mailConfig; 60. //构建属性对象。 61. Properties properties = new Properties(); 62. //设置发送邮件的协议。 63. properties.put("mail.transport.protocol", this.getMailConfig().getProtocol()); 64. //设置是否需要身份验证。 65. properties.put("mail." + this.getMailConfig().getProtocol() + ".auth", "true"); 66. //是否显示调试信息。 67. properties.put("mail.debug", String.valueOf(this.getMailConfig().isDebug())); 68. //启动一个邮件会话。 69. this.session = Session.getInstance(properties); 70. } 71. 72. /** 73. * 获取发送或接收邮件的配置信息。 74. * @return 发送或接收邮件的配置信息。 75. */ 76. public MailConfig getMailConfig() 77. { 78. return mailConfig; 79. } 80. 81. /** 82. * 设置邮件对象。 83. * @param mail 邮件对象。 84. * @throws Exception Exception 异常。 85. */ 86. public void setMail(Mail mail) throws Exception 87. { 88. if(this.mailConfig != null) 89. { 90. //保存邮件对象。 91. this.mail = mail; 92. //构建 MIME 消息对象。 93. this.mimeMessage = new MimeMessage(this.session); 94. //设置发件人。 95. this.mimeMessage.setFrom(new InternetAddress(this.getMail().getFrom())); 96. //设置邮件主题。 97. this.mimeMessage.setSubject(this.getMail().getSubject()); 98. //设置邮件发送时间。 99. this.mimeMessage.setSentDate(this.getMail().getSendDate()); 100. //设置收件人。 101. this.setMailRecipients(this.mimeMessage, Message.RecipientType.TO, this.getMail().getSendTo()); 102. //设置抄送人。 103. this.setMailRecipients(this.mimeMessage, Message.RecipientType.CC, this.getMail().getCarbonCopy()); 104. //设置暗送人。 105. this.setMailRecipients(this.mimeMessage, Message.RecipientType.BCC, this.getMail().getBlindCarbonCopy()); 106. //判断邮件是否有附件。 107. if(this.getMail().getAttachment().isEmpty()) 108. { 109. //设置邮件文本内容。 110. this.mimeMessage.setText(this.getMail().getContent()); 111. } 112. else 113. { 114. //设置邮件内容。 115. this.setMailBody(this.getMail()); 116. //设置Multipart对象。 117. this.mimeMessage.setContent(this.mimeMultipart); 118. } 119. } 120. else 121. { 122. throw new Exception("You must set mail config first"); 123. } 124. } 125. 126. /** 127. * 获取邮件对象。 128. * @return 邮件对象。 129. */ 130. public Mail getMail() 131. { 132. return mail; 133. } 134. 135. /** 136. * 发送邮件。 137. * @param mail 邮件对象。 138. * @throws Exception Exception 异常。 139. */ 140. public void send(Mail mail) throws Exception 141. { 142. this.setMail(mail); 143. this.connect(); 144. this.send(); 145. } 146. 147. /** 148. * 连接到配置信息设置的邮件服务器。 149. * @throws Exception Exception 异常。 150. */ 151. public void connect() throws Exception 152. { 153. if(this.getMail() != null) 154. { 155. //构建发送者。 156. this.transport = this.session.getTransport(); 157. //连接到邮件服务器。 158. this.transport.connect(this.getMailConfig().getServer(), this.getMailConfig().getPort(), this.getMailConfig().getUser(), this.getMailConfig().getPassword()); 159. } 160. else 161. { 162. throw new Exception("No such mail to set"); 163. } 164. } 165. 166. /** 167. * 关闭与邮件服务器的连接。 168. * @throws Exception Exception 异常。 169. */ 170. public void close() throws Exception 171. { 172. //关闭邮件发送者对象。 173. if(this.transport != null && this.transport.isConnected()) 174. { 175. this.transport.close(); 176. } 177. //置空对象。 178. this.mailConfig = null; 179. this.mail = null; 180. this.session = null; 181. this.transport = null; 182. this.mimeMultipart = null; 183. this.mimeMessage = null; 184. } 185. 186. /** 187. * 发送邮件。 188. * @throws Exception Exception 异常。 189. */ 190. public void send() throws Exception 191. { 192. if(this.transport != null && this.transport.isConnected()) 193. { 194. //发送邮件给收件人。 195. if(this.mimeMessage.getRecipients(Message.RecipientType.TO) != null) 196. { 197. this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.TO)); 198. } 199. //发送邮件给抄送人。 200. if(this.mimeMessage.getRecipients(Message.RecipientType.CC) != null) 201. { 202. this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.CC)); 203. } 204. //发送邮件给暗送人。 205. if(this.mimeMessage.getRecipients(Message.RecipientType.BCC) != null) 206. { 207. this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.BCC)); 208. } 209. } 210. else 211. { 212. throw new Exception("No such connection"); 213. } 214. } 215. 216. /** 217. * 设置收信人。 218. * @param mimeMessage MIME 消息类型。 219. * @param recipientType 收信人类型。 220. * @param recipients 收信人列表。 221. * @throws Exception Exception 异常。 222. */ 223. private void setMailRecipients(MimeMessage mimeMessage, RecipientType recipientType, List recipients) throws Exception 224. { 225. //构建收信人地址数组。 226. InternetAddress[] internetAddresses = new InternetAddress[recipients.size()]; 227. //遍历所有收信人。 228. for(int i = 0; i < recipients.size(); i++) 229. { 230. //构建收信人地址对象。 231. internetAddresses[i] = new InternetAddress(recipients.get(i).toString()); 232. } 233. //设置收信人。 234. mimeMessage.setRecipients(recipientType, internetAddresses); 235. } 236. 237. /** 238. * 设置邮件体。包括邮件内容与附件。 239. * @param mail Mail 对象。 240. * @throws Exception Exception 异常。 241. */ 242. private void setMailBody(Mail mail) throws Exception 243. { 244. //移除 MimeMultipart 对象中的 MimeBodyPart 对象。 245. if(this.mimeMultipart != null) 246. { 247. //清空 MimeMultipart 对象。 248. for(int i = 0; i < this.mimeMultipart.getCount(); i++) 249. { 250. this.mimeMultipart.removeBodyPart(i); 251. } 252. } 253. //base64编码器。 254. BASE64Encoder base64Encoder = new BASE64Encoder(); 255. //构建邮件文本内容对象。 256. MimeBodyPart textBodyPart = new MimeBodyPart(); 257. //设置邮件文本内容。 258. textBodyPart.setText(mail.getContent()); 259. //添加邮件文本内容。 260. this.mimeMultipart.addBodyPart(textBodyPart); 261. //遍历所有附件本地路径。 262. for(int i = 0; i < mail.getAttachment().size(); i++) 263. { 264. //邮件附件本地路径。 265. String attachmentPath = mail.getAttachment().get(i).toString(); 266. //构建邮件附件对象。 267. MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 268. //用文件数据源对象构建一个数据句柄对象并设置到邮件附件对象。 269. attachmentBodyPart.setDataHandler(new DataHandler(new FileDataSource(attachmentPath))); 270. //设置邮件附件对象的名字。 271. attachmentBodyPart.setFileName("=?GBK?B?" + base64Encoder.encode(attachmentPath.substring(attachmentPath.replace('\\', '/').lastIndexOf('/') + 1).getBytes()) + "?="); 272. //添加邮件附件。 273. this.mimeMultipart.addBodyPart(attachmentBodyPart); 274. } 275. } 276. } import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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 sun.misc.BASE64Encoder; /** * 使用 MailSender 来发送邮件。 * @author Dingli. */ public class MailSender { //发送或接收邮件的配置信息。 private MailConfig mailConfig = null; //待发送的邮件对象。 private Mail mail = null; //邮件会话对象。 private Session session = null; //邮件发送者对象。 private Transport transport = null; //MIME 消息对象。 private MimeMessage mimeMessage = null; //MimeMultipart 对象。 private MimeMultipart mimeMultipart = null; /** * MailSender 类的默认构造方法。 */ public MailSender() { this.mimeMultipart = new MimeMultipart(); } /** * MailSender 类的构造方法。 * @param mailConfig MailConfig 对象。 */ public MailSender(MailConfig mailConfig) { this(); this.setMailConfig(mailConfig); } /** * 设置发送或接收邮件的配置信息。 * @param mailConfig 发送或接收邮件的配置信息。 */ public void setMailConfig(MailConfig mailConfig) { //设置邮件发送配置对象。 this.mailConfig = mailConfig; //构建属性对象。 Properties properties = new Properties(); //设置发送邮件的协议。 properties.put("mail.transport.protocol", this.getMailConfig().getProtocol()); //设置是否需要身份验证。 properties.put("mail." + this.getMailConfig().getProtocol() + ".auth", "true"); //是否显示调试信息。 properties.put("mail.debug", String.valueOf(this.getMailConfig().isDebug())); //启动一个邮件会话。 this.session = Session.getInstance(properties); } /** * 获取发送或接收邮件的配置信息。 * @return 发送或接收邮件的配置信息。 */ public MailConfig getMailConfig() { return mailConfig; } /** * 设置邮件对象。 * @param mail 邮件对象。 * @throws Exception Exception 异常。 */ public void setMail(Mail mail) throws Exception { if(this.mailConfig != null) { //保存邮件对象。 this.mail = mail; //构建 MIME 消息对象。 this.mimeMessage = new MimeMessage(this.session); //设置发件人。 this.mimeMessage.setFrom(new InternetAddress(this.getMail().getFrom())); //设置邮件主题。 this.mimeMessage.setSubject(this.getMail().getSubject()); //设置邮件发送时间。 this.mimeMessage.setSentDate(this.getMail().getSendDate()); //设置收件人。 this.setMailRecipients(this.mimeMessage, Message.RecipientType.TO, this.getMail().getSendTo()); //设置抄送人。 this.setMailRecipients(this.mimeMessage, Message.RecipientType.CC, this.getMail().getCarbonCopy()); //设置暗送人。 this.setMailRecipients(this.mimeMessage, Message.RecipientType.BCC, this.getMail().getBlindCarbonCopy()); //判断邮件是否有附件。 if(this.getMail().getAttachment().isEmpty()) { //设置邮件文本内容。 this.mimeMessage.setText(this.getMail().getContent()); } else { //设置邮件内容。 this.setMailBody(this.getMail()); //设置Multipart对象。 this.mimeMessage.setContent(this.mimeMultipart); } } else { throw new Exception("You must set mail config first"); } } /** * 获取邮件对象。 * @return 邮件对象。 */ public Mail getMail() { return mail; } /** * 发送邮件。 * @param mail 邮件对象。 * @throws Exception Exception 异常。 */ public void send(Mail mail) throws Exception { this.setMail(mail); this.connect(); this.send(); } /** * 连接到配置信息设置的邮件服务器。 * @throws Exception Exception 异常。 */ public void connect() throws Exception { if(this.getMail() != null) { //构建发送者。 this.transport = this.session.getTransport(); //连接到邮件服务器。 this.transport.connect(this.getMailConfig().getServer(), this.getMailConfig().getPort(), this.getMailConfig().getUser(), this.getMailConfig().getPassword()); } else { throw new Exception("No such mail to set"); } } /** * 关闭与邮件服务器的连接。 * @throws Exception Exception 异常。 */ public void close() throws Exception { //关闭邮件发送者对象。 if(this.transport != null && this.transport.isConnected()) { this.transport.close(); } //置空对象。 this.mailConfig = null; this.mail = null; this.session = null; this.transport = null; this.mimeMultipart = null; this.mimeMessage = null; } /** * 发送邮件。 * @throws Exception Exception 异常。 */ public void send() throws Exception { if(this.transport != null && this.transport.isConnected()) { //发送邮件给收件人。 if(this.mimeMessage.getRecipients(Message.RecipientType.TO) != null) { this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.TO)); } //发送邮件给抄送人。 if(this.mimeMessage.getRecipients(Message.RecipientType.CC) != null) { this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.CC)); } //发送邮件给暗送人。 if(this.mimeMessage.getRecipients(Message.RecipientType.BCC) != null) { this.transport.sendMessage(this.mimeMessage, this.mimeMessage.getRecipients(Message.RecipientType.BCC)); } } else { throw new Exception("No such connection"); } } /** * 设置收信人。 * @param mimeMessage MIME 消息类型。 * @param recipientType 收信人类型。 * @param recipients 收信人列表。 * @throws Exception Exception 异常。 */ private void setMailRecipients(MimeMessage mimeMessage, RecipientType recipientType, List recipients) throws Exception { //构建收信人地址数组。 InternetAddress[] internetAddresses = new InternetAddress[recipients.size()]; //遍历所有收信人。 for(int i = 0; i < recipients.size(); i++) { //构建收信人地址对象。 internetAddresses[i] = new InternetAddress(recipients.get(i).toString()); } //设置收信人。 mimeMessage.setRecipients(recipientType, internetAddresses); } /** * 设置邮件体。包括邮件内容与附件。 * @param mail Mail 对象。 * @throws Exception Exception 异常。 */ private void setMailBody(Mail mail) throws Exception { //移除 MimeMultipart 对象中的 MimeBodyPart 对象。 if(this.mimeMultipart != null) { //清空 MimeMultipart 对象。 for(int i = 0; i < this.mimeMultipart.getCount(); i++) { this.mimeMultipart.removeBodyPart(i); } } //base64编码器。 BASE64Encoder base64Encoder = new BASE64Encoder(); //构建邮件文本内容对象。 MimeBodyPart textBodyPart = new MimeBodyPart(); //设置邮件文本内容。 textBodyPart.setText(mail.getContent()); //添加邮件文本内容。 this.mimeMultipart.addBodyPart(textBodyPart); //遍历所有附件本地路径。 for(int i = 0; i < mail.getAttachment().size(); i++) { //邮件附件本地路径。 String attachmentPath = mail.getAttachment().get(i).toString(); //构建邮件附件对象。 MimeBodyPart attachmentBodyPart = new MimeBodyPart(); //用文件数据源对象构建一个数据句柄对象并设置到邮件附件对象。 attachmentBodyPart.setDataHandler(new DataHandler(new FileDataSource(attachmentPath))); //设置邮件附件对象的名字。 attachmentBodyPart.setFileName("=?GBK?B?" + base64Encoder.encode(attachmentPath.substring(attachmentPath.replace('\\', '/').lastIndexOf('/') + 1).getBytes()) + "?="); //添加邮件附件。 this.mimeMultipart.addBodyPart(attachmentBodyPart); } } } 用一个 main() 方法来简单表明如何使用。 Java代码 复制代码 1. public class MainClass 2. { 3. public static void main(String[] args) 4. { 5. //邮件发送者。 6. MailSender mailSender = new MailSender(); 7. try 8. { 9. //SMTP 配置。 10. MailConfig mailConfig = new MailConfig(); 11. //设置登录的邮件服务器。 12. mailConfig.setServer("smtp.126.com"); 13. //设置登录用户名。 14. mailConfig.setUser("dingli717"); 15. //设置登录密码。 16. mailConfig.setPassword("******"); 17. //一封电子邮件. 18. Mail mail = new Mail(); 19. //发件人。 20. mail.setFrom("dingli717@126.com"); 21. //收件人,可以添加多个。 22. mail.getSendTo().add("dingli717@126.com"); 23. //邮件主题。 24. mail.setSubject("Java 你好"); 25. //邮件内容。 26. mail.setContent("邮件发送测试"); 27. //本地附件路径,可以添加多个附件。 28. mail.setAttachment("E:/testExcel.xls"); 29. //设置邮件配置。 30. mailSender.setMailConfig(mailConfig); 31. //设置要发送的邮件。 32. mailSender.setMail(mail); 33. //连接邮件服务器。 34. mailSender.connect(); 35. //发送邮件。 36. mailSender.send(); 37. //邮件成功发送。 38. System.out.println("邮件发送成功!"); 39. } 40. catch(Exception ex) 41. { 42. ex.printStackTrace(); 43. } 44. finally 45. { 46. try 47. { 48. mailSender.close(); 49. } 50. catch(Exception ex) 51. { 52. ex.printStackTrace(); 53. } 54. } 55. } 56. } public class MainClass { public static void main(String[] args) { //邮件发送者。 MailSender mailSender = new MailSender(); try { //SMTP 配置。 MailConfig mailConfig = new MailConfig(); //设置登录的邮件服务器。 mailConfig.setServer("smtp.126.com"); //设置登录用户名。 mailConfig.setUser("dingli717"); //设置登录密码。 mailConfig.setPassword("******"); //一封电子邮件. Mail mail = new Mail(); //发件人。 mail.setFrom("dingli717@126.com"); //收件人,可以添加多个。 mail.getSendTo().add("dingli717@126.com"); //邮件主题。 mail.setSubject("Java 你好"); //邮件内容。 mail.setContent("邮件发送测试"); //本地附件路径,可以添加多个附件。 mail.setAttachment("E:/testExcel.xls"); //设置邮件配置。 mailSender.setMailConfig(mailConfig); //设置要发送的邮件。 mailSender.setMail(mail); //连接邮件服务器。 mailSender.connect(); //发送邮件。 mailSender.send(); //邮件成功发送。 System.out.println("邮件发送成功!"); } catch(Exception ex) { ex.printStackTrace(); } finally { try { mailSender.close(); } catch(Exception ex) { ex.printStackTrace(); } } } } Mail 类表示一封电子邮件,MailConfig 类表示发送或接收电子邮件的配置信息,而 MailSender 类用来处理邮件的具体发送。
转载请注明原文地址: https://www.6miu.com/read-4940900.html

最新回复(0)