How to Send an Email in JDK 1.6

The JDK libraries offer a class called Java Mail. The class lets you send an email using the Java language. You must specify your outgoing email server and any pertinent information for the email such as recipient, sender and the content. The Java classes take care of the rest.

Instructions

    • 1

      Open the Java editor and open the source code file you want to use to send the email. Add the following code to the top of the file to include the Java mail libraries:

      import javax.mail.*;
      import javax.mail.internet.*;

    • 2

      Instantiate the class to use the mail functions. The following code sets up the class, so you can send email:

      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.jcom.net");

      Replace the "smtp.jcom.net" with the name of your outgoing SMTP server..

    • 3

      Set up the recipient, sender and the body of your message. Use the following template to set up the outgoing email:

      Message msg = new MimeMessage("Body of your content");
      InternetAddress from = new InternetAddress("you@domain.com");
      msg.setFrom(from);
      InternetAddress to = new InternetAddress("recipient@domain.com");
      msg.setRecipients(to, from);
      msg.setSubject("The subject of the email");
      msg.setContent(message, "text/plain");

    • 4

      Send the email. The following code sends the email to your recipient:

      Transport.send(msg);

Related Searches:

References

Comments

Related Ads

Featured