
»
Sockets and Internet Protocols
Author
Java and SMTP
Matt Millar
Greenhorn
Joined: Oct 16, 2012
Posts: 11
posted
Feb 20, 2013 08:11:09
0
Hello I am trying to learn how SMTP works and I went with
java
for my
test
. I have been following some examples and putting together a Frankenstein kinda code but it works to a degree. Everything works fine up till you try to send a email I think. I gets down to the mail FROM part then gives me this error
530 5.7.0 Must issue a STARTTLS command first. gm14sm23464592bkc.7 - gsmtp Exception in thread "main" java.lang.Exception: 250 reply not received from server. at emailagent.EmailAgent.main(EmailAgent.java:73) Java Result: 1
I am stumped on it. It look like the one online that I used and from my book but it does not work. Am i missing something here? Thanks for any help with this.
Here my code
import java.io.*; import java.net.*; import java.util.*; public class EmailAgent { public static void main(String[] args) throws Exception { // Establish a TCP connection with the mail server. System.out.println("Enter the mail server you wish to connect to (example: pop.gmail.com):\n"); String hostName = new String(); Scanner emailScanner = new Scanner(System.in); hostName = emailScanner.next(); Socket socket = new Socket(hostName, 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); 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\r\n"; System.out.println(mailFromCommand); os.write(mailFromCommand.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server.\n"); } // Send RCPT TO command. System.out.println("Please type the destination e-mail address (example: example@nova.edu):\n"); String destEmailAddress = new String(); destEmailAddress = emailScanner.next(); String fullAddress = new String();\r\n"; System.out.println(fullAddress); os.write(fullAddress.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if(!response.startsWith("250")) { throw new Exception("250 reply not received from server.\n"); } // Send DATA command. String dataString = new String(); dataString = "DATA"; System.out.println(dataString); os.write(dataString.getBytes("US-ASCII")); response = br.readLine(); if(!response.startsWith("354")) { throw new Exception("354 reply not received from server.\n"); } System.out.println(response); // Send message data. System.out.println("Enter your message, enter '.' on a separate line to end message data entry:\n"); String input = new String(); while(input.charAt(0) != '.') { input = emailScanner.next(); os.write(input.getBytes("US-ASCII")); } //End with line with a single period. os.write(input.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if(!response.startsWith("250")) { throw new Exception("250 reply not received from server\n"); } // Send QUIT command. String quitCommand = new String(); quitCommand = "QUIT"; os.write(quitCommand.getBytes("US-ASCII")); } }
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 18113
8
I like...
posted
Feb 20, 2013 10:16:13
1
Looks like you're trying to connect to a mail server which requires a secure connection. That's going to be a complication which you don't need while you're learning the basics of SMTP. So I suggest you test with a different server.
Matt Millar
Greenhorn
Joined: Oct 16, 2012
Posts: 11
posted
Feb 20, 2013 11:20:09
0
Thank you I didnt even think of that. Could you suggest one that would work?
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 18113
8
I like...
posted
Feb 20, 2013 12:34:52
0
I would suggest running your own. (That also allows you to get experience with the server side of SMTP.) Apache James is an open-source SMTP server written in Java, you could use that.
I agree. Here's the link:
subject: Java and SMTP
Similar Threads
send email
SMTP java program...
error while sending mail using socket class
Not able to send a mail using JAVA code
File transfer using FTP
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
JForum
|
Paul Wheaton