Attach TXT file using Javamail

3

I'm using Javamail to send email, with attachment, on Android.

When attaching a file with a ".txt" extension, it is assigning the content to the email body, not as an attached file.

My code for file attachments:

public void addAttachment(String filename) throws Exception {

    DataSource file = new FileDataSource(filename);

    BodyPart attachment = new MimeBodyPart();
    attachment.setDataHandler(new DataHandler(file)); 
    attachment.setFileName(filename);

    multipart.addBodyPart(attachment);
}

I did a test by changing the file extension to ".tx" and it was attached as a file. How do I attach the ".txt" as a file?

    
asked by anonymous 23.07.2014 / 19:43

2 answers

4

I think you have to say explicitly that this BodyPart is an attachment, add this line:

attachment.setDisposition(MimeBodyPart.ATTACHMENT);

Or even try to do this ( Complete example ):

public void addAttachment(String filename) throws Exception {
    DataSource file = new FileDataSource(filename){  

        public String getContentType() {  
            return "application/octet-stream";  
        }  
    };
    MimeBodyPart attachmentPart = new MimeBodyPart();
    attachmentPart.setDataHandler(new DataHandler(file));
    attachmentPart.setFileName(filename);

    // Acredito que só essa linha já resolveria seu problema
    attachmentPart.setDisposition(MimeBodyPart.ATTACHMENT);

    multipart.addBodyPart(attachmentPart);
}

What I use is not with File and yes with byte [], it's like this:

DataHandler handlerAttach = new DataHandler(
new ByteArrayDataSource(binary, "application/octet-stream"));

Thinking so try the answer option Dante :

DataSource file = new FileDataSource(filename){
   public String getContentType() {  
       return "application/octet-stream";  
   }  
};
    
23.07.2014 / 20:05
3

Dude, what I use looks like this:

public void sendMail(String from, String to, String subject, String message, String attach)     {  

        MimeMessage mimeMessage = mailSender.createMimeMessage();  
        mimeMessage.setSubject(subject, CHARSET);  

        Address addressFrom = new InternetAddress(from);  
        mimeMessage.setFrom(addressFrom);  
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  

        Multipart multipart = new MimeMultipart();  

        // Texto  
        BodyPart messageBodyPart = new MimeBodyPart();  
        messageBodyPart.setContent(message, "text/plain");  
        multipart.addBodyPart(messageBodyPart);  

        //Anexo  
        File file = new File(attach);  
        DataSource ds = new FileDataSource(file) {  

            public String getContentType() {  
                return "application/octet-stream";  
            }  
        };  

        BodyPart mbp = new MimeBodyPart();  
        mbp.setDataHandler(new DataHandler(ds));  
        mbp.setFileName(file.getName());  
        mbp.setDisposition(Part.ATTACHMENT);  
        multipart.addBodyPart(mbp);  

        mimeMessage.setContent(multipart);  
        mailSender.send(mimeMessage);  
}  

I noticed that your code does not have the line:

mbp.setDisposition(Part.ATTACHMENT);  

Try to add this way to your code and test:

attachmentPart.setDisposition(Part.ATTACHMENT);  
    
23.07.2014 / 20:32