Quick walk through the advanced concepts in Java - Part 3 of series
Chapters
Working with JavaMail API
The JavaMail API provides a protocol-independent and platform-independent framework to build messaging and mail applications. The JavaMail API defines classes which represent components of a mail system. JavaMail does not implement an email server instead it allows accessing an email server using a Java API. JavaMail typically includes support for IMAP,SMTP and POP3. The below can be downloaded from below link:
Download and unzip these files in newly created top level directories.
Set CLASSPATH to add javax.mail.jar.
Send a Simple E-mail using JavaMail API 1.5.2 using SMTP:
A simple e-mail application from machine
import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; public class SMTPExample { private static final String SMTP_HOST_NAME = "localhost"; //or any other SMTP host private static final String SMTP_AUTH_USER = "me@localhost"; private static final String SMTP_AUTH_PWD = "password"; private static final String message = "This is a test email"; private static final String subject = "Email test"; private static final String from = "me@localhost"; // recipients email address private static final String[] recipients = { "you@localhost" }; public static void main(String args[]) throws Exception { SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication(); smtpMailSender.postMail(recipients, subject, message, from); System.out.println("Email has been sent successfully"); } public void postMail(String recipients[], String subject, String message, String from) throws MessagingException, AuthenticationFailedException { boolean debug = false; //Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getDefaultInstance(props, auth); session.setDebug(debug); // Message part Message msg = new MimeMessage(session); // spcifiy who from this email will be sent from InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom);
// set who to this email will be sent to InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i & lt; recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); } private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { String username = SMTP_AUTH_USER; String password = SMTP_AUTH_PWD; return new PasswordAuthentication(username, password); } } }
You are also able to send HTML emails or emails with attachments. More on JavaMail can be found on java.net JavaMail project page and the API pages
Description
This is the last part in our tutorial series on Java. This tutorial is designed as a quick walk through the advanced concepts of Java Languages. This tutorial is subdivided into few chapters as shown below
- Data Structures
- Collections
- Generics
- Serialization
- Networking
- Working with Emails
- Multithreading
- Getting started with Applets
- JavaDoc
Let us know if you find any issues with this tutorial. Also, if you can provide us with your feedback, that always help us improve.
Prerequisites
You must have read Part 1 and Part 2 of our tutorial series on Java.
Audience
Beginners or students looking to brush up their Java knowledge
Learning Objectives
To get an understanding on some of the advanced topics of Java.
Author: Subject Coach
Added on: 10th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!