Wednesday, June 17, 2009 1 comments

Simple Java Mail SMTP Client

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

class SimpleMail {
static Message emailMessage;

public static void main(String[] args) {

try {

emailMessage = new MimeMessage(createSession());
// replace this with the TO mail address
InternetAddress add = new InternetAddress("receipeicnt@abc.com");
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress("d");
emailMessage.setFrom(add);
emailMessage.setRecipients(javax.mail.Message.RecipientType.TO,
addresses);
emailMessage.setSubject("My Subject ");
emailMessage.setContent("My First Mail", "text/plain");
emailMessage.saveChanges();

Transport.send(emailMessage);

} catch (AddressException e) {

e.printStackTrace();
} catch (MessagingException e) {

e.printStackTrace();
}

}

private static Session createSession() {

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "your_mail_server_IP");

// If your sending multiple mails and even if one mail fails this by
// setting
// this property all the other mails except that mail will still be
// delievered
props.put("mail.smtp.sendpartial", "true");

Authenticator authenticator = null;

props.put("mail.smtp.auth", "true");
// enter your mail address and password here
authenticator = new MessagingAuthenticator("xxx@abc.com", "password");

return Session.getDefaultInstance(props, authenticator);
}
}

class MessagingAuthenticator extends Authenticator {
private String userName;
private String password;

public MessagingAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
2 comments

Simple Java Mail Pop 3 client

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

public class POP3Client {

public static void main(String args[]) throws Exception {
String host = "your_mail_server_ip";

// Get system properties
Properties props = System.getProperties();


Session session = Session.getDefaultInstance(props, null);

// Get the store
Store store;
store = session.getStore("pop3");
store.connect(host, "xxx@abc.com", "password");

// Get inbox folder

Folder folder = store.getDefaultFolder().getFolder("INBOX");
folder.open(Folder.READ_WRITE);


// Get directory
Message message[] = folder.getMessages();
for (int i = 0, n = message.length; i < n; i++) {

System.out.println(i + ": Subject" + message[i].getFrom()[0] + "\t"
+ message[i].getSubject());
String content = message[i].getContent();


System.out.print(content);
//This will delete the mail from the inbox after you close the folder
message[i].setFlag(Flags.Flag.DELETED, true);
}

// Close connection
folder.close(true);
store.close();

}

}
0 comments

Small glitch in the Java Mail API when used in the Linux Environment

I was writing a Java Mail POP3 client recently and was reading both multi part and plain text mails, getting the body text and persisting that text to the database. This worked fine when i tested it in the Windows environment. But when we deployed the application in a Linux environment we saw that the database had one extra blank line coming in between each new line. When we debugged this it came down to the point where we get the body text from the message using the getContent() method provided by the Java Mail API. And in this too it was showing a new line in between each line and hence i had to write a regex to get rid of that extra line before persisting to the database.

Hope this helps anyone else who might face this prob in the future.
 
;