I need to write email content from hbase to .html file format. I have all data of email in java object. I am using java.email lib for this. However facing issue with writing multipart image MimeMessage to .html file.
I have image embedded in html as follows. I tried different approach without datasource those are the commented lines in below code.
MimeMessage mimeMessage = outlookMsg.toMimeMessage();
mimeMessage.setFrom("abc#gmail.com");
mimeMessage.setRecipients(RecipientType.TO, "xyz#gmail.com");
mimeMessage.setSubject("story completion");
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
MimeBodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\"/>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(new File("D:\\JAVA\\Practice_Workspace\\pst\\krishna_radha.jpg"));
messageBodyPart.setDataHandler(new DataHandler(fds));
//messageBodyPart.setHeader("Content-ID", "<image>");
messageBodyPart.setContentID("<image>");
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
//messageBodyPart.attachFile("D:\\JAVA\\Practice_Workspace\\pst\\krishna_radha.jpg");
mimeMessage.setContent(multipart);
File file = new File("D:\\JAVA\\Practice_Workspace\\pst\\htmlemail.html");
//OutputStream os = new FileOutputStream(file);
FileOutputStream os = new FileOutputStream(file);
mimeMessage.writeTo(os);
os.flush();
os.close();
I am getting output as below. Image is not getting displayed.
Please help. Thanks in advance.
enter image description here
Related
I have a web service that sends an email with attachments.
the code snippet that sends email is
MimeMultipart content = new MimeMultipart("related");
msg.setContent(content);
MimeBodyPart attachment = new MimeBodyPart();
File file = new File("filename.txt");
String fileName = "";
DataSource fds;
String fullPathFile = mail.getAttachment().get(i);
String pathArray[] = fullPathFile.split("/");
fds = new FileDataSource(file);
attachment.setDataHandler(new DataHandler(fds));
attachment.setHeader("Content-ID", "<" + id + ">");
attachment.setFileName(fds.getName());
content.addBodyPart(attachment);
This works fine for every email app. But in the native iPhone email app, I am unable to view the attachment.
In the image, we can see the attachment icon but when I open the email, I find no attachments
I also referred the link:
https://discussions.apple.com/thread/7491137?start=30&tstart=0
Is there a programing solution for this?
The way you attache the MimeBodyPart is causing this issue. I had the same problem. Your fix would look like:
attachments = new MimeBodyPart();
DataSource source = new FileDataSource(dest);
attachments.setDataHandler(new DataHandler(source));
attachments.setFileName(source.getName());
mp.addBodyPart(attachments);
Multipart htmlAndTextMultipart = new MimeMultipart("alternative");
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html; charset=utf-8");
htmlAndTextMultipart.addBodyPart(htmlBodyPart);
MimeBodyPart htmlAndTextBodyPart = new MimeBodyPart();
htmlAndTextBodyPart.setContent(htmlAndTextMultipart);
mp.addBodyPart(htmlAndTextBodyPart);
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.
Currently I am attaching files (small) in mail as follows:
byte[] byteArray = IOUtils.toByteArray(new FileInputStream(file));
MimeBodyPart messageBodyPart = new PreencodedMimeBodyPart("base64");
String contentType = "application/octet-stream";
String base64Content = new String(Base64.encodeBase64(byteArray));
messageBodyPart.setContent(base64Content, contentType);
messageBodyPart.setFileName(MimeUtility.encodeText(attachment.getFileName(),
CharEncoding.UTF_8, null));
messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(messageBodyPart);
But reading the file to a byte[] at a time won't work for large files. But at the end I want to put attachments in base64 encoded string in email. So how can I tackle large files in attachment here?
Use MimeBodyPart.attachFile:
messageBodyPart.attachFile(file, "application/octet-stream", "base64");
The file won't be read into memory, it will be encoded "on the fly" as the message is sent.
Well you can try with it to send whatever file you want. Also any number of files.
/**
Multi part message email
**/
Multipart multipart = new MimeMultipart();
//Add atachments
String[] attachments = new String[2];
attachments[0] = "your_complete_path.pdf";
attachments[1] = "your_complete_path.txt";
if(attachments != null && attachments.length > 0) {
for (String str : attachments) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(str);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
}
message.setContent(multipart);
I am currently trying to send an email with a picture inside the mail using the Google App Engine 1.9.5. This features is availible only from the version 1.9.0 of the SDK :
Users now have the ability to embed images in emails via the Content-Id attachment header.
https://code.google.com/p/googleappengine/issues/detail?id=965
https://code.google.com/p/googleappengine/issues/detail?id=10503
Source : https://code.google.com/p/googleappengine/wiki/SdkForJavaReleaseNotes
This is my code :
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("leo.mieulet#xxx.com", "xxx.com newsletter"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("leo.mieulet#xx.com", "Leo Mieulet"));
msg.setSubject("Inline image test : "+new Date().getTime());
String imageCid = "graph";
DataSource ds = new ByteArrayDataSource(imageBase64, "image/png");
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setDataHandler(new DataHandler(ds));
imagePart.setFileName(imageCid + ".png");
imagePart.setHeader("Content-Type", "image/png");
imagePart.addHeader("Content-ID", "<" + imageCid + ">");
String htmlBody = "My html text... <img src=\"cid:"+imageCid+"\"> ... ends here.";
// Create alternate message body.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>"+htmlBody+"</body></html>", "text/html");
final Multipart multipart = new MimeMultipart();
multipart.addBodyPart(htmlPart);
multipart.addBodyPart(imagePart);
msg.setContent(multipart);
msg.saveChanges();
Transport.send(msg);
I receive an email which looks like :
Could anyone help me with the problem ?
Based on the imageBase64 variable name, you seems to give to the ByteArrayDataSource the image already encoded in Base64. You should directly use the image byte array without Base64.encode() it.
Awesome ! ;)
If you want to display the image in pure HTML ( in app-engine doGet() context ) :
//byte[] imgContent = the content of your image
Base64 base64 = new Base64();
imgContent = base64.encode(imgContent);
resp.getWriter().write("<html><img src='data:image/png;base64,"+new String(imgContent)+"'></html>");
And as said #benoit-s, you don't need to encode in base64 the content of your image.
I just edited this line :
DataSource ds = new ByteArrayDataSource(imageBase64, "image/png");
to
//byte[] imageAsByteArray = the content of your image
DataSource ds = new ByteArrayDataSource(imageAsByteArray, "image/png");
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.