I am sending mail in with a excel attachment file through JAVA mail. Whenever i send the email to a IMAP email box it's filename gets changed to Untitled attachment as oppose to name i am passing for other email boxes it works file.
I have contacted my email provided and they suspect it is formatting issue as JAVa mail by default formats in RFC 2231, for IMAP it has to be RFC 2047/2231.
Note: application is deployed on Widfly 9 using JAVA 8.
This is how i call the method:
ByteArrayOutputStream baos = new ByteArrayOutputStream(excel_bytes.length);
baos.write(excel_bytes, 0, excel_bytes.length);
DataSource aAttachment = new ByteArrayDataSource(baos.toByteArray(),"application/vnd.ms-excel");
String to_email = tenant.getEmail();
email.sendRechargeBillEmail(to_email, aAttachment, file_name + ".xlsx",tenant.getCompanyName(), String.valueOf(job.getStartDate()), String.valueOf(job.getEndDate()));
This is the class which does have the email methods: package com.nextcontrols.rechargecalculation;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class SendAttachmentInEmail {
private static final Logger logger =
Logger.getLogger(SendAttachmentInEmail.class.getCanonicalName());
public void sendEmail(String to_email, DataSource attachment, String
file_name, String from_email, String user_name, String password, String
email_body, String email_subject) {
Properties props = new Properties();
props.put("mail.smtp.host", "mail2.nextcontrols.local");
Session session = Session.getDefaultInstance(props);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from_email));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to_email));
message.setSubject(email_subject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(email_body, "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart mailattachment = new MimeBodyPart();
DataSource source = attachment;
mailattachment.setFileName(file_name);
mailattachment.setDataHandler(new DataHandler(source));
mailattachment.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(mailattachment);
message.setContent(multipart);
Transport.send(message);
logger.info("////// Sent message successfully to "+ to_email);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public void sendRechargeBillEmail(String to_email, DataSource attachment, String file_name,String tenant_name, String start_date, String end_date){
String subject = "Billing - " + tenant_name + " - " + start_date + " to " + end_date;
String bodyText = "Please find attached your utility recharge bill for " + start_date + " to " + end_date;
String note = "<i>This is an automated email, please do not reply to this address</i>";
String fromText = "Regards,<br />-- <br />";
String disclaimerTop = "Privacy and Confidentiality Notice";
String disclaimerDown = " txt";
bodyText = bodyText + "<br /> <br /> <br />" + note + "<br />" + fromText + "<br />" + addColor (disclaimerTop, Color.GRAY ,"2") + "<br />" + addColor(disclaimerDown, Color.GRAY, "1");
sendEmail(to_email, attachment, file_name, "[email protected]", "Billing", "billing", bodyText, subject);
}
public static String addColor(String msg, Color color, String size) {
String hexColor = String.format("#%06X", (0xFFFFFF & color.getRGB()));
String colorMsg = "<FONT size = \" "+ size +"\" COLOR=\"" + hexColor + "\">" + msg + "</FONT>";
return colorMsg;
}
}
If your IMAP server is changing the file name because it doesn't understand RFC 2231, then it really needs to be updated. RFC 2231 is 20 years old.
You can disable the use of RFC 2231 to encode the file name by setting the JavaMail System property mail.mime.encodeparameters
to "false".