473,473 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SMTP Email in Java

22 New Member
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 10926
lilbit02
22 New Member
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
lilbit02
22 New Member
Nevermind I Got It To Work.
Feb 4 '08 #3
vfly7
1 New Member
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
midofidodido
1 New Member
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 Recognized Expert MVP
Why don't you people use Sun's Java Mail API?

kind regards,

Jos
Nov 2 '08 #6
N002213F
39 New Member
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 Recognized Expert MVP
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

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

Similar topics

1
by: stevenkblack | last post by:
I want to send out emails like the task center sends. I have found several post about creating your own stored procedures in Java or C that will do it. The Task center is already using an api to...
3
by: dale zhang | last post by:
Hi, I write an asp.net web application. It has a “Contact Us” page, where users fill in their email, subject and text and hit send. Then the email will go to my hard coded yahoo email...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
0
by: John Doe | last post by:
A while ago I wrote an email program that worked fine with smtp. My internet provider recently switched to SMTP Authentication. My email program doesn't work anymore. I think I need to authenticate...
0
by: jlconde | last post by:
I have a classe to send mails. It runs on yahoo well but with hotmail I never receive the mails.I do not receive an error neither. I would need some strange header to make the hotmail like my...
7
by: oopsbabies | last post by:
Hello everyone, I am using Apache 1.3.33 as the web server and PHP version 4.3.10. My machine is using Windows XP 2002 professional edition which comes with a Windows firewall. I am using McAfee...
3
dmjpro
by: dmjpro | last post by:
i have been trying to do mail programming in java.... al in vain one thing still irritating that SMTP ..... the server must have the SMTP service provider. how can i know that the server is...
0
by: sudipmondal440 | last post by:
import javax.mail.*; import javax.mail.internet.*; import java.util.*; import javax.activation.*; import javax.swing.text.*; import javax.swing.*; import java.io.*; import java.awt.*; ...
1
by: rajujrk | last post by:
hi, i am currently sending mail through the JavaMail API. I had a problem to find out the SMTP mailserver. First i tried in airtel broadband Connection its works fine.. but now i using it in my...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.