JavaMail Projects

JavaMail Project 1

How to send a Mail using javaMail API
Previous Home Next

The example shows to how to send a mail with using JavaMail API. In this program the client created the new message by use messaging subclass. In this program set the attributes and the subject. You insert the content into the Message object. After that invoke the Transport.send() method.

package R4R;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.Authenticator;
import javax.mail.Session;
import com.sun.mail.smtp.SMTPTransport;
public class firstmailsend {
public static void send(String smtpHost, int smtpPort,
String from, String to,
String subject, String content)
throws AddressException, MessagingException {
// Create a mail session


Properties props = new Properties(); props.put("", smtpHost); props.put("mail.smtp.port", ""+smtpPort); Session session = Session.getDefaultInstance(props, null); // Construct the message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(content); // Send the message Transport.send(message); } public static void main(String[] args) throws Exception { // Send a test message firstmailsend sender = new firstmailsend(); firstmailsend.send("hostname", 25, "", "", "javamail example ", "How to send first mail by javamail API?"); } System.out.println ("Mail Sending"); }
Output :

Mail sending..............

Previous Home Next