473,781 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ 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 10944
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(mailServ er,25);

//Create a BufferedReader to read a line at a time.
InputStream is = socket.getInput Stream();
InputStreamRead er isr = new InputStreamRead er(is);
BufferedReader br = new BufferedReader( isr);

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

//DataOutputStrea m
DataOutputStrea m toServer = new DataOutputStrea m(socket.getOut putStream());

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

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

//Send HELO command and get server response.
String command = "HELO alice\r\n";
System.out.prin t(command);
os.write(comman d.getBytes("US-ASCII"));
response = br.readLine();
System.out.prin tln(response);
if (!response.star tsWith("250"))
{
throw new Exception("250 reply not received from server.");
}

//Send MAIL FROM command
String cmdFrom = "MAIL FROM:<" + from + ">\r\n";
toServer.writeB ytes(cmdFrom);
response = br.readLine();
System.out.prin tln(response);

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

//Send DATA command
String cmdData = "DATA\r\n";
toServer.writeB ytes(cmdData);
response = br.readLine();
System.out.prin tln(response);

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

//Date
SimpleDateForma t format = new SimpleDateForma t("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
String dateString = format.format(n ew 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.writeB ytes(cmdAll);

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

//Send Quit command
String cmdQuit = "QUIT\r\n";
toServer.writeB ytes(cmdQuit);
response = br.readLine();
System.out.prin tln(response);

socket.close();

System.out.prin tln(cmdFrom);
System.out.prin tln(cmdTo);
System.out.prin tln(cmdData);
System.out.prin tln(cmdMsg);
System.out.prin tln(cmdQuit);
System.out.prin tln(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
1621
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 send email. Is there any way to use the same process that it is using. I can't find anything about it in the documentation and I don't want to reinvent the wheel. Thanks, Steven
3
5588
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 account. I was using optimum online service with smtp server as “mail.optonline.net”. I can receive email correctly. Now I switch to verizon DSL. I change my smtp server accordingly as
34
18271
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 "127.0.0.1". 4. Authentication: "Anonymous access" only. 5. Outbound connection listen to TCP 25. Besides,
0
2518
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 using my username and password. Does anyone know if the Java library has any methods/classes that allow me to authenticate?
0
6343
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 email???? My code is: import java.net.*; import java.io.*; import java.util.*;
7
7746
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 free edition for anti-virus. I use an ISP provider from my country and according to them I do not need to perform authentication while sending mails through their SMTP address. Thus I am using external SMTP server. I don't have IIS installed...
3
2205
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 SMTP supported????? is it the extra job i have to do .... if i have JAVA MAIL package???? total thing is now mess up to me.
0
1640
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.*; import javax.activation.DataSource; import javax.mail.internet.MimeMessage;
1
2416
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 college lab. we have broadband internet connection, but dont know what kindof connection.. Here is my Code import javax.mail.*; import javax.mail.internet.*; import java.util.*; class SendMail{ public static void main(String jrk)throws...
0
9636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6727
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.