Get TextBody for an email through the ews-java-api - java

It seems not to be possible to get email body as plain text if there is some in mime encoded mail. How to work around it?

I solved the problem. When you get the item
ExchangeService.bindToItem(EmailMessage.class, itemId, PROPERTY_SET_TEXT_BODY).getBody() use need use following property set and then set request body type to BodyType.Text
PropertySet PROPERTY_SET_TEXT_BODY = new PropertySet(ItemSchema.Body);
PROPERTY_SET_TEXT_BODY.setRequestedBodyType(BodyType.Text);

Related

How can I prevent the automatic decoding of RequestParam Strings in a Spring controller

I have an HTTP GET request controller endpoint where I take in a fileName as a query param and pass that on to another service. For this request the param the filename could include any sort of special characters and I would like to keep these values encoded when passing them on. 2 Characters that have been causing issues are spaces (%20) and +(%2B).
How can I keep these characters encoded in the request params.
So far I have tried using the #RequestParam annotation as well as retrieving the params via HttpServletRequest.getParameterValues(String) but both return the decoded values as spaces.
Any help is appreciated thanks!
Yes, these are automatically decoded by the servlet API. You should be able to re-encode them -
encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
I found out that I could get the actual value passed in by using the HttpServletRequest.getQueryString() method. Parsing this query string I was able to get the un-decoded version of the fileName being passed in. I hope this helps someone in the future.

Receive Email address instead of Name with POI from *.msg-File [Java]

i wrote a small java programm which extract the names,email address, subject, bodytext from a *.msg by using POI 3.15 and writes it to an excel sheet.
By reading the MAPIMessage API Documentation i saw:
getDisplayFrom() --> Gets the display value of the "FROM" line of the outlook message This is not the actual address that was sent from but the formated display of the user name.
Now i would like to get the email address from sender instead of his stored nickname.
Just btw - for receiving the Emailaddress of all "to"-persons you can use getRecipientEmailAddress().
Any suggestions how to deal with it?
thanks in advance
Edit:
I just noticed you can use the first element of getHeaders() to get the Return-Path - which is the emailaddress of "from". kinda dirty way ... so my question is still up to be answered ;)
I don't know in previous versions, but in 3.17 you can get it from main chunks.
MAPIMessage msg = new MAPIMessage("email.msg");
Chunks mainChunks = msg.getMainChunks();
StringChunk emailFromChunk = mainChunks.getEmailFromChunk();
String emailFrom = emailFromChunk.getValue();

How can I get the body of a message using JavaMail?

Is there anyway to read in the body of a message using JavaMail? I'm able to read the body, but when I convert it to a string and print it, it's in HTML format. I just need the text. Obviously, I could use JSoup to parse it. But is there any other easier, quicker way to do it?
Thanks.
If stackoverflow would let me give a simple answer, it would be "no".
you can put this method to remove html tag from string
//html = mail Content message
public static String removeHtmlTag(String html){
return html.replaceAll("\\<.*?>","");
}
there are 4 type of body message of mail
TEXT/HTML / TEXT/PLAIN
2.MULTIPART/ALTERNATIVE
3.MULTIPART/RELATED
4.MULTIPART/MIXED
if you have simple text message in body
use 1. type in TEXT/HTML OR TEXT/PLAIN
direct check message contentype
String contentType=message.getContentTyp();
if(contentType.equals("text/plain")){
Print Here......
}
You can use IOUtils.toString(..) from Apache Commons.

How do i send HTML email containing dynamic link via JAVA

I am trying to send a dynamic link via email with the following code.
Message messageSSL = new MimeMessage(session);
int hash=1000;
String content="click here";
messageSSL.setContent(content, "text/html");
However, i have failed to generate a dynamic link.The output in the mail is in the plain text format.
Output (In the mail):
click here
Even though, the following code works and generates a link called "click here".
String content="click here";
Thanks!!
I think the problem is with backward slash. We should be using forward slash in urls. Please change and try it.
The Apache Commons Email library has some useful classes that take care of the low-level details for stuff like making HTML email work properly. Check it out:
http://commons.apache.org/proper/commons-email/
can you please enclose the link by html tag and try once.
String content="<html><body>click here </body></html>";
i am using same library and working fine for me.
please check below thread
How Can I put a HTML link Inside an email body?

Play Framework - receiving email through SendGrid - character encoding of email body

I am developing a small mail client in the Java Play Framework and I'm using SendGrid for the e-mails. When an e-mail is received, it gets posted to a url and I then parse the posted form using JsonNode. Now the problem is the "to", "from", "subject" fields of that form are automatically converted by SendGrid to UTF-8. Now comes the problem: apparently, the email message body is encoded in "ISO-8859-1". And I need to convert that String to "UTF-8". I already tried several ways of doing so, but most probably I'm doing something very wrong, since I always get strange characters for French or German words containing accents/umlauts (Example "Zürich" comes out as "Z?rich". The code I'm using for the conversion is the following:
byte[] msg = message.getBytes("ISO-8859-1");
byte[] msg_utf8 = new String(msg, "ISO-8859-1").getBytes("UTF-8");
message = new String(msg_utf8, "UTF-8");
Could you, please, suggest a solution? Thank you very much in advance!
Ok so I managed to get the raw byte request from SendGrid using the annotation and created the java String with the correct encodings:
#BodyParser.Of(BodyParser.Raw.class)
public static Result getmail() {
...
}
Now the problem is that for retrieving the file attachments from the request I would need the request to be parsed as MultipartFormData. With the annotation above set, I get a NullPointerException when calling, which was predictable:
request().body().asMultipartFormData().getFiles()
Does any of you have any idea on how I could get the same request again, but parsed with the #BodyParser.Of(Bodyparser.MultipartFormData.class) ? So I kind of need to combine the two annotations or find a way to convert the byte[] I get from the Raw parser to a MultiFormData. Thanks!

Categories