import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
class SocketMailTest
{
public static void main(String[] args)
{
Socket socket = null;
System.setProperty("line.separator", "\r\n");
try {
socket = new Socket("서버IP",서버PORT);
String forward = "jungws55@nate.com";
String reverse = "jungws55@naver.com";
String replyto = reverse;
String subject = "Hello World";
String content = "<html><body><h2>Hello World</h2> </body></html>";
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
sendAndReceive(null, rd, pw);
sendAndReceive("HELO mtarget.cn", rd, pw);
sendAndReceive("MAIL FROM:<" + reverse + ">", rd, pw);
sendAndReceive("RCPT TO:<" + forward + ">", rd, pw);
sendAndReceive("DATA", rd, pw);
pw.println("Subject:" + subject);
pw.println("From:<"+ reverse +">");
pw.println("To:<"+ forward +">");
pw.println("Reply-To:<"+ replyto +">");
pw.println("Date:" + (new Date()));
pw.println("Mime-Version: 1.0;");
pw.println("Content-Type: text/html;");
pw.println("charset=\"utf-8\";");
pw.println();
pw.println();
pw.println(content);
pw.flush();
sendAndReceive(".", rd, pw);
sendAndReceive("QUIT", rd, pw);
} catch (Exception e) {
}
if (socket != null) {
try {
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
private static void sendAndReceive(String text, BufferedReader br, PrintWriter pw) throws IOException {
if (text != null) {
System.out.println("Client> " + text);
pw.println(text);
pw.flush();
}
String response;
if ((response = br.readLine()) != null) {
System.out.println("Server> " + response);
}
}
}
'JAVA' 카테고리의 다른 글
[펌]Java Architecture for XML Binding (JAXB) 예제 (0) | 2014.03.03 |
---|---|
[ERROR] java 오류 정리 (0) | 2014.02.26 |
SMTP 서버를 통해 메일 전송 (0) | 2014.02.19 |
java.lang.reflect.Method 샘플 (0) | 2013.12.05 |
Annotation Example (0) | 2013.12.05 |