欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现发送邮件功能(可以添加附件)

程序员文章站 2023-12-03 10:47:10
import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.*;i......

记录一下在java中发送邮件的实现方式,下面这个工具类是根据在网上查阅的资料自己整理出来的。

类中只有两个方法:
1.第一个就是普通的发送文本内容邮件的方法。
2.第二个是能发送附件邮件的方法。如果还想实现更复杂的可以自行百度。

代码

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Properties;
public class MailUtil{
    
    //邮件服务器地址(SMTP)
    public static String myEmailSMTPHost = "smtp.ampthon.com";

    /**
     * 发送纯文本邮件
     * @param mailTitle 邮件主题
     * @param content 邮件内容
     * @param sendMail 发件人邮箱地址
     * @param sendName 发件人姓名
     * @param receiveMail 收件人
     * @param sendMailPassword 发送人邮箱密码
     * @return
     * @throws Exception
     */
     public static void sendEmail(String mailTitle, String content, String sendMail, String sendName,
                   String receiveMail, String sendMailPassword) throws Exception{
         Properties p = new Properties();
         p.setProperty("mail.transport.protocol", "smtp");
         p.setProperty("mail.smtp.host", myEmailSMTPHost);
         p.setProperty("mail.smtp.localhost", "localhost");//不加的话在linux报错:501 syntax:ehlo hostname
         p.setProperty("mail.smtp.auth", "true");
         
         Session session = Session.getInstance(p);
         //设置为debug模式,可以查看详细的发送log
         session.setDebug(true);
         
         MimeMessage message = new MimeMessage(session);
         //发件人
         message.setFrom(new InternetAddress(sendMail, sendName, "UTF-8"));

         //收件人
         message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, receiveMail, "UTF-8"));

         //邮件主题
         message.setSubject(mailTitle, "UTF-8");

         //邮件正文
         message.setContent(content, "text/html;charset=UTF-8");

         //设置显示的发件时间
         message.setSentDate(new Date());

         //保存前面的设置
         message.saveChanges();

         //根据session获取邮件传输对象
         Transport transport = session.getTransport();
         //连接邮箱服务器
         transport.connect(sendMail, sendMailPassword);
         //发送邮件到所有的收件地址
         transport.sendMessage(message, message.getAllRecipients());
         transport.close();
     }

      //和上边方法基本相同,只不过可以添加附件发送邮件
      public static void sendEmailWithFile(String mailTitle, String content, String sendMail, String sendName,
                   List<String> receiveMailList, String sendMailPassword)) throws Exception{
         Properties p = new Properties();
         p.setProperty("mail.transport.protocol", "smtp");
         p.setProperty("mail.smtp.host", myEmailSMTPHost);
         //p.setProperty("mail.smtp.localhost", "localhost");//不加的话在linux报错:501 syntax:ehlo hostname
         p.setProperty("mail.smtp.auth", "true");
   
         Session session = Session.getInstance(p);
         //设置为debug模式,可以查看详细的发送log
         session.setDebug(true);
         
         MimeMessage message = new MimeMessage(session);
         //发件人
         message.setFrom(new InternetAddress(sendMail, sendName, "UTF-8"));

         //收件人
         for(Strin receiveAddress : receiveMailList){
             message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveAddress, receiveAddress, "UTF-8"));
         }

         //邮件主题
         message.setSubject(mailTitle, "UTF-8");

         //向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
         MimeMultipart multipart = new MimeMultipart();
         //设置邮件的文本内容
         MimeBodyPart contentPart = new MimeBodyPart();
         contentPart.setContent(content, "text/html;charset=UTF-8");
         multipart.addBodyPart(contentPart);
         //添加附件
         MimeBodyPart filePart = new MimeBodyPart();
         DataSource source = new FileDataSource("E:\\file_path" + File.separator + "附件.xlsx");
         //添加附件的内容
         filePart.setDataHandler(new DataHandler(source));
         //添加附件的标题
         filePart.setFileName(MimeUtility.encodeText("附件.xlsx"));
         multipart.addBodyPart(filePart);
         multipart.setSubType("mixed");
         //将multipart对象放到message中
         message.setContent(multipart);

         //设置显示的发件时间
         message.setSentData(new Date());

         //保存前面的设置
         message.saveChanges();

         //根据session获取邮件传输对象
         Transport transport = session.getTransport();
         //连接邮箱服务器
         transport.connect(sendMail, sendMailPassword);
         //发送邮件到所有的收件地址
         transport.sendMessage(message, message.getAllRecipients());
         transport.close();
     }
}

另外,邮件的内容(content)也可以通过拼接html代码来发送文本内容格式是表格的邮件,如下:

content = "<html><head><head><body><table>" +
          "<tr>" + 
          "<td>第一个单元格</td>" +
          "<td>第二个单元格</td>" +
          "</tr>" + 
          "</table></body></html>"

按上边这个content发送的邮件内容将展示为一个表格。

好了,有不同意见的欢迎留言哈~

本文地址:https://blog.csdn.net/sfh2018/article/details/104652557