메일구축시에 편리한 SMTP, POP3 테스트툴

[다운로드] SMTP 서버를 통해 메일이 잘 전송되는지 확인하기 위한 툴 (자작툴) [crayon] import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SMTPTest { private static void mail(String address, String title, String body) { System.out.println(“Sending…”); Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); MimeMessage mimeMessage = new MimeMessage(session); try { mimeMessage.setFrom(new InternetAddress(“admin@test.com”, “SMTPTest“, “iso-2022-jp”)); mimeMessage.setRecipients(Message.RecipientType.TO, address); mimeMessage.setSubject(title, “iso-2022-jp”); mimeMessage.setText(body, “iso-2022-jp”); mimeMessage.setHeader(“Content-Type”, “text/plain; charset=”iso-2022-jp””); mimeMessage.setHeader(“Content-Transfer-Encoding”, “7bit”); mimeMessage.setSentDate(new Date()); Transport transport = session.getTransport(“smtp”); if ( props.getProperty(“mail.smtp.auth”) != null ) { transport.connect(null, props.getProperty(“mail.smtp.auth.id”), props.getProperty(“mail.smtp.auth.password”)); } else { transport.connect(); } transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); transport.close(); System.out.println(“OK!!”); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { if ( args.length < 3 ) { System.exit(1); } mail(args[0], args[1], args[2]); } } [/crayon] [tip] 별도 mail.jar 가 필요함. smtp.sh 을 만들어 두면 편리함.

#!/bin/bash java -Dmail.smtp.host=localhost -Dmail.smtp.auth=true -Dmail.smtp.auth.id=아이디 -Dmail.smtp.auth.password=비밀번호 -classpath ./mail.jar:. SMTPTest $*
[/tip] POP3 서버에 로그인하여 메일 리스트를 확인하기 위한 툴 (조금 참조하여 고침) [crayon] import java.net.Socket; import java.io.*; public class POP3Test { private Socket mailserver; private BufferedReader reader; private BufferedWriter writer; public POP3Test(String servername, String username, String password) throws Exception { System.out.println(“[*] Connection ” + servername + “:110”); mailserver = new Socket(servername, 110); reader = new BufferedReader(new InputStreamReader(mailserver .getInputStream())); writer = new BufferedWriter(new OutputStreamWriter(mailserver .getOutputStream())); if (isReady() && isReady(“user ” + username) && isReady(“pass ” + password)) { System.out.println(“[*] Login Success!!”); } else { System.out.println(“[*] Login Failure.”); mailserver.close(); } } private boolean isReady() throws IOException { String response = reader.readLine(); System.out.println(“[-] ” + response); return response.matches(“^\+OK.*$”); } private boolean isReady(String message) throws IOException { final String end = “rn”; writer.write(message + end); writer.flush(); return isReady(); } public void transaction() throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); while (!mailserver.isClosed()) { System.out.print(“# “); String command = reader.readLine(); if (command.equalsIgnoreCase(“quit”)) { if (isReady(command)) { mailserver.close(); } } else if (command.equalsIgnoreCase(“list”)) { if (isReady(command)) { System.out.println(getMessage().split(” “)[0]); } } else if (command.matches(“(?i)^read \d+$”)) { if (isReady(command.replaceFirst(“read”, “retr”))) { System.out.println(getMessage()); } } else { System.out.println(“Ignored: ” + command + ” Only use quit, list, read *”); } } } private String getMessage() throws IOException { StringBuffer message = new StringBuffer(); while (true) { String s = reader.readLine(); if (s.equals(“.”)) { if (message.length() > 0) { message.deleteCharAt(message.length() – 1); } break; } else { message.append(s + “n”); } } return message.toString(); } public static void main(String[] args) { if ( args.length < 3 ) { System.exit(1); } try { POP3Test pop3 = new POP3Test(args[0], args[1], args[2]); pop3.transaction(); } catch (Exception e) { e.printStackTrace(); } } } [/crayon] [tip] pop3.sh 을 만들어 두면 편리함.
#!/bin/bash java -classpath . POP3Test $*
실행결과는 대략 아래와 같다.
[root@centis1504 mail-test]# ./pop3.sh centis1504.net test1@centis1504.net testtest [*] Connection centis1504.net:110 [-] +OK Dovecot ready. [-] +OK [-] +OK Logged in. [*] Login Success!! ready: list [-] +OK 19 messages: 1 ready: read 1 [-] +OK 670 octets
[/tip]]]>

Related Posts