- 2023-04-12 18:29:28
- 4779 热度
- 0 评论
一般是这样的,我们可能不会要求马上发送这封邮件,为了减少服务器压力和带宽压力,我们一般是在空闲时在发送,或者用户指定时间才发送
那么就要先把邮件进行保存,然后按时发送
可以将信息保存到数据库,然后获取后再组装邮件信息,这里我们就把邮件打成一个文件保存在本地
保存到本地很简单,只要之前你的示例都能跑通,只差一步
package com.mail; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; /** * @说明 保存一个邮件 * @author cuisuqiang * @version 1.0 * @since */ public class TextMail { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put( "mail.smtp.host ", "smtp.163.com "); props.put("mail.smtp.port", 25); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props); session.setDebug(true); Message message = new MimeMessage(session); InternetAddress from = new InternetAddress("test20120711120200@163.com"); from.setPersonal(MimeUtility.encodeText("java無名<test20120711120200@163.com>")); message.setFrom(from); InternetAddress to = new InternetAddress("test20120711120200@163.com"); message.setRecipient(Message.RecipientType.TO, to); message.setSubject(MimeUtility.encodeText("强哥邀请,谁敢不从!")); message.setText("强哥邀请你访问我的博客:http://cuisuqiang.iteye.com/"); message.setSentDate(new Date()); // 邮件对象 File file = new File("C:\\textmail.eml"); // 获得输出流 OutputStream ips = new FileOutputStream(file); // 把邮件内容写入到文件 message.writeTo(ips); // 关闭流 ips.close(); System.out.println("发送完毕"); } }
邮件对象创建后没有立即发送,而是保存到了一个文件中
那么如何发送一封已经存在的邮件呢?也很简单,只是邮件对象的创建的方式不一样了而已
package com.mail; import java.io.File; import java.io.FileInputStream; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage; /** * 放松一封现有邮件 * @author cuisuqiang@163.com */ public class SendCurrentMail { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props); // 现有邮件文件 File file = new File("C:\\textmail.eml"); FileInputStream fis = new FileInputStream(file); // 创建邮件对象 Message message = new MimeMessage(session, fis); message.setSentDate(new Date()); message.saveChanges(); // 发送邮件 Transport transport = session.getTransport("smtp"); transport.connect("smtp.163.com", 25, "test20120711120200", "test123456"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); fis.close(); System.out.println("发送完毕"); } }
当然,具体业务中的应用肯定要增加许多判断的,特别是设计到了文件的操作,我这里就不再一一的解说了!
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)