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.
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 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()
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 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 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...
How to write IO program in java?
How to write IO program in java?
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...
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT