How to read body of mail using javax.mail.internet.MimeMulitpart - jakarta-mail

I am trying to read the body contents of a mail and i keep getting the following "Message" string...but not actual body of mail
SentDate : Mon May 21 14:56:47 CAT 2012
From : {FROM}
Subject : TEST
Message :javax.mail.internet.MimeMultipart#320e7a
Any help/advice on what exactly it is i might be missing?
Thanking you in adnvance
Faheem

A mail could be plain text, html or Multipart(text + attachments), Multipart Alternative(text + html) etc.
You have to iterate through each BodyPart to know it's type and then get the content accordingly. This javamail FAQ entry could help you.

Related

Unable to retrieve attachments from a signed mail having ContentType as "application/pkcs7-mime; name=smime.p7m"

I am trying to read a digitally signed mail from java code using multipart and mime messaging and fetch the attachments (xml, pdf, txt etc.,) and message details.
My code is working fine for mails having Content-Type as : multipart/signed; protocol="application/x-pkcs7-signature";
But For few mails having Content-Type as : application/pkcs7-mime; smime-type=signed-data; name=smime.p7m it is not fetching the attachments and message details. Can anyone explain what is the difference between both of them and how to resolve it.
I recently came across this issue myself, and although this question is three month old, I leave an answer with my findings, just in case.
Both kinds of messages are instances of S/MIME signed messages as specified in RFC2633 (https://www.rfc-editor.org/rfc/rfc2633).
The multipart/signed; protocol="application/x-pkcs7-signature" indicates a clear-signed message (section 3.4.3.3 of the RFC), meaning you can read the original message content without having S/MIME capabilities in your client code. Hence no problem with these.
The application/pkcs7-mime; smime-type=signed-data; name=smime.p7m indicates an S/MIME signedData email (section 3.4.2) Your client code needs S/MIME capability in order to read the original message (even if you don’t care about the signature).
Easiest way (worked for me) is to use bouncycastle's SMIMESigned class (from the S/MIME API, https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15on), like this:
byte[] content = <the signed data's content as byte[]>;
ByteArrayDataSource dataSource = new ByteArrayDataSource(content,"multipart/signed");
SMIMESigned signedData = new SMIMESigned(new MimeMultipart(dataSource));
MimeBodyPart bodyPart = signedData.getContent();
<you can process the body part as normal from here>

how to extract dkim value of Authentication-Results field from message header using java mail

I want to extract dkim value of Authentication-Results field from email header.
I tried with below code and below is the response of code for reference.
code:
String Auth[] =messages[(int) i].getHeader("Authentication-Results");
System.out.println("Authentication-Results : " +
Arrays.toString(Auth) );
output:
Authentication-Results:mta4458.mail.bf1.yahoo.com from=xyz.com;
domainkeys=neutral (no sig); from=xyz-com.20150623.gappssmtp.com;
dkim=pass (ok)
I Want the following output:
dkim=pass
You need to parse the string. It doesn't seem to use the standard MIME format that other headers use so you'll need to write your own parser. The syntax is specified by RFC 5451.

Google Apps Script - Gmail Edit and, after, forward a message

Wherever I receive a message that contains "Nova reserva" in subject, I need to send an email to "accumakerapp-103#forms.zohocreator.com" with this EXACT format:
Remetente : (mail address of the sender)
Destinatário : (mail address of the receiver)
Assunto : (mail subject)
Texto : (mail body)
Someone can help me??
You can refer to the documentation. To be able to filter the required subject, use getSubject(). Then, when you already got the email that you needed using the method above proceed to create a reply message with your specified format using reply(body), reply(body, options), replyAll(body, options) whichever is applicable/suit your requirements. Additionally, if you want to forward the exact message, use forward(recipient, options).

How can I send a file, for example "exe", with Telnet?

With smtp, i know the commands: "HELO", "MAIL FROM", "RCPT TO", "QUIT", but i don't know how can i attach one file. Anyone can help me ?
telnet smtp.xxxx.xxxx 25
helo xxxx.xxxx
mail from: yyy#xxx.xxx
rcpt to: yy2#xxx.xxx
data
subject: hi
hello
.
quit
This is not something that can be easily done. You probably want to use a library (ex : in python) that will take care of formatting your email according to your needs.
In very brief :
Sending an attachement requires the email to be formatted according to the MIME RFC
A MIME formatted message will use some delimiters to separate the different parts of the message (ex : a plain text part, an HTML part, an attachment part, etc...)
Each MIME part will be prefixed by a header detailing the part content
an attachment part will be identified by a "Content-disposition" header, as detailed in RFC 2183.
The representation of your file will have to be specified using the "Content-Transfer-Encoding" header, described in RFC 2045. A common way to encode files for mail transfer is base64.
If you want to get an idea of how complex it is to generate an email with a valid attachment, you can use your email client to check the source of an email with an attachment (most email clients have this feature). That should eventually convince you to avoid doing this manually :)

java mail attachments getting corrupted

This is my code for attaching the files to the mail:
Multipart mp=new MimeMultipart("mixed");
BodyPart mbody=new MimeBodyPart();
mbody.setHeader("Content-Type", "text/html; charset=us-ascii");
mbody.setHeader("Content-Transfer-Encoding","7bit");
mbody.setContent(content2, "text/html");
mp.addBodyPart(mbody);
for(File file:f){
BodyPart mbody2=new MimeBodyPart();
DataSource ds=new FileDataSource(file.getAbsolutePath());
mbody2.setDataHandler(new DataHandler(ds));
mbody2.setFileName(ds.getName());
mbody2.setHeader("Content-Type", "multipart/mixed");
mbody2.setHeader("Content-Transfer-Encoding", "base64");
mp.addBodyPart(mbody2);
}
m.setContent(mp);
content2 is the html content I am embedding in the E-mail, and I am adding files from an arraylist f.
The problem here is that although the files get attached and I receive the E-mail fine, I am unable to open the attachments because the data is corrupt. This happens for all the files I've tried to attach like jpegs, pdfs, spreadsheets, word docs and txt files.
I read here: https://community.oracle.com/thread/1589120 that this could happen because JavaMail uses encoding that messes up the binary data of the file and adding mbody2.setHeader("Content-Transfer-Encoding", "base64"); should fix the problem but that doesn't work for me.
Any ideas on what could be wrong?
Thanks
Time for some debugging...
First, remove all of the setHeader calls; some of them are wrong and none of them should be necessary.
Next, determine if the problem is on the sending end or the receiving end. Try multiple mail readers to see if they all have problems with the attachments.
Try sending plain text attachments. Are they also corrupted?
Post the protocol trace showing what happens when you send a simple message with a simple attachment that fails, so we can see if the message is being constructed correctly.
What version of JavaMail are you using?
What mail reader are you using to view the attachments?

Resources