469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

SMTP Email in Java

I'm trying to figure this out with out any Java experience. I'm trying to write a program that will send a SMTP email. I was given a shell and I do know how to establish a connection. My next question is how do I Send MAIL FROM command, Send RCPT TO command, Send DATA command, Send message data and End with line with a single period. Will this be just a matter or printing this information? Someone please help me figure this out.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4.  
  5. public class EmailSender
  6. {
  7.    public static void main(String[] args) throws Exception
  8.    {
  9.       // Establish a TCP connection with the mail server.
  10.  
  11.     Socket TCPConnection = new TCPConnection("mail.bellsouth.net, 25");
  12.  
  13.       // Create a BufferedReader to read a line at a time.
  14.  
  15.       InputStream is = socket.getInputStream();
  16.       InputStreamReader isr = new InputStreamReader(is);
  17.       BufferedReader br = new BufferedReader(isr);
  18.  
  19.       // Read greeting from the server.
  20.  
  21.       String response = br.readLine();
  22.       System.out.println(response);
  23.       if (!response.startsWith("220")) {
  24.          throw new Exception("220 reply not received from server.");
  25.       }
  26.  
  27.       // Get a reference to the socket's output stream.
  28.       OutputStream os = socket.getOutputStream();
  29.  
  30.       // Send HELO command and get server response.
  31.       String command = "HELLO Nikki\r\n";
  32.       System.out.print(command);
  33.       os.write(command.getBytes("US-ASCII"));
  34.       response = br.readLine();
  35.       System.out.println(response);
  36.       if (!response.startsWith("250")) {
  37.          throw new Exception("250 reply not received from server.");
  38.       }
  39.  
  40.       // Send MAIL FROM command.
  41.  
  42.  
  43.       // Send RCPT TO command.
  44.  
  45.  
  46.       // Send DATA command.
  47.  
  48.  
  49.       // Send message data.
  50.  
  51.       // End with line with a single period.
  52.  
  53.  
  54.       // Send QUIT command.
  55.  
  56.    }
  57. }
  58.  
  59.  
  60.  
  61.  
Feb 3 '08 #1
7 10601
I did get my code to compile and run. I also established a connection with the mail server but I can't seem to get an email sent. Can someone please help.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class EmailAgent
  5. {
  6.    public static void main(String[] args) throws Exception
  7.    {
  8.       // Establish a TCP connection with the mail server.
  9.  
  10.       Socket TCPConnection = new Socket("mail.bellsouth.net", 25);
  11.  
  12.       // Create a BufferedReader to read a line at a time.
  13.  
  14.       InputStream is = TCPConnection.getInputStream();
  15.       InputStreamReader isr = new InputStreamReader(is);
  16.       BufferedReader br = new BufferedReader(isr);
  17.  
  18.       // Read greeting from the server.
  19.  
  20.       String response = br.readLine();
  21.       System.out.println(response);
  22.       if (!response.startsWith("220")) {
  23.          throw new Exception("220 reply not received from server.");
  24.       }
  25.  
  26.       // Get a reference to the socket's output stream.
  27.       OutputStream os = TCPConnection.getOutputStream();
  28.  
  29.       // Send HELLO command and get server response.
  30.       //String command = "HELLO Nikki\r\n";
  31.  
  32.         // Send FROM command.
  33.       String command = "MAIL FROM:Nikki<nikki_barnard@bellsouth.net>\r\n";
  34.       // Send RCPT TO command.
  35.       String command2 = "RCPT TO:Nikki<misslilbit02@yahoo.com>\r\n";
  36.       // Send DATA command.
  37.       String command3 = "DATA\r\n";
  38.       // Subject
  39.       String command4 = "SUBJECT: DataComm Programming Assignment\r\n\r\n";
  40.       // Send message data.
  41.       String command5 = "HELLO Nikki. This is your java programming assignment for DataComm\r\n";
  42.       // End with line with a single period.      
  43.       String command6 = ".\r\n";
  44.       // Send QUIT command.
  45.       String command7 = "QUIT\r\n";
  46.  
  47.       System.out.print(command);
  48.       System.out.print(command2);
  49.       System.out.print(command3);
  50.       System.out.print(command4);
  51.       System.out.print(command5);
  52.       System.out.print(command6);
  53. //      System.out.print(command7);      
  54.       os.write(command.getBytes("US-ASCII"));
  55.       response = br.readLine();
  56.       System.out.println(response);
  57.       if (!response.startsWith("250")) {
  58.          throw new Exception("250 reply not received from server.");
  59.       }
  60.  
  61.    }
  62. }
  63.  
  64.  
