N. Achyut csit,java,Network Programming SMTP Program in java |Send mail using java Program

SMTP Program in java |Send mail using java Program

Sending Mail in Java

SMTP Program in java / Send mail using java Program / code in java / bsc csit notes/ SMTP program / java program/ smtp server in nepal/

To send e-mail using java program, we use SMTP specification (RFC 821)

Open a socket to your host using SMTP server of your internet provider
smtp.vianet.com.np,
smtp.ntc.net.np,
smtp.wlink.com.np
etc to send email and port 25

To send emails using Java, you need three things:

  • JavaMail API
  • Java Activation Framework (JAF)
  • Your SMTP server details

Send the following information to print stream

HELO sending-host
MAIL FROM:
RCPT TO:
DATA
mail message
(any number of lines)
.
QUIT

Complete SMTP Program in java :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package smtp;
import java.io.*;
import java.util.*;
import java.net.*;
/**
 *
 * @author achyut1324
 */
public class Smtp {

    public static void main(String[] args) throws IOException {
        Email email = new Email(
                "achyut1324@gmail.com",
                "achyut1324@outlook.com",
                "Test email."
        );
        email.send();
    }
}

class Email {

    private Scanner in = null;
    private PrintWriter out = null;
    private final String SMTP_SERVER = "smtp.wlink.com.np";
    private final int SMTP_PORT = 25;
    private String from = null;
    private String to = null;
    private String message = null;

    public Email(String from, String to, String message) {
        this.from = from;
        this.to = to;
        this.message = message;

    }

    private void send(String s) throws IOException {
        System.out.println(">> " + s);
        out.print(s.replaceAll("n", "rn"));
        out.print("rn");
        out.flush();
    }

    private void receive() throws IOException {
        String line = in.nextLine();
        System.out.println(" " + line);
    }

    public void send() throws IOException {
        Socket socket = new Socket(SMTP_SERVER, SMTP_PORT);
        in = new Scanner(socket.getInputStream());
        out = new PrintWriter(socket.getOutputStream(), true);
        String hostName
                = InetAddress.getLocalHost().getHostName();

        receive();
        send("HELO " + hostName);
        receive();
        send("MAIL FROM: <" + from + ">");
        receive();
        send("RCPT TO: <" + to + ">");
        receive();
        send("DATA");
        receive();
        send(message);
        send(".");
        receive();
        socket.close();
    }
}

Output of the Program :

Related Post

Adding your Project to GitHub using CMD prompt or GitBase||PUSH operation for GitHub||Best solution || COLZLIFEAdding your Project to GitHub using CMD prompt or GitBase||PUSH operation for GitHub||Best solution || COLZLIFE

Adding your Project to GitHub using CMD prompt or GitBase/PUSH operation for GitHub/Best solution GitHub, Inc. is a multinational US-based organization that utilizes Git to offer storage and software product

Read MoreRead More