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

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 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.
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
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a JAVA program read a number within the range of 1 through 1000 to display...
Write a JAVA program read a number within the range of 1 through 1000 to display the Roman numeral version of that number. Include screenshot of the output
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session. The server should support multiple clients (use telnet as the client in this task). Compile and run the server program. Try and connect to it from multiple other hosts using telnet as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT