Java中邮件发送session.getDefaultInstance和getInstance的区别

假设你想要同时用两个邮箱分别给再给两个邮箱发送邮件时,你就需要创建两个java.mail.Session对象,这时候你用getDefaultInstance的话会发现第二个Session对象和第一个对象永远都是一样的。

这是应为getDefaultInstance才是真正的单例模式,而且你会发现里面的username、password属性都是final型的,是无法进行更改的。所以你会发现封装的两个E-mail都是由第一个创建的Session对象的邮件发出来的,这时你就要用到javax.mail.Sessin.getInstance()方法创建Session对象就不会出现以上问题。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用于Java邮件发送的一个类方法 Java邮件发送类 这是第一部分第一个类:MailSenderInfo.java 1. package com.util.mail; 2. /** 3. * 发送邮件需要使用的基本信息 4. */ 5. import java.util.Properties; 6. public class MailSenderInfo { 7. // 发送邮件的服务器的IP和端口 8. private String mailServerHost; 9. private String mailServerPort = "25"; 10. // 邮件发送者的地址 11. private String fromAddress; 12. // 邮件接收者的地址 13. private String toAddress; 14. // 登陆邮件发送服务器的用户名和密码 15. private String userName; 16. private String password; 17. // 是否需要身份验证 18. private boolean validate = false; 19. // 邮件主题 20. private String subject; 21. // 邮件的文本内容 22. private String content; 23. // 邮件附件的文件名 24. private String[] attachFileNames; 25. /** 26. * 获得邮件会话属性 27. */ 28. public Properties getProperties(){ 29. Properties p = new Properties(); 30. p.put("mail.smtp.host", this.mailServerHost); 31. p.put("mail.smtp.port", this.mailServerPort); 32. p.put("mail.smtp.auth", validate ? "true" : "false"); 33. return p; 34. } 35. public String getMailServerHost() { 36. return mailServerHost; 37. } 38. public void setMailServerHost(String mailServerHost) { 39. this.mailServerHost = mailServerHost; 40. } 41. public String getMailServerPort() { 42. return mailServerPort; 43. } 44. public void setMailServerPort(String mailServerPort) { 45. this.mailServerPort = mailServerPort; 46. } 47. public boolean isValidate() { 48. return validate; 49. } 50. public void setValidate(boolean validate) { 51. this.validate = validate; 52. } 53. public String[] getAttachFileNames() { 54. return attachFileNames; 55. } 56. public void setAttachFileNames(String[] fileNames) { 57. this.attachFileNames = fileNames; 58. } 59. public String getFromAddress() { 60. return fromAddress; 61. } 62. public void setFromAddress(String fromAddress) { 63. this.fromAddress = fromAddress; 64. } 65. public String getPassword() { 66. return password; 67. } 68. public void setPassword(String password) { 69. this.password = password; 70. } 71. public String getToAddress() { 72. return toAddress; 73. } 74. public void setToAddress(String toAddress) { 75. this.toAddress = toAddress; 76. } 77. public String getUserName() { 78. return userName; 79. } 80. public void setUserName(String userName) { 81. this.userName = userName; 82. } 83. public String getSubject() { 84. return subject; 85. } 86. public void setSubject(String subject) { 87. this.subject = subject; 88. } 89. public String getContent() { 90. return content; 91. } 92. public void setContent(String textContent) { 93. this.content = textContent; 94. } 95. } 第二部分第二个类:SimpleMailSender.java Java代码 1. package com.util.mail; 2. 3. import java.util.Date; 4. import java.util.Properties; 5. import javax.mail.Address; 6. import javax.mail.BodyPart; 7. import javax.mail.Message; 8. import javax.mail.MessagingException; 9. import javax.mail.Multipart; 10. import javax.mail.Session; 11. import javax.mail.Transport; 12. import javax.mail.internet.InternetAddress; 13. import javax.mail.internet.MimeBodyPart; 14. import javax.mail.internet.MimeMessage; 15. import javax.mail.internet.MimeMultipart; 16. 17. /** 18. * 简单邮件(不带附件的邮件)发送器 19. */ 20. public class SimpleMailSender { 21. /** 22. * 以文本格式发送邮件 23. * @param mailInfo 待发送的邮件的信息 24. */ 25. public boolean sendTextMail(MailSenderInfo mailInfo) { 26. // 判断是否需要身份认证 27. MyAuthenticator authenticator = null; 28. Properties pro = mailInfo.getProperties(); 29. if (mailInfo.isValidate()) { 30. // 如果需要身份认证,则创建一个密码验证器 31. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 32. } 33. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 34. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 35. try { 36. // 根据session创建一个邮件消息 37. Message mailMessage = new MimeMessage(sendMailSession); 38. // 创建邮件发送者地址 39. Address from = new InternetAddress(mailInfo.getFromAddress()); 40. // 设置邮件消息的发送者 41. mailMessage.setFrom(from); 42. // 创建邮件的接收者地址,并设置到邮件消息 43. Address to = new InternetAddress(mailInfo.getToAddress()); 44. mailMessage.setRecipient(Message.RecipientType.TO,to); 45. // 设置邮件消息的主题 46. mailMessage.setSubject(mailInfo.getSubject()); 47. // 设置邮件消息发送的时间 48. mailMessage.setSentDate(new Date()); 49. // 设置邮件消息的主要内容 50. String mailContent = mailInfo.getContent(); 51. mailMessage.setText(mailContent); 52. // 发送邮件 53. Transport.send(mailMessage); 54. return true; 55. } catch (MessagingException ex) { 56. ex.printStackTrace(); 57. } 58. return false; 59. } 60. 61. /** 62. * 以HTML格式发送邮件 63. * @param mailInfo 待发送的邮件信息 64. */ 65. public static boolean sendHtmlMail(MailSenderInfo mailInfo){ 66. // 判断是否需要身份认证 67. MyAuthenticator authenticator = null; 68. Properties pro = mailInfo.getProperties(); 69. //如果需要身份认证,则创建一个密码验证器 70. if (mailInfo.isValidate()) { 71. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 72. } 73. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 74. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 75. try { 76. // 根据session创建一个邮件消息 77. Message mailMessage = new MimeMessage(sendMailSession); 78. // 创建邮件发送者地址 79. Address from = new InternetAddress(mailInfo.getFromAddress()); 80. // 设置邮件消息的发送者 81. mailMessage.setFrom(from); 82. // 创建邮件的接收者地址,并设置到邮件消息 83. Address to = new InternetAddress(mailInfo.getToAddress()); 84. // Message.RecipientType.TO属性表示接收者的类型为TO 85. mailMessage.setRecipient(Message.RecipientType.TO,to); 86. // 设置邮件消息的主题 87. mailMessage.setSubject(mailInfo.getSubject()); 88. // 设置邮件消息发送的时间 89. mailMessage.setSentDate(new Date()); 90. // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 91. Multipart mainPart = new MimeMultipart(); 92. // 创建一个包含HTML内容的MimeBodyPart 93. BodyPart html = new MimeBodyPart(); 94. // 设置HTML内容 95. html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 96. mainPart.addBodyPart(html); 97. // 将MiniMultipart对象设置为邮件内容 98. mailMessage.setContent(mainPart); 99. // 发送邮件 100. Transport.send(mailMessage); 101. return true; 102. } catch (MessagingException ex) { 103. ex.printStackTrace(); 104. } 105. return false; 106. } 107. } 第三个类:MyAuthenticator.java Java代码 1. package com.util.mail; 2. 3. import javax.mail.*; 4. 5. public class MyAuthenticator extends Authenticator{ 6. String userName=null; 7. String password=null; 8. 9. public MyAuthenticator(){ 10. } 11. public MyAuthenticator(String username, String password) { 12. this.userName = username; 13. this.password = password; 14. } 15. protected PasswordAuthentication getPasswordAuthentication(){ 16. return new PasswordAuthentication(userName, password); 17. } 18. } 19. 下面给出使用上面三个类的代码: Java代码 1. public static void main(String[] args){ 2. //这个类主要是设置邮件 3. MailSenderInfo mailInfo = new MailSenderInfo(); 4. mailInfo.setMailServerHost("smtp.163.com"); 5. mailInfo.setMailServerPort("25"); 6. mailInfo.setValidate(true); 7. mailInfo.setUserName("[email protected]"); 8. mailInfo.setPassword("**********");//您的邮箱密码 9. mailInfo.setFromAddress("[email protected]"); 10. mailInfo.setToAddress("[email protected]"); 11. mailInfo.setSubject("设置邮箱标题"); 12. mailInfo.setContent("设置邮箱内容"); 13. //这个类主要来发送邮件 14. SimpleMailSender sms = new SimpleMailSender(); 15. sms.sendTextMail(mailInfo);//发送文体格式 16. sms.sendHtmlMail(mailInfo);//发送html格式 17. }
要获取邮件并将其保存为.msg格式,您可以使用JavaMail API。以下是一个简单的示例代码,可以帮助您开始: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.MimeMessage; public class SaveAsMSG { public static void main(String[] args) throws MessagingException, IOException { // 配置邮件会话 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getInstance(props); // 连接到邮箱 Store store = session.getStore(); store.connect("imap.gmail.com", "your_username", "your_password"); // 打开收件箱 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); // 获取最新的邮件 Message[] messages = inbox.getMessages(inbox.getMessageCount(), inbox.getMessageCount()); Message message = messages[0]; // 将邮件保存为 .msg 文件 MimeMessage mimeMessage = (MimeMessage) message; OutputStream outputStream = new FileOutputStream("email.msg"); mimeMessage.writeTo(outputStream); outputStream.close(); // 标记邮件为已读 message.setFlag(Flags.Flag.SEEN, true); // 关闭收件箱 inbox.close(false); } } ``` 此代码将连接到您的Gmail帐户并获取收件箱的最新邮件。然后,它将将该邮件保存为名为"email.msg"的文件。请注意,此代码仅保存单个邮件,您可以更改邮件获取的逻辑来获取多个邮件并将它们保存到单个文件。 要使用JavaMail API读取.msg文件,请使用以下代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.MimeMessage; public class ReadMSG { public static void main(String[] args) throws MessagingException, IOException { // 配置邮件会话 Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); // 读取 .msg 文件 FileInputStream fileInputStream = new FileInputStream("email.msg"); MimeMessage mimeMessage = new MimeMessage(session, fileInputStream); fileInputStream.close(); // 打印邮件主题和内容 System.out.println("Subject: " + mimeMessage.getSubject()); System.out.println("Content: " + mimeMessage.getContent()); } } ``` 此代码将打开名为"email.msg"的文件并读取其内容。然后,它将打印邮件的主题和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值