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.
Related
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
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 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.
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.
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.