Feb 3 '08 #2
Nevermind I Got It To Work.
Feb 4 '08 #3
vfly7
1
This is one of the example for this SMTP Email in Java Solution.
Hope this will help you.
Thanks.

Code(EmailAgent.java):-
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class EmailAgent
{
public static void main (String [] args) throws Exception
{
//Fill in the require information
String mailServer = ""; //Fill in the mail server address
String to = ""; //Fill in the receiver email address
String from = ""; //Fill in the sender email address
String msg = ""; //Fill in the message that want to be send

//Establish a TCP connection with the mail server
Socket socket = new Socket(mailServer,25);

//Create a BufferedReader to read a line at a time.
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

//Read greeting from the server
String response = br.readLine();
System.out.println(response);

//DataOutputStream
DataOutputStream toServer = new DataOutputStream(socket.getOutputStream());

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

//Get a reference to the socket's output stream.
OutputStream os = socket.getOutputStream();

//Send HELO command and get server response.
String command = "HELO alice\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 cmdFrom = "MAIL FROM:<" + from + ">\r\n";
toServer.writeBytes(cmdFrom);
response = br.readLine();
System.out.println(response);

//Send RCPT TO command
String cmdTo = "RCPT TO:<" + to + ">\r\n";
toServer.writeBytes(cmdTo);
response = br.readLine();
System.out.println(response);

//Send DATA command
String cmdData = "DATA\r\n";
toServer.writeBytes(cmdData);
response = br.readLine();
System.out.println(response);

//Send message data
String cmdMsg = msg + "\r\n";

//Date
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
String dateString = format.format(new Date());
String cmdDate = "Date: " + dateString + "\r\n";

//Send write to fill in email form
String cmdAll = "From: " + from + "\r\n" + "To: " + to + "\r\n" + cmdDate + "\r\n" + cmdMsg + ".";
toServer.writeBytes(cmdAll);

//End with line with a single period
String cmdCrlf = "\r\n";
toServer.writeBytes(cmdCrlf);
response = br.readLine();
System.out.println(response);

//Send Quit command
String cmdQuit = "QUIT\r\n";
toServer.writeBytes(cmdQuit);
response = br.readLine();
System.out.println(response);

socket.close();

System.out.println(cmdFrom);
System.out.println(cmdTo);
System.out.println(cmdData);
System.out.println(cmdMsg);
System.out.println(cmdQuit);
System.out.println(cmdCrlf);
}
}
Feb 18 '08 #4
Nevermind I Got It To Work.
Well, me too had the same problem, i used this code, and still no mail is being sent.
Can you help me regarding this ?

Thanks,
Mohamed
Nov 2 '08 #5
JosAH
11,448 Expert 8TB
Why don't you people use Sun's Java Mail API?

kind regards,

Jos
Nov 2 '08 #6
Why don't you people use Sun's Java Mail API?

kind regards,

Jos
Finally someone sees the light
Nov 3 '08 #7
JosAH
11,448 Expert 8TB
Finally someone sees the light
I've decided to put the download link in the "Read This First" article; people seem
to be unable to google for it themselves. Our solar system must be floating through
a huge cloud of silly particles and we haven't hit the center yet I'm afraid.

kind regards,

Jos
Nov 3 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by stevenkblack | last post: by
3 posts views Thread by dale zhang | last post: by
34 posts views Thread by antonyliu2002 | last post: by
reply views Thread by John Doe | last post: by
reply views Thread by jlconde | last post: by
7 posts views Thread by oopsbabies | last post: by
dmjpro
3 posts views Thread by dmjpro | last post: by
1 post views Thread by rajujrk | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.