denniskhor -4 Junior Poster in Training
public void actionPerformed (ActionEvent e)
{
String hostServer, fromEmail, fromTo, contentSubject, contentMsg;


if (e.getSource()== btnSend)
{
hostServer=txtHost.getText();
fromEmail=txtFrom.getText();
fromTo=txtTo.getText();
contentSubject=txtSub.getText();
contentMsg=msgArea.getText();


// Establish a TCP connection with the mail server.
Socket soc = new Socket("" + hostServer, 25);


// Create a BufferedReader to read a line at a time.
InputStream is = soc.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 = soc.getOutputStream();


// Send HELO command and get server response.
String command = "HELO Ng\r\n";
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.
String from = "\r\n"+ email.getFromEmail();
System.out.print(from);
os.write(from.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);
if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}


// Send RCPT TO command.
String rcpt = "\r\n"+ email.getFromTo();
System.out.print(rcpt);
os.write(rcpt.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);


if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}


// Send DATA command.
String data = "\r\n"+ email.getContentSubject();
System.out.print(data);
os.write(data.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);


if (!response.startsWith("354"))
{
throw new Exception("354 reply not received from server.");
}


// Send message data.
String subject = "\r\n\r\n"+ email.getContentMsg();
System.out.print(subject);
os.write(subject.getBytes("US-ASCII"));


// End with line with a single period.
String end = ".\r\n";
System.out.print(end);
os.write(end.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);


if (!response.startsWith("250"))
{
throw new Exception("250 reply not received from server.");
}


// Send QUIT command.
String quit = "QUIT\r\n";
System.out.print(quit);
os.write(quit.getBytes("US-ASCII"));
response = br.readLine();
System.out.println(response);


if (!response.startsWith("221"))
{
throw new Exception("221 reply not received from server.");
}



}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.