发送email,附带附件

1.导入依赖

 <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.6</version>
    </dependency>
2.前端注意地方:
<form name="form2" id="form2" action="${pageContext.request.contextPath}/email/sendEmail" 
method="post" enctype="multipart/form-data">

3.controller
3.1准备工具类

public class SendEmailUtil {




/*
@param:
sendAccoun: 发送者的邮箱用户名
sendPassword:发送者的邮箱密码
reciveAccont:收件人的邮箱
path:资源文件路径
filename:资源文件
subject:邮件主题
text:邮件地址
@注:自行修改:邮箱和邮箱的授权码
@注:若不是使用163邮箱发送邮箱则需要修改 邮件服务器地址 properties.setProperty("mail.smtp.host","smtp.163.com"); 修改为发送者的邮件服务器
 */
    public  static  void sendEmail( String  sendAccount, String  sendPassword,String reciveAccont, String path ,String filename,String subject,String text) throws  Exception{

//        附件
        File file = new File(path,filename);

        //设定的邮件服务器的协议内容
        Properties properties = new Properties();
        //邮件的协议
        properties.setProperty("mail.transport.protocol","smtp");
        //邮件服务器的地址
        properties.setProperty("mail.smtp.host","smtp.163.com");
        //使用授权码
        properties.setProperty("mail.smtp.auth","true");

        //使用授权码创建我们的授权对象  linzhonglang007 设置的邮箱的授权码
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxxxxxx","xxxxxx");
            }
        };
        //

        //创建一个和邮件服务器的会话对象(163邮件服务器信任当前客户端)
        Session session = Session.getInstance(properties,authenticator);
        //设置我们可以在控制台输出传输的过程
        session.setDebug(true);
        // 发送邮件的时候需要一个消息对象(封装我们发送内容)
        Message message  = new MimeMessage(session);
        //设定邮件的发送人(InternetAddress extends Address )
        message.setFrom(new InternetAddress(sendAccount));
        // 给当前的自己的邮箱抄送一份(网易邮件服务器不会将当前的邮件作为垃圾邮件处理)
        message.addRecipient(Message.RecipientType.CC,new InternetAddress(sendAccount));

        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(reciveAccont));

        //设定我们发送的内容(List)
        MimeMultipart mimeMultipart = new MimeMultipart();
        // obj
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setText(text);
        // list.add(obj);
        mimeMultipart.addBodyPart(mimeBodyPart);


        //使用工具类包装路径下的文件
        DataSource dataSource = new FileDataSource(file);
        //邮件的一部分内容
        DataHandler handler = new DataHandler(dataSource);

        MimeBodyPart bodyPart = new MimeBodyPart();
        //设置发送邮件的名称
        bodyPart.setFileName(MimeUtility.encodeText(filename));
        //填充bodyPart
        bodyPart.setDataHandler(handler);
        //将我们的主题内容放入到容器中
        mimeMultipart.addBodyPart(bodyPart);

        //使用的message对象添加我们的消息主题  multipart
        message.setContent(mimeMultipart);
        //设定发送邮件的主题内容
        message.setSubject(subject);
        //设定发送的时间
        message.setSentDate(new java.util.Date());
        //保留添加的内容
        message.saveChanges();

        //发送邮件(传输对象)
        Transport transport = session.getTransport();
        //需要使用我们邮箱的用户名和密码
        transport.connect(sendAccount,sendPassword);
        //发送邮件
        transport.sendMessage(message,message.getAllRecipients());
        //关闭连接
        transport.close();

    }





}

3.2写方法

 //调用工具类 发送邮件
        /*@param:
        sendAccoun: 发送者的邮箱用户名
        sendPassword:发送者的邮箱密码
        reciveAccont:收件人的邮箱
        path:资源文件路径
        filename:资源文件
        subject:邮件主题
        text:邮件文档*/
    @RequestMapping("sendEmail")
    public String sendEmail(MultipartFile files, EmailVo emailVo) {
        try {
            String path = "E:\\Download\\";
            File file = new File(path,files.getOriginalFilename());

            files.transferTo(file);
            String sendAccount = "xxxxxxx";
            String sendPassword = "xxxxxxxx";
            String reciveAccount = emailService.selectReciveAccontByEmployeeId(emailVo.getAddressee());
            String filename = files.getOriginalFilename();
            String title = emailVo.getTitle();
            String text = emailVo.getEmailcontent();
            SendEmailUtil.sendEmail(sendAccount, sendPassword, reciveAccount, path, filename, title, text);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "forward:index.jsp";
    }

4.service层

 @Override
    public String selectReciveAccontByEmployeeId(String addressee) {
        return employeeMapper.selectReciveAccontByEmployeeId(addressee);
    }

5.mapper层通过id查询收件人的邮箱地址

<select id="selectReciveAccontByEmployeeId" resultType="java.lang.String" parameterType="java.lang.String">
    select e.email
    from archives e
    where e.emp_fk = #{eid}

  </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值