使用javamail发送邮件的时候如何阻止附件内容输出到控制台
伊谢尔伦
伊谢尔伦 2017-04-18 09:53:06
0
4
471

我在使用JavaMail发送带附件的邮件时候,每次到了Transport.sendMessage()这一步,控制台就会输出附件内容,请问如何设置可以取消输出呢?

public void sendFileAttachedMail(String fromMail, String toMail, String fromMailPwd, String bookId) {
        Properties prop = new Properties();
        prop.setProperty(MAIL_HOST, MAIL_HOST_VALUE);
        prop.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_PROTOCOL_VALUE);
        prop.setProperty(MAIL_SMTP_AUTH, MAIL_SMTP_AUTH_VALUE);
        Session session = Session.getInstance(prop);
        session.setDebug(true);
        try {
            Transport ts = session.getTransport();
            String fromMailPrefix = fromMail.split("@")[0];
            ts.connect(MAIL_HOST_VALUE,fromMailPrefix, fromMailPwd);
            String subject = "FILE ATTACHED MAIL TEST";
            String content = "Mail Content RE";
            String fileSavePath = "E://attachMail.eml";
            Message message = createFileAttachedMail(session, fromMail, toMail, subject, content, bookId, fileSavePath);
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        } catch (Exception e) {
            if(logger.isErrorEnabled()){
                logger.error("send fileAttachedMail failed!",e);
            }
        }
    }
    
    
     public MimeMessage createFileAttachedMail(Session session, String fromAdd, String toAdd, String subject, String content, String fileObjectId, String fileSavePath) throws Exception {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAdd));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAdd));
        message.setSubject(subject);
        // 邮件正文
        MimeBodyPart text  = new MimeBodyPart();
        text.setContent(content, MAIL_CONTENT_FORMAT_CHARSET);

        // 附件
        MimeBodyPart attach = new MimeBodyPart();
        DataHandler handler = new DataHandler(new FileDataSource(this.gridFSService.readFiles(fileObjectId)));
        attach.setDataHandler(handler);
        attach.setFileName(handler.getName());

        // 创建容器描述数据关系
        MimeMultipart mp = new MimeMultipart();
        mp.addBodyPart(text);
        mp.addBodyPart(attach);
        mp.setSubType("mixed");

        message.setContent(mp);
        message.saveChanges();
        //将创建的email写入到本地存储
        //message.writeTo(new FileOutputStream(fileSavePath));
        return message;
    }
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
洪涛

session.setDebug(true);Remove this sentence

小葫芦

If you increase the level of the logger, it will not print. It must be printed during its internal implementation.

左手右手慢动作

Printing to the console is equivalent to opening another thread, which will affect the performance of the business logic.

Peter_Zhu

Brother, whether you output or not is up to you. It outputs, there must be output statements in the code. Isn’t it OK if you delete it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!