Missing CR char in binary email attachment in Java - java

I am using the following code to send a binary file as an email attachment in Java. The code works in that is does send the file. However, in the file that is received any hex value of $0d either deleted or converted to $0a. Since the file is a binary file not a text file the received file is incorrect. Any suggestions please?
Rgds,
Helen
String fileAttachment = "command.cmd";
Session session =
Session.getInstance(props, null);
// Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(emailSubject);
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
FileDataSource source =
new FileDataSource(fileAttachment);
System.out.println("Sending");
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
messageBodyPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send( message );

Binary attachments should really be encoded in a way that their non-printable characters are gone. The most obvious way to do that coming to mind would be BASE64 encoding.

Related

What is maximum size for attachment for mimebodypart in sending email with Gmail API (Java)?

I am using Gmail Java SDK for sending email with attachment.
From this document, the maximum of uploading file size is 35 MB. (https://developers.google.com/gmail/api/v1/reference/users/messages/send).
However, In the reality, I only can send email with attachment with only 5MB maximum size, beyond that I get 400 Bad Request Too Large error from google.
This is my code for creating the mime message before send:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(from));
email.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(SUBJECT_RE + subject);
email.setReplyTo(new Address[]{new InternetAddress(from)});
String references = getMailReferences(messageId, service);
if(StringUtils.isNotEmpty(references)) {
email.setHeader(MAIL_HEADER_REFERENCES, references);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(bodyText, "text/plain");
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(mimeBodyPart);
for(int i=0 ; i< attachments.size() ; i++) {
EmailAttach attachment = attachments.get(i);
MimeBodyPart mimeBodyPartAttachment = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(attachment.getAttachmentBytes());
DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream, attachment.getFileName()));
mimeBodyPartAttachment.setDataHandler(dataHandler);
mimeBodyPartAttachment.setFileName(dataHandler.getName());
multipart.addBodyPart(mimeBodyPartAttachment);
}
email.setContent(multipart);
Is there any size limitation sending email with attachment with gmail or there is change needed in code to handle big attachment (example handle/creating MimeBodyPart) (>5MB)?
Solved, You need to use the method send with AbstractInputStreamContent parameter and DON'T do encode Base64 for the message content:
Message message = createMessageWithEmail(emailContent, threadId);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.writeTo(buffer);
message = service.users().messages().send(userId, message, new ByteArrayContent("message/rfc822", buffer.toByteArray())).execute();

problems with attachments (javax.mail)

i have a problem with the attachments, when I send these files to an email xxx#gmail.com, from the gmail application (mobile application) shows the exact amount of attachments but from Mail (IOS) shows some more.
I apologize for the time !! thank you very much
Note: I am using java-mail.1.4.4
From gmail(native)
From Mail(IOS)
Code:
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "HTML code";
messageBodyPart.setContent(htmlText, "text/html");
multipart.addBodyPart(messageBodyPart);
//code for images part ...... //
//code for attachments
messageBodyPart = new MimeBodyPart();
String pdf = pdf1;
DataSource source = new FileDataSource(pdf);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(pdf);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
messageBodyPart = new MimeBodyPart();
String xml = xml1;
DataSource sourceXml = new FileDataSource(xml);
messageBodyPart.setDataHandler(new DataHandler(sourceXml));
messageBodyPart.setFileName(xml);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
First, you're using a very old version of JavaMail. Please upgrade if possible.
If your html text in your message is referencing the images, you should create a multipart/related message.
If you want that message to also include attachments, you need to nest that multipart/related content in a multipart/mixed, with the multipart/related being the first body part and the attachments being other body parts.

Sending email as attachment in java

I have an image in my d drive and i want to send it as an email attachment in java. Recipients mail will be entered by sender, I just want to attach it to my email account. Please help.
thank you.
Check out the link(my answer to that question) for email sending utility code. You've got to add few line of code to send mail with attachment.
On submit the information should come to email
In EmailUtility.java after
msg.setSentDate(new Date());
comment
msg.setText(message);
And add the following code:
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
String attachFile = "C:/imgname.jpg";
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if(reason.equals("attach"))
if (attachFile != null) {
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(attachFile);
multipart.addBodyPart(attachPart);
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
You've got to change C:/imgname.jpg to your file name along with its path.

The file is never sent in JavaMail

I am trying to send an attachment by using JavaMail API, and it doesn't sem to work. Can you please tell me the mistake which I am making, the file ABC.pdf is in the same file of the project. I do get the correct path in "s" in the first sysout. The program never reaches the second sysout. I hace replace Id's with "trial" in email id.
Please help
Message message = new MimeMessage(session);
message.setSubject("Trial Messages");
message.setFrom(new InternetAddress("trial#gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("trial#yahoo.co.in"));
MimeBodyPart body = new MimeBodyPart();
body.setText(content);;
Multipart part = new MimeMultipart();
part.addBodyPart(body);
File f = new File("ABC.pdf");
String s = f.getAbsolutePath();
System.out.printf(s);
DataSource source = new FileDataSource(s);
body.setDataHandler(new DataHandler(source));
body.setFileName("ABC Bill");
part.addBodyPart(body);
System.out.printf(s);
Transport.send(message);
System.out.printf(s);
Try to replace
body.setFileName("ABC Bill");
with
body.setFileName(source.getName());
You can try here to passe source.getName() to sysout to see if everything is correct (the right file name)
add also
// add the Multipart to the message
message.setContent(part);
remove part.addBodyPart(body); because you added it twice
Message message = new MimeMessage(session);
message.setSubject("Trial Messages");
message.setFrom(new InternetAddress("trial#gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("trial#yahoo.co.in"));
MimeBodyPart body = new MimeBodyPart();
body.setText(content);;
Multipart part = new MimeMultipart();
File f = new File("ABC.pdf");
String s = f.getAbsolutePath();
System.out.printf(s);
DataSource source = new FileDataSource(s);
body.setDataHandler(new DataHandler(source));
body.setFileName("ABC Bill");
part.addBodyPart(body);
message.setContent(part);
System.out.printf(s);
Transport.send(message);
System.out.printf(s);

How do I send mail with both plain text as well as HTML text so that each mail reader can choose the format appropriate for it?

From http://www.oracle.com/technetwork/java/faq-135477.html#sendmpa:
You'll want to send a MIME multipart/alternative message. You
construct such a message essentially the same way you construct a
multipart/mixed message, using a MimeMultipart object constructed
using new MimeMultipart("alternative"). You then insert the text/plain
body part as the first part in the multpart and insert the text/html
body part as the second part in the multipart. You'll need to
construct the plain and html parts yourself to have appropriate
content. See RFC2046 for details of the structure of such a message.
Can someone show me some sample code for this?
This is a part of my own code:
final Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress, senderDisplayName));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(m.getRecipient(), m.getRecipientDisplayName()));
msg.setSubject(m.getSubject());
// Unformatted text version
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(m.getText(), "text/plain");
// HTML version
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(m.getHtml(), "text/html");
// Create the Multipart. Add BodyParts to it.
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);
// Set Multipart as the message's content
msg.setContent(mp);
LOGGER.log(Level.FINEST, "Sending email {0}", m);
Transport.send(msg);
Where m is an instance of my own class.

Categories