In: Computer Science
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()); }
}
}
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();
}
}
}