Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java program (the image got cut off so here is the full code:) import java.io.*;

ID: 3586366 • Letter: J

Question

Java program


(the image got cut off so here is the full code:)

import java.io.*;

import java.net.*;

public class EmailSender

{

public static void main(String[] args) throws Exception

{

// Establish a TCP connection with the G mail server.

Code Goes Here

// Create a BufferedReader to read a line at a time.

InputStream is = socket.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

// Read greeting from the server.

String response = br.readLine();

System.out.println(response);

if (!response.startsWith("220")) {

throw new Exception("220 reply not received from server.");

}

// Get a reference to the socket's output stream.

OutputStream os = socket.getOutputStream();

// Send HELO command and get server response.

String command = "HELO x ";

System.out.print(command);

os.write(command.getBytes("US-ASCII"));

response = br.readLine();

System.out.println(response);

if (!response.startsWith("250")) {

throw new Exception("250 reply not received from server.");

}

// Send MAIL FROM command.

Code Goes Here

// Send RCPT TO command.

Code Goes Here

// Send DATA command.

Code Goes Here

// Send message data.

//(Do not forget to include header lines, e.g., To: From: Subject:

//Please send me your section num(03 or 09), YOUR _NAME, and your Project Topic or Title.

Code Goes Here

// End with line with a single period.

Code Goes Here

// Send QUIT command.

Code Goes Here

}

}

Java provides an API for interacting with the Internet mail system, which is called JavaMail. However, we will not be using this API, because it hides the details of SMTP and socket programming. Instead, you should write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message *Create a Java file named EmailSender.java. Below is the shell of the code, but you will need to read the step before using code as it contains important server and client information Mai Server: gmail-smtp-in.lgoogle.com Port 25 From: youremailnortheastern.e du To: yourgamil@gmail.com This means you wil include in your code the details of the particular email message you are trying to send. Here is a skeleton of code you'l need to write: import java io.*. import java.net.*. public class EmailSender public static void main(String[] args) throws Exception / Establish a TCP connection with the Gmail server Code Goes Here // Create a BufferedReader to read a line at a time InputStream is = socket.getlnputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br new BufferedReader(isr);

Explanation / Answer

Please check the below code for SMTP mail sending

package com.shop;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SMTPDemo {

public static void main(String args[]) throws IOException,
UnknownHostException {
String msgFile = "file.txt";
String from = "yourmail";
String to = "destination mail";
String mailHost = "host name";
SMTP mail = new SMTP(mailHost);
if (mail != null) {
if (mail.send(new FileReader(msgFile), from, to)) {
System.out.println("Mail sent.");
} else {
System.out.println("Connect to SMTP server failed!");
}
}
System.out.println("Done.");
}

static class SMTP {
private final static int SMTP_PORT = 25;

InetAddress mailHost;

InetAddress localhost;

BufferedReader in;

PrintWriter out;

public SMTP(String host) throws UnknownHostException {
mailHost = InetAddress.getByName(host);
localhost = InetAddress.getLocalHost();
System.out.println("mailhost = " + mailHost);
System.out.println("localhost= " + localhost);
System.out.println("SMTP constructor done ");
}

public boolean send(FileReader msgFileReader, String from, String to)
throws IOException {
Socket smtpPipe;
InputStream inn;
OutputStream outt;
BufferedReader msg;
msg = new BufferedReader(msgFileReader);
smtpPipe = new Socket(mailHost, SMTP_PORT);
if (smtpPipe == null) {
return false;
}
inn = smtpPipe.getInputStream();
outt = smtpPipe.getOutputStream();
in = new BufferedReader(new InputStreamReader(inn));
out = new PrintWriter(new OutputStreamWriter(outt), true);
if (inn == null || outt == null) {
System.out.println("Failed to open streams to socket.");
return false;
}
String initialID = in.readLine();
System.out.println(initialID);
System.out.println("HELO " + localhost.getHostName());
out.println("HELO " + localhost.getHostName());
String welcome = in.readLine();
System.out.println(welcome);
System.out.println("MAIL From:<" + from + ">");
out.println("MAIL From:<" + from + ">");
String senderOK = in.readLine();
System.out.println(senderOK);
System.out.println("RCPT TO:<" + to + ">");
out.println("RCPT TO:<" + to + ">");
String recipientOK = in.readLine();
System.out.println(recipientOK);
System.out.println("DATA");
out.println("DATA");
String line;
while ((line = msg.readLine()) != null) {
out.println(line);
}
System.out.println(".");
out.println(".");
String acceptedOK = in.readLine();
System.out.println(acceptedOK);
System.out.println("QUIT");
out.println("QUIT");
return true;
}
}
}


  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote