首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用java mail无法将文本文件作为附件附加

使用java mail无法将文本文件作为附件附加
EN

Stack Overflow用户
提问于 2013-09-21 18:20:10
回答 2查看 1.4K关注 0票数 0

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

代码语言:javascript
复制
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);
        }
    }

}
EN

回答 2

Stack Overflow用户

发布于 2013-09-22 03:22:29

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

票数 0
EN

Stack Overflow用户

发布于 2014-08-28 00:03:11

我已经执行了你的代码,如果你去掉这一行,它就能正常工作:

代码语言:javascript
复制
msg.saveChanges();

但是,使用此代码时,通过邮件接收的附件的名称将包含路径:"D:\\Deployment Docs\\Document.txt"。如果你想避免这种情况,只发送带有规范名称的文件(不包括路径:"Document.txt"),你可以这样做:

更改此设置:

代码语言:javascript
复制
String filename = "D:\\Deployment Docs\\Document.txt";
DataSource source = new FileDataSource(filename);
//...
messageBodyPart.setFileName(filename);

为此:

代码语言:javascript
复制
File file = new File("D:\\Deployment Docs\\Document.txt");
DataSource source = new FileDataSource(file);
//...
messageBodyPart.setFileName(file.getName());

希望能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18931473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档