Monday, April 03, 2006

Sending Mails.... With Java..

Here I Come with the Java Code Which can Be used to mail other persons.. Earlier i had written about Oracle Procedure which can send mails. The Java Code which I have made is quite simple and can be easily understood. The code can be extended to incorporate features such as Attaching files. All One has to have is an SMTP host which will send the mail.

People can look for JavaDoc for more Details..

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMailTry {

public void sendMessage() throws AddressException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "SMTP Host Server Name Here");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("rakesh_xav@rediffmail.com"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("rakesh_xav@yahoo.co.in"));
msg.setSubject("Testing....");
msg.setSentDate(new Date());
msg.setText("Hi All,\nTrying for a Test Mail\n\nRegards,\nRakesh");
Transport.send(msg);
}
public static void main(String[] args) {
SendMailTry sendMail = new SendMailTry();
try {
sendMail.sendMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Java 10 Features

My blog post in JournalDev for Java 10 Features