JavaMail - could not get body of text/plain message - jakarta-mail

I am writing a pop3 client using JavaMail to retrieve mails. But, I could not retrieve the body of the text/plain encoded mail messages.
The mail.getContent() is not fetching the body of the message. But mail.writeTo(System.out) is printing the complete mail data including the envelope information.
Is there any way to fetch only the body of the mail message?
Thanks!
Update: From the debug watch, I can see that mail.getContent() is only returning "\r\n".
Solution:
Seems like the problem is with mails sent directly from telnet session. The body of the mail is fetched propertly for mails sent from a smtp client program.

JavaMail is expecting a blank line (CRLF CRLF) to terminate the header block. When you enter the message via telnet, you're sending your system newline (which is probably just LF) at the end of each line you enter. So my guess is that JavaMail doesn't see the header/body separator it's expecting, and thus it's just tacking your "body" lines onto the end of its header block as malformed headers.

Related

Blank email is sent with Send Email connector in Mule 4

I am using Send Email connector (SMTP configuration) to send email to gmail. It is observed that sometimes email is sent with blank body. Though in the mule log I am able to see that the email body is constructed correctly just before the Send Email connector, but the email sent is having blank body.
Even though this issue is occurring very rarely but would like to know how we can avoid such situation and is there way to handle this situation? Also, what could cause this issue?
Thanks in advance
There could be two (and more) possibilities:
Your payload being json is not getting read correctly, try payload.^raw when working with email body json. Had to do this when sending a multipart/form-data containing json content for email service
Having said this is a rare scenario it could be possible that your error flow is itself throwing an error ( either while building the body / while calling email end-point) which throws off the body and in the 2nd flow error type being changed without a body the email is sent across? Check your flows make sure http calls are surrounded with Untill-successful scope..
With more info, we could dive deep. What do you think ?

Put/Post JSON message to Azure Storage Queue via Logic App

I want to be able to use Logic Apps to put/post messages in an Azure Storage Queue, because I want to make use of the Managed Identity option that HTTP Logic App acion provides.
I have a Logic App that uses HTTP action to post XML messages to the queue and I have a "Put a message on a queue" action that puts JSON message to the queue for debugging purposes.
My ultimate goal is to be able to use the HTTP action with Managed Identity as Authentication but be able to post JSON messages to the queue like the "Put a message on a queue" action is able to.
You can certainly send JSON as message body. In fact you can send any text. You just have to ensure that the text you're sending as message body must be XML safe e.g. replace < with < etc. Generally Base64 encoded string messages are sent to ensure this.
From the REST API documentation:
A message must be in a format that can be included in an XML request
with UTF-8 encoding. To include markup in the message, the contents of
the message must either be XML-escaped or Base64-encode. Any XML
markup in the message that is not escaped or encoded will be removed
before the message is added to the queue.
Here is what worked for me:
Enabled "Managed Identity" on the Logic App.
Added Storage-Queue-Contributor permissions on the storage queue.
Used utcnow('R') to get this date format ("Tue, 08 Sep 2020 12:03:08 GMT")
for x-ms-date HTTP header (no doc from MS about this).
Inserted JSON data inside
<QueueMessage>
<MessageText>
{
"car": "Audi",
"year": 1983
}
</MessageText>
</QueueMessage>
Final result in Logic App designer:

Filling in a webform using Arduino and HTTP

I'm trying to get an arduino to login in to a website that I have created.
On the website there is a basic form that has two fields one for password and one for username, it also has a submit button labelled login.
I used fiddler2 to sniff the http packets when I login in using chrome and am trying to use the information from that http post to recreate my own post to login.
Here is the portion of the code that I am using for the login:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("POST/username=slwhore&passwd=1234qwer%21&op2=login&lang=english&force_session=1&return=B%3AaHR0cDovL3JlbW90bGV0LmNvbS8%3D&message=0&loginfrom=loginmodule&cbsecuritym3=cbm_56b7d5e7_00583e07_b0b6f81b4c86d117542f5cc7b7c3416e&Submit=Login HTTP/1.1");
client.println("Host:www.remotlet.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: 229");
client.println("Connection: close");
client.println();
I then have another piece of code that recieves the information coming back from the host which I know works. When I run this code I am able to connect to the server but I don't get any response form it at all. Any help would be greatly appreciated.
Your HTTP request is completely wrong, it will never ever be accepted by any kind of server web.
1st line: the HTTP 1st line is METHOD URI VERSION.
You didn't put a space between the method and the URI, also the POST data is not part of the URI as it is when using GET requests. I don't know what your server uses but usually sane logins don't use GET and don't pass the login inside the URI.
2nd line: you forgot a space
4th line: you set a content length but you don't send any content apparently.
General consideration: in HTTP the line terminator is \r\n, not just \n.
I suggest you do the request with the browser, intercept the traffic with wireshark and see how it's done.

