Friday, May 1, 2015

How to send the mail in Java.

Sending mail in java is very easy just follow the below stpes-


Step 1- Create a property with smtp host, smtp port. Here I’m going the send the mail using gmail so I’m using smtp host as smtp.gmail.com and port is 465.


Add activation.jar and mail.jar

Note: This smtp host and port is varying with mail server.

1
2
3
4
5
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", "465");

Step 2- Create Authenticator object (using your gmail id and password) this is mandatory for gmail authentication.

1
Authenticator auth = new SMTPAuthenticator();

1
2
3
4
5
private class SMTPAuthenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("ashishkumarsinghmca@gmail.com", "yourpassword");
    }
}

Step 3- Create a session using above property and authentication objects.

1
Session session = Session.getInstance(props, auth);

Step 4- Create a message using MimeMessage object and set the required property like subject, from address, to address and message.

1
2
3
4
MimeMessage msg = new MimeMessage(session);
msg.setSubject("this is test mail subject");
msg.setFrom(new InternetAddress("ashishkumarsinghmca@gmail.com"));            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("ashishkumarsinghmca@gmail.com"));
msg.setText("this is body of mail");

Step 5- Send the message using send() method (static method of Transport class) by passing message object as parameter.

1
Transport.send(msg);
Example :

SendMail.java 

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.mymail;
import java.util.Properties;
import javax.mail.Authenticator;
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 SendMail{

    SendMail () {
        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", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        try {
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            MimeMessage msg = new MimeMessage(session);
            msg.setSubject("this is test mail subject");
            msg.setFrom(new InternetAddress("ashishkumarsinghmca@gmail.com"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("ashishkumarsinghmca@gmail.com"));
            msg.setText("this is body of mail");
            Transport.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("ashishkumarsinghmca@gmail.com", "yourpassword");
        }
    }

    public static void main(String[] args) {
        SendMail mail = new SendMail ();
        System.out.println("mail send!");
    }
}

No comments :

Post a Comment