使用Java发送各种格式的邮件

转自:https://blog.csdn.net/u011031689/article/details/51326595


          测试可用:

         有些重复代码没有给注释。类的方法作用自行查看API了解,最后附上源码。


        首先使用JavaMail的jar,官网可下载。

       公共静态的常量:

       

 
  1. public final static String MAIL = "@sina.com"; // 邮箱格式

  2. public final static String SEND_HOST = "smtp.sina.com"; // 邮箱发送服务器

  3. public final static String ACCEPT_HOST = "pop.sina.com"; // 邮箱服务器

  4. public final static String SEND_USER = "xxxx"; // 用户名

  5. public final static String SEND_PWD = "xxxxx"; // 密码

  6. public final static String FROM_MAIL = SEND_USER + "@sina.com";// 发送方邮箱地址

  7. public final static String TO_MAIL = "xxxx@sina.com"; // 接收方邮箱地址

  8. public final static String CC_MAIL = SEND_USER + MAIL; // 抄送方邮箱地址

  9. public final static String BCC_MAIl = SEND_USER + MAIL; // 密送方邮箱地址

  10.  
  11. public final static String ENCODE = "UTF-8";

  12. public static Date date = new Date();

    

        使用java自带的接口和类发送文本格式邮件

   

 
  1. Properties prop = new Properties();

  2. prop.setProperty("mail.host", SEND_HOST);

  3. prop.setProperty("mail.transport.protocol", "smtp");

  4. prop.setProperty("mail.smtps.ssl.enable", "true");

  5. // prop.setProperty("mail.smtp.port", "25");

  6. prop.setProperty("mail.smtp.auth", "true");

  7.  
  8. Session session = Session.getInstance(prop); //创建应用会话

  9.  
  10. Message message = new MimeMessage(session); //消息摘要,是邮件的主体

  11. message.setSubject("测试"); //设置主题

  12. message.setText("你好!"); //邮件内容

  13. message.setSentDate(new Date()); //发送日期

  14. message.setFrom(new InternetAddress(FROM_MAIL)); //发送方

  15. message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_MAIL)); //接受方

  16. message.saveChanges(); //保存邮件主体对象内容

  17.  
  18. Transport transport = session.getTransport(); //传输对象

  19. transport.connect(SEND_HOST, FROM_MAIL, SEND_PWD); //连接服务器中的邮箱

  20. transport.sendMessage(message, message.getAllRecipients()); //发送

  21. transport.close(); //关闭传输

  22. System.out.println("Successfully send mail to all user");

 

感觉自带的接口方法比较麻烦,使用spring封装的javamail,记的导入spring相关包。

 

