reusable java class to send email through pop3
Code:
/*
* Mail.java
*
* Created on January 1, 2003, 1:58 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ocricket.bean;
import java.util.Date;
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;
/**
*
* @author Dheeraj
*/
public class Mail {
public void sendMail(String toemail, String subject, String body)
throws MessagingException {
String host = "mail.ofindia.in";
String user = "ocricket@ofindia.in";
String pass = "86qx3@i48fg6";
// Create properties, get Session
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.socketFactory.fallback", "false");
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(user));
InternetAddress[] address = { new InternetAddress(toemail) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
}