Soumyadeep Roy
Soumyadeep Roy

Reputation: 5

Umlaut character support in java mailapi

MimeBodyPart mimebodypart = new MimeBodyPart();
DataSource datasource = new FileDataSource(file);
mimebodypart.setDataHandler(new DataHandler(datasource));
//mimebodypart.setFileName(MimeUtility.encodeText(file.getName(), "UTF-8", null));
mimebodypart.setFileName(file.getName());

In the above code I have the file name as PDF testöäüßÜÄÖtest DEV2 - 13.06.2016.tiff The attachment is coming as untitled_000000456.tiff <000000456> is a random number that is showing up.

Using stöäüßÜÄÖ is working fine but when the string is one more character long i.e estöäüßÜÄÖ it is not working.

Also öäüßÜÄÖöäüßÜÄÖ and testtesttest (any length) is working.

Could anyone please point out what I am doing wrong?

I am using JavaMail API JAR » 1.5.6

https://mvnrepository.com/artifact/javax.mail/javax.mail-api/1.5.6

java Version 1.8

EDIT

After the some headers I am able to get the proper filename but the message body is missing. Here is the updated code.

try {
    Message message = new MimeMessage(Session.getInstance(System.getProperties()));
    message.setHeader("MIME-Version", "1.0");
    message.setHeader("Content-Disposition", "attachment; filename=\""+ file.getName() +"\""); 
    //file.getName() returns PDF "testöäüßÜÄÖtest DEV2 - 13.06.2016.tiff"
    message.saveChanges();

    String email_subject = "subject";
    message.setSubject(email_subject, "UTF-8", null));
    message.setSentDate(new Date());

    MimeBodyPart content = new MimeBodyPart();
    String msg_body = "Message body";
    content.setContent("<p style=font-style:italic;font-size:20px>"+ msg_body +" </p>", "text/html;charset=UTF-8");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(content);



    MimeBodyPart attachment = new MimeBodyPart();
    DataSource source = new FileDataSource(file); //file is of type File 
    attachment.setDataHandler(new DataHandler(source));
    multipart.addBodyPart(attachment);

    message.setContent(multipart);
    message.getFlags().add(Flags.Flag.DRAFT);

    OutputStream out = new FileOutputStream(fileName);
    //fileName.getName() returns PDF testöäüßÜÄÖtest DEV2 - 13.06.2016.eml
    message.writeTo(out);
    out.close();
    out=null;
}catch (Exception ex) {
    ex.printStackTrace();
}

What I am doing wrong that the body is missing?

Upvotes: 0

Views: 867

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

Try this simpler and more correct version:

Message message = new MimeMessage(Session.getInstance(System.getProperties()));

String email_subject = "subject";
message.setSubject(email_subject, "UTF-8");
message.setSentDate(new Date());

MimeBodyPart content = new MimeBodyPart();
String msg_body = "Message body";
content.setText("<p style=font-style:italic;font-size:20px>"+ msg_body +" </p>", "UTF-8", "html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(content);

MimeBodyPart attachment = new MimeBodyPart();
attachment.attachFile(file);
multipart.addBodyPart(attachment);

message.setContent(multipart);
message.setFlags(new Flags(Flags.Flag.DRAFT), true);

OutputStream out = new FileOutputStream(fileName);
message.saveChanges();
message.writeTo(out);
out.close();

If that doesn't work, post the contents of the file referenced by fileName.

Upvotes: 1

Related Questions