In JAVA Platform Enterprise Edition, javax.activation, and javax.mail packages has APIs to send or receive EMAIL. javax.mail.internet.InternetAddress class helps us to construct Address from String object.
Variety of properties which used to control the flow, monitor the flow, per session based properties and global properties for JAVA Mail. It is very tedious to remember all those properties. However, JAVA Doc is nicely written to list all those properties with brief explanation. Mail Session Properties | MIME properties | sMTP Properties
Mail Send
In the following sample, mail server is running with default port mail.smtp.port:25 in same machine, where this program is get executed. If no Mail server is configured to run in local, then we can configure to point thirdparty mailserver like gmail, hotmail, or rediffmail.
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailTest {
private static Session session = null;
public static void main(String[] args) throws Exception {
sendMail("krishna.oracleb2b@gmail.com", "abc@abc.com",
"abc1@abc.com,abc2@abc.com", "Test mail",
"Hello All, this is test message", true);
}
public static void sendMail(String sFrom, String sTo, String sBcc,
String sSubject, String sBody, boolean sTextBody) throws Exception {
try {
Message message = new MimeMessage(getMailSession());
message.setFrom(new InternetAddress(sFrom));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(sTo);
message.setRecipients(javax.mail.Message.RecipientType.TO, to);
if (sBcc.length() > 0) {
message.setRecipients(javax.mail.Message.RecipientType.BCC,
InternetAddress.parse(sBcc));
}
message.setSubject(sSubject);
if (sTextBody)
message.setContent(sBody, "text/plain");
else
message.setContent(sBody, "text/html");
Transport.send(message);
} catch (MessagingException m) {
m.printStackTrace();
// throw new ShirtCommonException(m);
} catch (Exception m) {
m.printStackTrace();
}
}
/**
* getMail session
*
* @return
*/
private static Session getMailSession() {
if (session == null) {
// Get system properties
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", "localhost");
// Get session
session = Session.getDefaultInstance(props, null);
session.setDebug(true);
}
return session;
}
}
In the above example, no security details used to send the message. Some server mandates user has to create session with username and password. And even more with SSL enable socket needs to be used to connect. In these cases, Transport has to be instantiated with proper SMTP security properties.Here is the sample for username-password authentication
Transport tr = session.getTransport("smtp");
tr.connect(smtphost, username, password);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
Mail Read
To read mail from MailServer, Mailserver has to support any of the mail reading protocol pop3, imap. Based on this, we have to create instance of javax.mail.Store and get the folder which ever we wants to operate on.
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
public class MaieReceivelTest {
private static Session session = null;
public static void main(String[] args) throws Exception {
readMail();
}
public static void readMail() throws Exception {
try {
Store store = getMailSession().getStore();
store.connect("localhost", "username", "password");
Folder folder = store.getFolder("Inbox");
System.out.println(folder.getMessageCount());
Message[] msgs = folder.getMessages();
if (null != msgs) {
for (Message msg : msgs) {
InputStream is = msg.getDataHandler().getInputStream();
if (is != null) {
byte[] bytes = new byte[is.available()];
is.read(bytes);
System.out.println(new String(bytes));
} else {
Multipart mp = (Multipart) msg.getDataHandler()
.getContent();
is = mp.getBodyPart(0).getInputStream();
byte[] bytes = new byte[is.available()];
is.read(bytes);
System.out.println(new String(bytes));
}
}
}
} catch (MessagingException m) {
m.printStackTrace();
// throw new ShirtCommonException(m);
} catch (Exception m) {
m.printStackTrace();
}
}
/**
* getMail session
*
* @return
*/
private static Session getMailSession() {
if (session == null) {
// Get system properties
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", "localhost");
// mail.smtp.user
// Get session
session = Session.getDefaultInstance(props, null);
session.setDebug(true);
}
return session;
}
}
No comments:
Post a Comment