하은양 믿음군 효실맘 호홍홍집s

간단한 Sendmail을 이용한 메일보내기 본문

가벼운 배움/jsp

간단한 Sendmail을 이용한 메일보내기

호홍홍집 2014. 12. 26. 20:08

기본적으로 linux 서버에 sendmail이 있는지 확인한다.
host : localhost, port : 25, username 및 password는 그냥 local이라서 생략...ㅋㅋ

 

<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8" errorPage="/errFolder/errPage.jsp" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%

    Util util = new Util();
    UtilParam param = new UtilParam(request);

//////////////////////////////////////////////////////////////////////////////
    String sbj = param.getParameter("subject","");
    String sf = param.getParameter("fEmail","");
    String st = param.getParameter("tEmail","");
    String sms = param.getParameter("msg","");

    String result = "";

    if(!sf.equals("") && !st.equals("") && !sms.equals("")){

        // Assuming you are sending email from localhost
        String host = "localhost";

        // Get system properties object
        Properties properties = System.getProperties();

        // Setup mail server
        properties.setProperty("mail.smtp.host", host);

        //Get the Session object.
        try {

            // username, password 생략 : null,  생략안할시는 다른데 검색해보면 금방 나옴...
            Session mailSession = Session.getDefaultInstance(properties,null);

            // Create a default MimeMessage object.
            Message message = new MimeMessage(mailSession);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(sf));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(st));

            // Set Subject: header field
            message.setSubject(sbj);

            // Now set the actual message
            message.setText(sms);
            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");
            result = "Sent message successfully....";

        } catch (MessagingException e) {
            e.printStackTrace();
            result = "Error: unable to send message...." + e.toString();

        }

    }


//////////////////////////////////////////////////////////////////////////////
    // 기본 변수 선언....
    request.setAttribute("RESULT",result);
%>