0

我正在使用 Java Mail API 将文本文件(作为附件)附加到电子邮件,但是当我运行程序时,它会复制文本文件内容并将其放入邮件正文而不是附件中

public class EmailCMSUsers {

    public  static void main(String args[])
    {

        Properties props = new Properties();
        props.put("mail.smtp.host", "10.10.55.11");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable", "true");
        // To see what is going on behind the scene
        //props.put("mail.debug", "true");
        props.put("mail.from", "test123@mycompany.com");
        Session session = Session.getInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO,"test123@mycompany.com");
            msg.setSubject("JavaMail hello world example");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");

            //Transport.send(msg);
            msg.saveChanges();



            /** 
             *  for attaching the documents
             */

         // Create the message part 
             BodyPart messageBodyPart = new MimeBodyPart();

             // Fill the message
             messageBodyPart.setText("This is message body");

             // Create a multipar message
             Multipart multipart = new MimeMultipart();

             // Set text message part
             multipart.addBodyPart(messageBodyPart);

             // Part two is attachment
             messageBodyPart = new MimeBodyPart();

             String filename = "D:\\Deployment Docs\\Document.txt";
             DataSource source = new FileDataSource(filename);
             messageBodyPart.setDataHandler(new DataHandler(source));
             messageBodyPart.setDisposition(Part.ATTACHMENT);
             messageBodyPart.setFileName(filename);
             multipart.addBodyPart(messageBodyPart);

             // Send the complete message parts
             msg.setContent(multipart );





            Transport transport = session.getTransport("smtp");
            transport.connect("10.10.55.11", "test123", "test123");
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();


            System.out.println(" email sucessfully  sent");
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }
    }

}
4

2 回答 2

0

为什么您认为附件被放入邮件正文?也许您的邮件阅读器正在显示附件,就好像它在邮件正文中一样?

于 2013-09-21T19:22:28.553 回答
0

我已经执行了您的代码,如果您删除此行,它可以正常工作:

msg.saveChanges();

但是,使用此代码,通过邮件收到的附件名称将包括路径:"D:\\Deployment Docs\\Document.txt". 如果您想避免这种情况并仅使用规范名称发送文件(不包括路径:“Document.txt”),您可以按如下方式进行:

改变这个:

String filename = "D:\\Deployment Docs\\Document.txt";
DataSource source = new FileDataSource(filename);
//...
messageBodyPart.setFileName(filename);

为了这:

File file = new File("D:\\Deployment Docs\\Document.txt");
DataSource source = new FileDataSource(file);
//...
messageBodyPart.setFileName(file.getName());

希望它可以帮助你。

于 2014-08-27T16:03:10.780 回答