How to build Mailing Application in JAVA
The JavaMail API provides a platform-independent and
protocol-independent framework to build mail and messaging applications. To send an e-mail using
your Java Application is simple enough but to start with you should haveJavaMail
API and Java Activation
Framework (JAF) installed on your machine.
You need to add mail.jar and activation.jar files in your
CLASSPATH.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email
ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID
needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending
email from localhost
String host = "localhost";
// Get system
properties
Properties properties = System.getProperties();
// Setup mail
server
properties.setProperty("mail.smtp.host", host);
// Get the default Session
object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default
MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header
field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header
field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header
field
message.setSubject("This is the
Subject Line!");
// Now set the actual
message
message.setText("This is actual
message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Compile and run this program
to send a simple e-mail:
$ java SendEmail
Sent message successfully....
If you
want to send an e-mail to multiple recipients then
following methods would be used to specify multiple e-mail IDs:
void addRecipients(Message.RecipientType type,
Address[] addresses)
throws MessagingException
Here
is the description of the parameters:
·
type: This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO
·
addresses: This is the array of email ID. You would need to use
InternetAddress() method while specifying email IDs
User Authentication
Part:
If
it is required to provide user ID and Password to the e-mail server for
authentication purpose then you can set these properties as follows:
·
props.setProperty("mail.user", "myuser");
·
props.setProperty("mail.password", "mypwd");
JavaMail API – Sending Email Via Gmail
SMTP Example
Here
are two examples to show you how to use JavaMail API method to send an email via Gmail SMTP server, using both TLS and SSL
connection.
To run
this example, you need two dependency libraries
–
javaee.jar
and mail.jar
, both
are bundle in
JavaEE SDK.Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
1. JavaMail – GMail via TLS
Send an Email via Gmail SMTP
server using TLS connection.
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "username@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
2. JavaMail – GMail via SSL
Send an Email via Gmail SMTP
server using SSL connection.
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@no-spam.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
No comments:
Post a Comment