<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Combining smtp with your own program"]]></title>
		<link>http://forums.hotjoe.com/posts/list/3.page</link>
		<description><![CDATA[Latest messages posted in the topic "Combining smtp with your own program"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Combining smtp with your own program</title>
				<description><![CDATA[ Hi,<br /> <br /> I have these two codes which both involve sockets. I can't figure out how to combine the smtp code into my first code. If you look at the first code where it prints out "you are at picture A" it is here were i want to be able to send an email. I am quite puzzled as how to combine the two codes to make it work. Any help would be greatly appreciated.<br /> <br /> First Code:<br /> [code]import java.io.*;<br /> import java.net.*;<br /> import java.util.Scanner;<br /> import java.io.File;<br /> <br /> <br /> <br /> public class Combo2 {<br />     public static void main(String[] args) throws IOException {<br /> <br />         Socket kkSocket = null;<br />         PrintWriter out = null;<br />         BufferedReader in = null;<br /> 	File yourFile = new File("outs.txt");<br /> 	yourFile.delete();<br /> <br /> <br />         try {<br /> <br />             kkSocket = new Socket("192.168.33.39", 12000);<br />             out = new PrintWriter(kkSocket.getOutputStream(), true);<br />             in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));<br />         } catch (UnknownHostException e) {<br />             System.err.println("Don't know about host: Adam.");<br />             System.exit(1);<br />         } catch (IOException e) {<br />             System.err.println("Couldn't get I/O for the connection to: Adam.");<br />             System.exit(1);<br />         }<br /> <br />         BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));<br />         String fromServer;<br />         String fromUser;<br /> <br />         while ((fromServer = in.readLine()) != null) <br /> 	  {<br /> <br /> 		<br />             <br />             if (fromServer.equals("Bye."))<br />                 break;<br /> <br />  try{<br />     // Create file <br />     FileWriter fstream = new FileWriter("outs.txt", true);<br />         BufferedWriter outs = new BufferedWriter(fstream);<br />     <br /> <br /> outs.write( fromServer);<br /> outs.newLine();<br />     //Close the output stream<br />     outs.close();<br /> <br /> <br /> <br /> <br />     <br /> }<br /> catch (Exception e){//Catch exception if any<br />       System.err.println("Error: " + e.getMessage());<br />     }<br /> FileReader fin = new FileReader("outs.txt");<br /> <br />     Scanner src = new Scanner(fin);<br /> <br />     while (src.hasNext()) {<br /> 		<br /> 		Double Tag = (src.nextDouble());<br /> 		Double x = (src.nextDouble());<br /> 		Double z = (src.nextDouble());<br /> 		Double y = (src.nextDouble());<br /> <br /> <br />       if ((x&lt;=19.92) && (y&lt;=6.1543) && (y&gt;=4.22))<br /> {<br /> <br />        <br /> <br /> 	System.out.println("You are at Picture A");<br /> 	<br /> }<br /> else if ((x&lt;=19.92) && (y&lt;=3)  && (y&gt;=1))<br /> {<br /> 	<br /> 	<br /> 	System.out.println("You are at Picture B");<br /> <br /> 	}<br /> else <br /> {<br /> System.out.println("Nope");<br /> }<br />       <br />       } <br /> 		<br /> <br />         }<br /> <br />         out.close();<br />         in.close();<br />         stdIn.close();<br />         kkSocket.close();<br />     }<br /> }[/code]<br /> <br /> <br /> SMTP code:<br /> [code]import java.io.*;<br /> import java.net.Socket;<br /> <br /> <br /> public class Emailer {<br /> <br />     public static void main(String[] args) throws Exception {<br />         String results = send("mailhost.computing.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)<br />         StringBuffer buffer = new StringBuffer();<br /> <br />         try {<br /> <br />             // Connect to the SMTP server running on the local machine. <br />             Socket smtpSocket = new Socket(host, port);<br /> <br />             //  send commands TO the server with this<br />             DataOutputStream output = new DataOutputStream(smtpSocket.getOutputStream());<br /> <br />             //  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 /> }[/code]]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/706/2637.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/706/2637.page</link>
				<pubDate><![CDATA[Mon, 15 Mar 2010 08:14:27]]> GMT</pubDate>
				<author><![CDATA[ colossusdub]]></author>
			</item>
	</channel>
</rss>
