Mail Sender
/* This is a very simple java program to send mail */
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;
public class MailSender extends Authenticator {
public static void send(String smtpHost, int smtpPort, String from, String to
String subject, String content) throws AddressException, MessagingException
{
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
props.put( "mail.smtp.auth", "true" );
Session session = Session.getDefaultInstance(props, new MailSender());
// Construct the message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setContent(content,"text/html");
System.out.println(msg.getAllRecipients());
// Send the message
Transport.send(msg);
}
protected PasswordAuthentication getPasswordAuthentication() {
// new PasswordAuthentication( "username", "password" );
return new PasswordAuthentication( "EJB-domain/ssilx54", "nqoh2007" );
}
public static void main(String[] args) throws Exception {
// Send a test message
//send("YOUR smtpHost",port number,from address , to address,subject,content);
send("192.50.51.154", 25, "alfathim@yahoo.co.in", "alfathim@yahoo.co.in", "Hello", "Test msg");
}
}
}
No comments:
Post a Comment