Attach TXT file using Javamail

Asked

Viewed 1,253 times

3

I’m using Javamail for sending email, with attachment, on Android.

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

My file attachment code:

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 the same came attached as file. How do I attach the ". txt" as file?

2 answers

4


I believe you have to say explicitly that this "Bodypart" is an attachment, add this line:

attachment.setDisposition(MimeBodyPart.ATTACHMENT);

Or still try to do it that way (Full 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 but with byte[], it’s like this:

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

Thinking so try the answer option of the Dante:

DataSource file = new FileDataSource(filename){
   public String getContentType() {  
       return "application/octet-stream";  
   }  
};
  • didn’t work. Keeps coming in the body of the email. Any other suggestions?

  • @felipearon, try adding the application/octet-stream mimetype. Check updated response.

  • gave error: Ioexception while sending message; javax.activation.Unsupporteddatatypeexception: no Object DCH for MIME type application/octet-stream

  • @felipearon, see edition in reply.

  • that way it worked, just like I mentioned in Dante’s answer. Only detail is that visualizing by Thunderbird it shows in the body and not as an attachment.

  • @felipearon, dai already believe that it is particularities of each email manager, in handling the attachments.

Show 1 more comment

3

Man, what I use is 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 your code doesn’t have the line:

mbp.setDisposition(Part.ATTACHMENT);  

Try to add this to your code and test:

attachmentPart.setDisposition(Part.ATTACHMENT);  
  • That way it worked correctly. Only one detail: Opening the email by Thunderbird the contents of the file appear in the body of the email. By the Browser (gmail) it shows as an attachment.

  • @felipearon It may be some specific problem of Thunderbird...I open here with Outlook and the email comes as attached file, it is strange. You need to open by Thunderbird?

  • I have no need not. I have already done a test with the client, and he also received attached. So closed!

  • Success then! If he has any need to download like this some day, indicates Outlook to him which works the same way! I don’t know if you can get more than one response marked as correct, but if you do, mark mine too! Hugs!

  • So, Dante, unfortunately, there’s no way to give two correct answers. As @Fernando answered the same as you, and was the first to answer, I marked his as right. But anyway, thanks too, it helped a lot. :)

  • Everything is quiet! Success for you!

  • 1

    Good the balcony to overwrite the method getContentType of FileDataSource. +1.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.