<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Problem using SMTP"]]></title>
		<link>http://forums.hotjoe.com/posts/list/3.page</link>
		<description><![CDATA[Latest messages posted in the topic "Problem using SMTP"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Problem using SMTP</title>
				<description><![CDATA[ Hi guys,<br /> <br /> I am experiencing problems when trying to send emails using the smtp.  I am using the code below but am getting the following errors:<br /> <br /> [b]220 hawk.dcu.ie ESMTP Service ready<br /> HELO localhost.localdomain<br /> 250 hawk.dcu.ie<br /> MAIL FROM: adam.keaney2@mail.dcu.ie<br /> 501 Syntax error in parameters or arguments to MAIL command<br /> RCPT to: adam.keaney2@mail.dcu.ie<br /> 501 Syntax error in parameters or arguments to RCPT command<br /> DATA<br /> 503 DATA command outside of MAIL transaction<br /> Subject: Test Email<br /> Content-type: text/html; charset="us-ascii"<br /> &lt;b&gt;You got mail!&lt;/b&gt;<br /> .<br /> 500 Subj command unrecognized[/b]<br /> <br /> [code]import java.io.*;<br /> import java.net.Socket;<br /> <br /> /**<br />  * Simple SMTP Client that allows your Java App send emails.<br />  * Uses Java Sockets to connect directly to an SMTP server.<br />  * It supports sending of plain text or HTML emails.<br />  * Bonus: Unlike many other Java SMTP Sockets examples, this one actually works.<br />  *<br />  * @author Olly Oechsle, www.intelligent-web.co.uk<br />  */<br /> public class Emailer {<br /> <br />     public static void main(String[] args) throws Exception {<br />         String results = send("mail.dcu.ie",<br />                 25,<br />                 "adam.keaney2@mail.dcu.ie",<br />                 "adam.keaney2@mail.dcu.ie",<br />                 "Test Email",<br />                 "&lt;b&gt;You got mail!&lt;/b&gt;");<br />         System.out.println(results);<br />     }<br /> <br />     /**<br />      * Sends an email.<br />      *<br />      * @return The full SMTP conversation as a string.<br />      */<br />     public static String send(<br />             String host,<br />             int port,<br />             String to,<br />             String from,<br />             String subject,<br />             String message) throws Exception {<br /> <br />         // Save the SMTP conversation into this buffer (for debugging if necessary)<br />         StringBuffer buffer = new StringBuffer();<br /> <br />         try {<br /> <br />             // Connect to the SMTP server running on the local machine. Usually this is SendMail<br />             Socket smtpSocket = new Socket(host, port);<br /> <br />             // We send commands TO the server with this<br />             DataOutputStream output = new DataOutputStream(smtpSocket.getOutputStream());<br /> <br />             // And recieve responses FROM the server with this<br />             BufferedReader input =<br />                     new BufferedReader(<br />                             new InputStreamReader(<br />                                     new DataInputStream(smtpSocket.getInputStream())));<br /> <br />             try {<br /> <br />                 // Read the server's hello message<br />                 read(input, buffer);<br /> <br />                 // Say hello to the server<br />                 send(output, "HELO localhost.localdomain\r\n", buffer);<br />                 read(input, buffer);<br /> <br />                 // Who is sending the email<br />                 send(output, "MAIL FROM: " + from + "\r\n", buffer);<br />                 read(input, buffer);<br /> <br />                 // Where the mail is going<br />                 send(output, "RCPT to: " + to + "\r\n", buffer);<br />                 read(input, buffer);<br /> <br />                 // Start the message<br />                 send(output, "DATA\r\n", buffer);<br />                 read(input, buffer);<br /> <br />                 // Set the subject<br />                 send(output, "Subject: " + subject + "\r\n", buffer);<br /> <br />                 // If we detect HTML in the message, set the content type so it displays<br />                 // properly in the recipient's email client.<br />                 if (message.indexOf("&lt;") == -1) {<br />                     send(output, "Content-type: text/plain; charset=\"us-ascii\"\r\n", buffer);<br />                 } else {<br />                     send(output, "Content-type: text/html; charset=\"us-ascii\"\r\n", buffer);<br />                 }<br /> <br />                 // Send the message<br />                 send(output, message, buffer);<br /> <br />                 // Finish the message<br />                 send(output, "\r\n.\r\n", buffer);<br />                 read(input, buffer);<br /> <br />                 // Close the socket<br />                 smtpSocket.close();<br /> <br />             }<br />             catch (IOException e) {<br />                 System.out.println("Cannot send email as an error occurred.");<br />             }<br />         }<br />         catch (Exception e) {<br />             System.out.println("Host unknown");<br />         }<br /> <br />         return buffer.toString();<br /> <br />     }<br /> <br />     /**<br />      * Sends a message to the server using the DataOutputStream's writeBytes() method.<br />      * Saves what was sent to the buffer so we can record the conversation.<br />      */<br />     private static void send(DataOutputStream output,<br />                              String data,<br />                              StringBuffer buffer)<br />             throws IOException {<br />         output.writeBytes(data);<br />         buffer.append(data);<br />     }<br /> <br />     /**<br />      * Reads a line from the server and adds it onto the conversation buffer.<br />      */<br />     private static void read(BufferedReader br, StringBuffer buffer) throws IOException {<br />         int c;<br />         while ((c = br.read()) != -1) {<br />             buffer.append((char) c);<br />             if (c == '\n') {<br />                 break;<br />             }<br />         }<br />     }<br /> <br /> }<br /> [/code]]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/705/2635.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/705/2635.page</link>
				<pubDate><![CDATA[Mon, 15 Mar 2010 06:35:07]]> GMT</pubDate>
				<author><![CDATA[ colossusdub]]></author>
			</item>
			<item>
				<title>Re:Problem using SMTP</title>
				<description><![CDATA[ Do you really want to implement the entire SMTP protocol yourself?  The problem is that you're not enclosing the &quot;from&quot; address in &lt; and &gt; which is required by the [url=http://www.freesoft.org/CIE/RFC/821/15.htm]RFC[/url].<br /> <br /> The [url=http://java.sun.com/products/javamail/]JavaMail API[/url] is very powerful and can handle much of this for you.  Unless this is a school project, I'd strongly encourage you to use existing libraries rather than roll your own.]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/705/2636.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/705/2636.page</link>
				<pubDate><![CDATA[Mon, 15 Mar 2010 08:11:48]]> GMT</pubDate>
				<author><![CDATA[ stdunbar]]></author>
			</item>
			<item>
				<title>Problem using SMTP</title>
				<description><![CDATA[ Thanks, I managed to get it sorted.  I am new to this whole java language so it's quite confusing for me!]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/705/2638.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/705/2638.page</link>
				<pubDate><![CDATA[Mon, 15 Mar 2010 08:16:18]]> GMT</pubDate>
				<author><![CDATA[ colossusdub]]></author>
			</item>
	</channel>
</rss>
