0

出于教育目的,我尝试使用 Jav Mail 直接发送一个字节 [],而不是之前保存为文件。

我有这个:

        Properties props = new Properties();
        props.put("mail.debug", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");            
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "587");                 

        Authenticator auth = new SMTPAuthenticator();

        Session mailSession = Session.getDefaultInstance(props, auth);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject (asunto, "text/plain");
        message.setContent(mensaje, "text/plain");
        message.setSentDate (new java.util.Date());
        message.setFrom(new InternetAddress(SMTP_AUTH_USER));

        InternetAddress address[] = new InternetAddress[destino.length];
        for( int i = 0; i < destino.length; i++ ) {
            address[i] = new InternetAddress ( destino[i] );
        }
        message.setRecipients (Message.RecipientType.TO, address);

        if (adjuntos){
            MimeBodyPart mbp = new MimeBodyPart();
            mbp.setFileName("AttachedFile");
            DataSource ds = new ByteArrayDataSource(archive, MIME);
            mbp.setDataHandler(new DataHandler(ds));
        }           

        transport.connect();
        transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));

        transport.close();

但是当我收到邮件时,没有任何附件。我不是专家,但我认为 MimeBodyPart 没有“附加”到消息中。

提前致谢!

4

1 回答 1

1

我找到了这个,并且为我工作:) 谢谢!

        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp);
        message.setContent(mp);
于 2013-06-16T21:13:52.380 回答