使用spring发送文本格式的邮件,代码如下:

 

 
  1. public static void sendTxtMail() throws MailException {

  2. JavaMailSenderImpl send = new JavaMailSenderImpl();

  3. Properties prop = new Properties();

  4. prop.setProperty("mail.transport.protocol", "smtp");

  5. prop.setProperty("mail.host", SEND_HOST);

  6.  
  7. prop.setProperty("mail.smtps.ssl.enable", "true");

  8. prop.setProperty("mail.smtp.auth", "true");

  9.  
  10.  
  11. send.setUsername(SEND_USER);

  12. send.setPassword(SEND_PWD);

  13. send.setJavaMailProperties(prop);

  14.  
  15. SimpleMailMessage msg = new SimpleMailMessage();

  16. msg.setFrom(FROM_MAIL);

  17. msg.setTo(TO_MAIL);

  18. msg.setReplyTo(FROM_MAIL);

  19. msg.setCc(CC_MAIL);

  20. msg.setBcc(BCC_MAIl);

  21. msg.setSentDate(date);

  22. msg.setSubject("发送的文本格式邮件");

  23. msg.setText("文本格式 测试成功!");

  24. send.send(msg);

  25. System.out.println("Successfully send mail to the user");


使用spring的封装方法发送Html格式邮件 ,代码如下:

 

 
  1. // 发送Html格式邮件

  2. public static void sendHtmlMail() throws Exception {

  3. JavaMailSenderImpl send = new JavaMailSenderImpl();

  4. Properties prop = new Properties();

  5. prop.setProperty("mail.transport.protocol", "smtp");

  6. prop.setProperty("mail.host", SEND_HOST);

  7. prop.setProperty("mail.smtps.ssl.enable", "true");

  8. prop.setProperty("mail.smtp.auth", "true");

  9.  
  10. send.setUsername(SEND_USER);

  11. send.setPassword(SEND_PWD);

  12. send.setJavaMailProperties(prop);

  13.  
  14. MimeMessage msg = send.createMimeMessage();

  15.  
  16. MimeMessageHelper helper = new MimeMessageHelper(msg, ENCODE);

  17. helper.setFrom(FROM_MAIL);

  18. helper.setTo(TO_MAIL);

  19. helper.setReplyTo(FROM_MAIL);

  20. helper.setCc(CC_MAIL);

  21. helper.setBcc(BCC_MAIl);

  22. helper.setSentDate(date);

  23. helper.setSubject("发送的HTML格式邮件");

  24. String html = "<font size='5' color='red'>HTML格式测试成功!</font>";

  25. helper.setText(html, true); // 邮件内容,参数true表示是html代码

  26. send.send(msg);

  27. System.out.println("Successfully send mail to the user");

 

使用spring的封装方法发送带内嵌内容的Html格式邮件 ,代码如下:

 

 

 
  1. // 发送带内嵌文件的HTML格式邮件

  2. public static void sendInlineMail() throws Exception {

  3. // spring提供的邮件实现类

  4. JavaMailSenderImpl send = new JavaMailSenderImpl();

  5. Properties prop = new Properties();

  6. prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议

  7. prop.setProperty("mail.host", SEND_HOST); // 邮件服务器地址

  8. prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证

  9. prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证

  10.  
  11. send.setUsername(SEND_USER); // 设置用户名

  12. send.setPassword(SEND_PWD); // 设置密码

  13. send.setJavaMailProperties(prop);

  14.  
  15. MimeMessage msg = send.createMimeMessage();

  16. // 指定HTML编码,参数true表示为multipart

  17. MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);

  18. helper.setFrom(FROM_MAIL); // 发送者邮箱

  19. helper.setTo(TO_MAIL); // 接收者邮箱

  20. helper.setReplyTo(FROM_MAIL); // 回复邮箱

  21. helper.setCc(CC_MAIL); // 抄送邮箱

  22. helper.setBcc(BCC_MAIl); // 密送邮箱

  23. helper.setSentDate(date); // 发送日期

  24. helper.setSubject("发送的带有内部文件的HTML格式邮件");

  25. String html = "<font size='5' color='red'>HTML格式测试成功!</font>"

  26. + "<img src ='cid:demoimg'>"; // cid是一个固定前缀,demoimg是一个资源名称

  27. helper.setText(html, true); // 邮件内容,参数true表示是html代码

  28. ClassPathResource resource = new ClassPathResource("col.jpg"); // 加载项目路径下资源

  29. helper.addInline("demoimg", resource); // 将资源绑定到demoimg上

  30. send.send(msg); // 发送邮件

  31. System.out.println("Successfully send mail to the user");

  32. }


使用spring的封装方法发送带附件的邮件 ,代码如下:

 

 

 
  1. // 发送带附件的邮件

  2. public static void sendAttachmentMail() throws Exception {

  3. // spring提供的邮件实现类

  4. JavaMailSenderImpl send = new JavaMailSenderImpl();

  5. Properties prop = new Properties();

  6. prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议

  7. prop.setProperty("mail.host", SEND_HOST); // 邮件服务器地址

  8. prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证

  9. prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证

  10.  
  11. send.setUsername(SEND_USER); // 设置用户名

  12. send.setPassword(SEND_PWD); // 设置密码

  13. send.setJavaMailProperties(prop);

  14.  
  15. MimeMessage msg = send.createMimeMessage();

  16. // 指定HTML编码,参数true表示为multipart

  17. MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);

  18. helper.setFrom(FROM_MAIL); // 发送者邮箱

  19. helper.setTo(TO_MAIL); // 接收者邮箱

  20. helper.setReplyTo(FROM_MAIL); // 回复邮箱

  21. helper.setCc(CC_MAIL); // 抄送邮箱

  22. helper.setBcc(BCC_MAIl); // 密送邮箱

  23. helper.setSentDate(date); // 发送日期

  24. helper.setSubject("发送带有附件的邮件");

  25. String html = "<font size='5' color='red'>附件测试成功!</font>";

  26. helper.setText(html, true); // 邮件内容,参数true表示是html代码

  27. String demo = "demo.docx";

  28. ClassPathResource resource = new ClassPathResource(demo); // 加载项目路径下资源

  29. helper.addAttachment(MimeUtility.encodeWord(demo), resource); // 如果文件是中文名,需要转码。

  30. send.send(msg); // 发送邮件

  31. System.out.println("Successfully send mail to the user");

  32. }

 

  

   在测试之前记得将邮箱smtp、pop设置上,否者会报验证错误或连接服务错误 即50X系列错误。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java发送邮件可以使用JavaMail API。下是一个简单的示例代码,演示如何使用JavaMail API发送邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 邮件发送者的邮箱地址和密码 String senderEmail = "[email protected]"; String senderPassword = "your_password"; // 邮件接收者的邮箱地址 String recipientEmail = "[email protected]"; // 邮件服务器的主机名和端口号 String host = "smtp.example.com"; int port = 587; // 创建Properties对象,设置邮件服务器的配置信息 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); // 创建Session对象,用于与邮件服务器进行通信 Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmail, senderPassword); } }); try { // 创建MimeMessage对象,设置邮件内容 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail)); message.setSubject("Hello, World!"); message.setText("This is a test email from Java."); // 发送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 请注意,上述代码中的`[email protected]`和`your_password`需要替换为实际的件人邮箱地址和密码,`[email protected]`需要替换为实际的收件人邮箱地址,`smtp.example.com`需要替换为实际的邮件服务器主机名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值