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.");
}
}
denniskhor
-4
Junior Poster in Training
Recommended Answers
Jump to PostThis is Java code not Javascript code; the languages are entirely different.
The thread has been moved to the Java forum.
On another note, you're more likely to get help with the problem if you explain the problem briefly -- rather than just posting a block of code …
Jump to PostYou're more likely to get help with the problem if you explain the problem briefly -- rather than just posting a block of code and expecting others to actually find the problem aswel.
Not only "more likely", but more like "required". I for one, and most others here, will not …
Jump to PostFix your imports. You seem to be attempting to use a lot of classes without ever having imported them.
Jump to PostAre you importing any of the packages that these things are?
All 14 Replies
MattEvans
473
Veteran Poster
Team Colleague
Featured Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
denniskhor
-4
Junior Poster in Training
denniskhor
-4
Junior Poster in Training
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
denniskhor
-4
Junior Poster in Training
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
denniskhor
-4
Junior Poster in Training
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
masijade
1,351
Industrious Poster
Team Colleague
Featured Poster
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
denniskhor
-4
Junior Poster in Training
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
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.