how http server received file from client

I have written code for sending client user passwords to an HTTP server for verification via HTTP. I generate the query string (containing usr, pwd) and send the request to the server. That works.
But now I have to send a file (text/xml) to that server. I don't know how it can be done.
Do we have to write some code on the server or only in the client?
What are the mechanisms on the server for accepting file and on the client for sending files?
The HTTP protocol is really simple, actually:
the client sends a line containing HTTP method name, URL and protocol version
the client sends an RFC822 header containing request parameters and, if a data block follows, details about the data block.
the client sends the data block
the server sends a line containing protocol version, status code and message
the server sends an RFC822 header containing response parameters and, if a data block follows (or the client performed a HEAD request), details about the data block
the server sends the data block, unless the method is HEAD.
the connection is either torn down, or the protocol restarted.
Typically, servers will understand at least these methods:
GET (client does not send data block, server sends data block)
HEAD (same as GET, but server omits response data block)
POST (client sends data block, server responds with data block)
PUT (client sends data block, server does not send data block)
There is some implied semantics in the choice of method, in that GET requests never modify server state and their results may be cached and reused (which is what allows the browser to go back and forth between pages), while POST requests do change server state -- incidentally, this is what you do when you upload a file.
So, in order to send a file, prepare a POST or PUT request (depending on whether you expect a reply document, or if a simple acknowledgement status code is sufficient), which consists of the request line, the headers containing extra protocol info ("Host:", "User-Agent:", ...), the headers describing the file ("Content-Type:", "Content-Length:", ...), an empty line, and the file contents, and send that over a TCP connection, then read back the status line, the response headers and the response file (if you asked for one).
It depends on the server's application how to load the file.
You may need to send the file using the HTTP "POST" method instead of "GET".
POST /your_uri HTTP/1.1
Host: www.yourhost.com
Content-type: application/x-www-form-urlencoded
Content-length: 41
filename=test.xml&data=yoururlencodeddata
The server application may expect files encoded with "multipart/form-data" boundaries, something like that:
Content-type: multipart/form-data, boundary=AaBb01x
--AaBb01x
content-disposition: form-data; name="yourfield"
Your field data
--AaBb01x
content-disposition: form-data; name="yourfilefield"; filename="filename.xml"
Content-Type: text/xml
<root>your xml data</root>
--AaBb01x
If its a XML file it is easy.
You can add Content-Type: text/xml in the HTTP header and append the XML file data after the \r\n\r\n of the HTTP header and send it via the socket to the webserver.
The webserver will understand from the HTTP header that it contains XML file and takes the file. In the case of a bnary file, you will need to convert it to base64.
For example I have used a buffer to store the http request. Now if you send this buffer to the socket connected to the webserver, the FileName.xml will be saved in the webserver. For this to work the upload.php has to able to work with POST data.
The boundary is to show the boundary between the data and is needed by the HTTP protocol. It can be any random generated number and make sure the start boundary and the close boundary numbers are equal. Also content length is the length of the file.
`
char buf[2048] = "POST http://www.nameofyoursite.com/upload.php HTTP/1.1\r\n"
"Host: www.nameofyoursite.com\r\n"
"Content-Type: multipart-form-data, boundary=1234567\r\n"
"Content-Length: 15\r\n\r\n"
"--1234567\r\n"
"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"FileName.xml\"\r\n"
"Content-Type: text/xml\r\n"
"<xml>This is a test</xml>\r\n"
"--1234567--\r\n";
`

C webserver header sending

I wrote a simple web server in C that listens for a connection and send some text over a socket.
I want my server to be accessed by a server. In this case I should s*end headers such as CODE(200), Content type, Content length*.
How these headers can be sent to the client(browser or telnet)?
How headers of request can be extracted?
(Maybe, I just don't understand the question, vote me down.)
The http response header format is not too difficult:
header1\n
header2\n
<empty line>\n
content\n
content\n
That's all. The webserver should send the headers, an empty line, then the content.
If you wanna test header sending, you should check it with a browser. Add a line to your webserver to send the following header:
Content-Disposition: attachment; filename=download_me.txt
So when you connect to your webserver with a browser, it will pop up a "save as" dialog instead of displaying the downloaded web page in the browser window. If you got the dialog, and the downloaded file is also OK, your webserver sends the headers properly.
G
It's another issue, wether the client handles them as you (and W3C) expect...
Edit: HTTP ok status is:
HTTP/1.1 200 OK\n
Google for more status codes.

Resources