Question

In: Computer Science

Write a Java program that establishes a TCP connection with a mail server through the socket...

Write a Java program that establishes a TCP connection with a mail server through the socket interface, and sends an email message. Your program sends SMTP commands to local mail server, receives and processes SMTP commands from local mail server. It sends email message to one recipient at a time. The email message contains a typical message header and a message body.

Here is a skeleton of the code you'll need to write:

import java.io.*;
import java.net.*;

public class EmailSender
{
   public static void main(String[] args) throws Exception
   {
      // Create a socket which connect to Server (HOSTNAME, PORTNUM)
        String Hostname = "___________________";
        int Port = _______;
        Socket emailClient;

        try {
            emailClient = new Socket(Hostname, Port);

            //InputStream & outputStream
            Scanner inFromServer = new Scanner(emailClient.getInputStream());
            DataOutputStream outToServer = new DataOutputStream(emailClient.getOutputStream());

            // Read greeting from the server.
            String response = inFromServer.nextLine();
            System.out.println(response);
            if (!response.startsWith("220")) {
                throw new Exception("220 reply not received from server.");
            }

            // Send HELO command and get server response.
            String command = "HELO usca.edu\r\n";
            System.out.print(command);
            outToServer.writeBytes(command);
            response = inFromServer.nextLine();
            System.out.println(response);
            if (!response.startsWith("250")) {
                throw new Exception("250 reply not received from server.");
            }


      // Send MAIL FROM command.
      

      // Send RCPT TO command.
      

      // Send DATA command.
      

      // Send message data.
      
      // End with line with a single period.
      

      // Send QUIT command.


     // Close the connection
            emailClient.close();

        } catch (Exception e) {
            System.err.print("Caught Exception" + e.getMessage());
        }

}

}

Solutions

Expert Solution

you can use java mail api for this which will be done easily in this way

code:

/*
* 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.
*/

/**
*
* @author Deepak
*/
/*
* 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.
*/

/**
*
* @author Deepak
*/
// Java program to send email

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.Session;
import javax.mail.Transport;


public class client
{

public static void main(String [] args)
{
   // email ID of Recipient.
   String recipient = "[email protected]";
   // email ID of Sender.
   String sender = "[email protected]";
   // using host as localhost
   String host = "127.0.0.1";
   // Getting system properties
   Properties properties = System.getProperties();
   // Setting up mail server
   properties.setProperty("mail.smtp.host", host);
   // creating session object to get properties
   Session session = Session.getDefaultInstance(properties);
   try
   {  
       MimeMessage message = new MimeMessage(session);
       message.setFrom(new InternetAddress(sender));
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
       // Set Subject
       message.setSubject("Subject");
       // body
       message.setText("Hello i mailed you");
       // Send
       Transport.send(message);
       System.out.println("Sent successful");
   }
   catch (MessagingException e)
   {
       e.printStackTrace();
   }
}
}


Related Solutions

Write two python codes for a TCP client and a server using a Socket/Server Socket. The...
Write two python codes for a TCP client and a server using a Socket/Server Socket. The client sends a file name to the server. The server checks if there is any integer value in the file content. If there is an integer number, the server will send a message to the client “Integer exists” otherwise, the message “Free of Integers”
Socket-based Java server program
Write a Socket-based Java server program that responds to client messages as follows: When it receives a message from a client, it simply converts all the letters into ASCII characters and sends back them to the client. Write both client and server programs demonstrating this.
write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket...
write a skeleton Python TCP servers as follows: Multi-threaded concurrent TCP server using Python’s low-level socket module and Python’s threading library Python sockets API: s = socket(), s.bind(), s.listen(), remote_socket, remote_addr = s.accept() Python threading API: thread = threading.Thread(target, args, daemon), thread.start()
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
Write a program to implement a distributed chat server using TCP sockets in ‘C’.
Write a Python Client/Server Socket Program that will allow you to send text messages between the...
Write a Python Client/Server Socket Program that will allow you to send text messages between the server and client, terminating when the exit is typed on the client. Build the program on your 2-VM’s and get it working. Cut and paste your code below along with screen shots of your program working.
Write and test a python program to demonstrate TCP server and client connections. The client sends...
Write and test a python program to demonstrate TCP server and client connections. The client sends a Message containing the 10 float numbers (FloatNumber[]) , and then server replies with a message containing maximum, minimum, and average of these numbers.
TCP Wireshark Lab – Working with a remote server.  You will go through the steps below, use...
TCP Wireshark Lab – Working with a remote server.  You will go through the steps below, use your captured wireshark file and the provided wireshark file (on D2L) to answer the questions.  When you have finished the lab you will submit the following: This document with your answers provided in the appropriate places.   Your wireshark capture file as a zipped file.   STEPS: 1. Start up your web browser. Go thehttp://gaia.cs.umass.edu/wireshark-labs/alice.txtand retrieve an ASCII copy of Alice in Wonderland. Store this file somewhere...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT