发送邮件java

一、邮件服务器与传输协议
要在网络上实现邮件功能,必须要有专门的邮件服务器。这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中。

SMTP服务器地址:一般是 smtp.xxx.com,比如163邮箱是smtp.163.com,qq邮箱是smtp.qq.com。

SMTP协议

通常把处理用户smtp请求(邮件发送请求)的服务器称之为SMTP服务器(邮件发送服务器)
二、开通邮箱服务并记录授权码
三、java开发
在这里插入图片描述
使用到的jar包:

mail.jar
activation.jar

    public void sendEmail(List<SignExcelData> list,String excelFileName) throws MessagingException {


        //梅和超--18755585253@139.com
        //创建一个配置文件保存并读取信息
        Properties properties = new Properties();

        //设置qq邮件服务器
        properties.setProperty("mail.host","smtp.163.com");
        //设置发送的协议
        properties.setProperty("mail.transport.protocol","smtp");
        //设置用户是否需要验证
        properties.setProperty("mail.smtp.auth", "true");


        //=================================准备工作完毕

        //1.创建一个session会话对象;
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("17856941469@163.com", "NGLPNSJMQWJHDOGC");
            }
        });

        //可以通过session开启Dubug模式,查看所有的过程
        session.setDebug(false);


        //2.获取连接对象,通过session对象获得Transport,需要捕获或者抛出异常;
        Transport tp = session.getTransport();

        //3.连接服务器,需要抛出异常;服务器,发送人账号,授权码
        tp.connect("smtp.xxx.com","xxxx@163.com","xxxxx");


        //4.连接上之后我们需要发送邮件;
        MimeMessage mimeMessage = mailService.sendAccessoryMail(session,list,excelFileName);

        //5.发送邮件
        tp.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

        //6.关闭连接
        tp.close();
    }

public MimeMessage sendAccessoryMail(Session session, List<SignExcelData> list,String excelFileName) throws javax.mail.MessagingException{

        CheckMail checkMail = sms5gSignatureMapper.findMail();
        //拼接附件
        MimeMultipart allFile =new MimeMultipart();

        //消息的固定信息
        MimeMessage mimeMessage = new MimeMessage(session);

        //邮件发送人
        mimeMessage.setFrom(new InternetAddress(checkMail.getMailSendUsername()));
        //邮件接收人,可以同时发送给很多人,我们这里只发给自己;

        // 接收人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(checkMail.getMailReceviceUsername()));
        mimeMessage.setSubject("签名申请"); //邮件主题

        //文本
        MimeBodyPart body2 = new MimeBodyPart();
        Date nowDate = new Date();
        body2.setContent("以下为申请材料,详见附件,请各位领导审核,感谢!","text/html;charset=utf-8");


        MimeBodyPart body4 = new MimeBodyPart();
        body4.setDataHandler(new DataHandler(new FileDataSource(GlobalConst.SIGNNATURE_EXCEL_PATH+excelFileName)));
        //附件设置名字
        body4.setFileName("申请材料.xls");
        allFile.addBodyPart(body4);

        for(int i=0;i<list.size();i++){
            if(!ObjectUtils.isEmpty(list.get(i).getAuthorizationId())){
                //附件
                MimeBodyPart body3 = new MimeBodyPart();
                body3.setDataHandler(new DataHandler(new FileDataSource(GlobalConst.SIGNNATURE_FILE_PATH+list.get(i).getAuthorizationId())));
                //附件设置名字
                body3.setFileName("【"+list.get(i).getSignName()+"】"+"集团授权书"+list.get(i).getAuthorizationId().substring(list.get(i).getAuthorizationId().indexOf(".")));
                allFile.addBodyPart(body3);
            }
        }




        /*MimeBodyPart body4 = new MimeBodyPart();
        body4.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));
        body4.setFileName(""); //附件设置名字*/

        //拼装邮件正文内容
        MimeMultipart multipart1 = new MimeMultipart();
        //multipart1.addBodyPart(body1);
        multipart1.addBodyPart(body2);
        //1.文本和图片内嵌成功!
        multipart1.setSubType("related");

        //new MimeBodyPart().setContent(multipart1); //将拼装好的正文内容设置为主体
        MimeBodyPart contentText =  new MimeBodyPart();
        contentText.setContent(multipart1);
        //附件
        //allFile.addBodyPart(body4);
        //正文
        allFile.addBodyPart(contentText);
        //正文和附件都存在邮件中,所有类型设置为mixed;
        allFile.setSubType("mixed");


        //放到Message消息中
        mimeMessage.setContent(allFile);
        mimeMessage.saveChanges();//保存修改


        return mimeMessage;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值