how to solve Host unknown exception -
when try execute code ...it produces hostunknownexception....
import java.io.*; import java.net.*; import java.net.socket; public class smtp { public static void main(string[] args) //throws exception { string results = send("sender@somewhere.com", "localhost/localdomain", "test email", "<b>you got mail!</b>"); system.out.println(results); } public static string send(string from,string to,string subject, string message) //throws exception { stringbuffer buffer = new stringbuffer(); try { socket smtpsocket = new socket("127.0.0.1",25); dataoutputstream output = new dataoutputstream(smtpsocket.getoutputstream()); bufferedreader input =new bufferedreader(new inputstreamreader( new datainputstream(smtpsocket.getinputstream()))); try { read(input, buffer); send(output, "helo localhost.localdomain\r\n", buffer); read(input, buffer); send(output, "mail from: " + + "\r\n", buffer); read(input, buffer); send(output, "rcpt to: " + + "\r\n", buffer); read(input, buffer); send(output, "data\r\n", buffer); read(input, buffer); send(output, "subject: " + subject + "\r\n", buffer); send(output, message, buffer); send(output, "\r\n.\r\n", buffer); read(input, buffer); smtpsocket.close(); } catch (ioexception e) { system.out.println("cannot send email error occurred."); } } catch (exception e) { system.out.println("host unknown"); } return buffer.tostring(); } private static void send(dataoutputstream output,string data,stringbuffer buffer) throws ioexception { output.writebytes(data); buffer.append(data); } private static void read(bufferedreader br, stringbuffer buffer) throws ioexception { int c; while ((c = br.read()) != -1) { buffer.append((char) c); if (c == '\n') { break; } } } }
please me.....i'm new java...i trying implement simple mail transfer protocol...there no compilation errors....when try execute ...unknown host exception produced....
you still need smtp server running on localhost. apache james. or try using google ones testing purpose.
try mykong example
Comments
Post a Comment