Handeling session cookies with Java

Let’s say you are creating an HTTP request using Java. You are probably doing it with the use of the URL object like this: But sometimes you will also want to use cookies to send along with you request. To do so first we will learn how to read cookies from the request. Getting the cookies from a URL connection After the function uc.connect(); you are able to get cookies from your request. Here is the result of this example: PREF=ID=f0fd8b4c16931772:TM=1261387922:LM=1261387922:S=LLhLkHSpgfXkqxIe; expires=Wed, 21-Dec-2011 09:32:02 GMT; path=/; domain=.google.co.uk NID=30=C6Hb5SV2-pqYymnnrZmTAprCvDMrFwo6wzg9We7rvPztXmA_fTWxv-GPmDVXPsIBOOkecj1Ms4skmTWT-uUN5iW_-ZYiPUNNJXE8CC44xWVORe8Yyu1jI32XL51tvzCz; expires=Tue, 22-Jun-2010 09:32:02 GMT; path=/; domain=.google.co.uk; HttpOnly Sending HTTP request with cookies: Notice that the value of the property ‘Cookie’ is a string which contains cookies in the format of cookieName=CookieValue with a ‘;’ as a delimiter. Notice you have to set the cookie value before the connect() function. download source

Receive POP3 Emails using Java

Take a look at this example on how to read pop3 emails. But there are 3 parameters you need to prepare: your pop3 server, username and passowrd for that server. Pretty straight forward, not much to explain. All that is missing is the readContent() function which prints the message.getContent(). First you need to understand that an Email message content can be just a regular String or an input stream, but in most cases it is a Multipart object which can be composed of different parts of different types. It can also actually contain inner multiparts. Take a look at readContent() You will also need the mail.jar that can be found here. download source

Send a simple SMTP email message with java

The next code will show you the simplest way to send an email in java. The only thing you need is an email address and the smtp host address of your email supplier. This code can only send a simple formatted text, without HTML or attachments. In the future I will show how to do those as well. You will also need the mail.jar that can be found here. Unless you are using Java 6 you will also need the activation.jar which can be found here. download source