Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sending email  RSS feed

 
sara li
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run this program which I took from one tutorial in the net. I passsed my localhost as my email server name 127.0.0.1 (I have email server but I do not know what is its name), anyway when I tried to run the code I got this error message. what should I do??
java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read
Press any key to continue...
=========
import java.net.*;
import java.io.*;
import java.util.*;
class Sockets02{
public static void main(String[] args){
String server = "127.0.0.1";
int port = 25; //mail port
try{
//Get a socket, connected to the specified server
// on the specified port.
Socket socket = new Socket(server,port);

//Get an input stream from the socket
BufferedReader inputStream =
new BufferedReader(new InputStreamReader(
socket.getInputStream()));
//Get an output stream to the socket. Note
// that this stream will autoflush.
PrintWriter outputStream =
new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()),true);
//Begin the conversation with the email server.
outputStream.println(
"mail from: 127.0.0.1");
System.out.println(inputStream.readLine());

outputStream.println(
"rcpt to: " + "127.0.0.1");
System.out.println(inputStream.readLine());
outputStream.println("data");
System.out.println(inputStream.readLine());
String timeStamp = (new Date()).toString();
outputStream.println("Test message " + timeStamp);
outputStream.println(".");
System.out.println(inputStream.readLine());
//Close the socket
socket.close();
}//end try
catch(UnknownHostException e){
System.out.println(e);
System.out.println(
"Must be online to run properly.");
}//end catch UnknownHostException
catch(IOException e){System.out.println(e);}
}//end main
}//end class Sockets02
//=======================================================//
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sara,
You don't seem to be talking the right language here to communicate with a mail server. You need to understand the SMTP protocol to communicate over plain sockets. You can check out the white paper here: RFC 821. That's pretty technical and a better way to get a feel for the protocol is the hacker's er I mean network programmers tustiest tool - telnet.
Just telnet into your mailserver and see how the conversation works. It should look something like this:

Once you understand the basic commands and the responses by the server then you should be able to code a Java program to communicate with an SMTP server over a plain socket.
Hope this helps,
Michael Morris
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you're doing this as a learning experience. If not, try JavaMail.
- Peter
 
sara li
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I want to learn how things work. I do not want to use ready API. Thanks
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara,
Just want to point out one other thing.
You must open the socket to a SMTP server to send mail. Most unix boxes work great but most pc's don't have an SMTP service running so I don't think you can use the local host for that.
 
sara li
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lu B. Thanks for your advice, but NO I have SMTP since my computer is email server, I am running ArgoSoft, thanks
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following method will help you get your email program started. It uses the java.net package instead of JavaMail API. Attachments can be sent this way too but are slightly more complicated - you have to dig into the MIME protocol. Be sure to use someone in the "CC", even if its yourself again, as this unpolished code will fail otherwise.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!