JavaMail发送邮件(含附件)的例子

本文介绍如何使用 JavaMail API 发送包含 HTML 格式的正文及附件的电子邮件。通过设置邮件服务器属性、创建消息体并附加文件,最终实现邮件的发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:http://blog.sina.com.cn/s/blog_3f0cd39a010006pa.html

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.mogoko.common.email;  
  2.   
  3. import javax.mail.Session;  
  4. import javax.mail.MessagingException;  
  5. import javax.mail.Multipart;  
  6. import javax.mail.Transport;  
  7. import javax.mail.internet.InternetAddress;  
  8. import javax.mail.internet.MimeMessage;  
  9. import javax.mail.internet.MimeBodyPart;  
  10. import javax.mail.internet.MimeMultipart;  
  11. import javax.activation.FileDataSource;  
  12. import javax.activation.DataHandler;  
  13.   
  14.   
  15.   
  16.   
  17. public class SendAttachMail {  
  18. public static void sendMessage(String smtpHost,String from, String to,String subject, String messageText,String fileName)  
  19.     throws MessagingException {  
  20.   
  21. // Step 1: Configure the mail session  
  22. java.util.Properties props = new java.util.Properties();  
  23. props.setProperty("mail.smtp.auth""true"); //指定是否需要SMTP验证  
  24. props.setProperty("mail.smtp.host", smtpHost); //指定SMTP服务器  
  25. props.put("mail.transport.protocol""smtp"); //指定传输协议  
  26.   
  27.   
  28. Session mailSession = Session.getDefaultInstance(props);  
  29. mailSession.setDebug(false); //是否在控制台显示debug信息  
  30.   
  31. // Step 2: Construct the message  
  32. System.out.println("Constructing message - from=" + from + " to=" +to);  
  33.   
  34. InternetAddress fromAddress = new InternetAddress(from); //From Mail  
  35. InternetAddress toAddress = new InternetAddress(to); //To Mail  
  36.   
  37. MimeMessage mimeMessage = new MimeMessage(mailSession);  
  38. mimeMessage.setFrom(fromAddress);  
  39. mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);  
  40.   
  41.   
  42. mimeMessage.setSentDate(new java.util.Date());  
  43. mimeMessage.setSubject(subject);  
  44.   
  45. // Step 3: Create a body part to hold the "text" portion of the message  
  46. System.out.println("Constructing 'text' body part");  
  47.   
  48. MimeBodyPart textBodyPart = new MimeBodyPart();  
  49. textBodyPart.setContent(messageText, "text/html;charset=gb2312");  
  50.   
  51. // Step 4: Create a body part to hold the "file" portion of the message  
  52. System.out.println("Attaching 'file' body part: " + fileName);  
  53.   
  54. MimeBodyPart fileBodyPart = new MimeBodyPart();  
  55. FileDataSource fileDataSource = new FileDataSource("E:\\a.zip");  
  56. fileBodyPart.setDataHandler(new DataHandler(fileDataSource));  
  57. fileBodyPart.setFileName(fileDataSource.getName());  
  58. //添加附件  
  59. System.out.println("Finished attaching file");  
  60.   
  61. // Step 5: Create a Multipart/container and add the parts  
  62. Multipart container = new MimeMultipart();  
  63. container.addBodyPart(textBodyPart);  
  64. container.addBodyPart(fileBodyPart);  
  65.   
  66. // Step 6: Add the Multipart to the actual message  
  67. mimeMessage.setContent(container);  
  68.   
  69. System.out.println("Message constructed");  
  70.   
  71. // Step 7: Now send the message  
  72. Transport transport = mailSession.getTransport("smtp");  
  73. transport.connect(smtpHost, "biansutao""password");  
  74. transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());  
  75. transport.close();  
  76.   
  77. System.out.println("Message sent!");  
  78. }  
  79. /* 
  80.  * 测试发送邮件 
  81.  */  
  82.   
  83. //+++++++++++++++++++++++++++++++++++++++++++++++  
  84. public static void main(String[] args) {  
  85.   
  86. String fileName = "b.zip";  
  87. String smtpHost = "smtp.163.com";  
  88. String from = "biansutao@163.com"//必须与transport.connect(smtpHost, "username1", "pwd1");的username1一样  
  89. String to = "biansutao@163.com";  
  90. String subject = "邮件测试从mogoko"//subject javamail自动转码  
  91. StringBuffer theMessage = new StringBuffer();  
  92. theMessage.append("邮件测试");  
  93.   
  94. try {  
  95. SendAttachMail.sendMessage(smtpHost, from, to, subject,  
  96. theMessage.toString(), fileName);  
  97. catch (javax.mail.MessagingException exc) {  
  98. exc.p  

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值