티스토리 뷰

반응형

Oracle 내부 Java로 메일전송하는 방법 입니다.


1. Oracle에 java Source를 컴파일한다.

2. Oracle Java 호출하는 프로시저나 함수를 생성한다.

3. 호출한다.



--Step 1.

DROP JAVA SOURCE "MailSend";


CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "MailSend" as  import java.util.Hashtable;

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.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import javax.mail.internet.MimeUtility;

import javax.activation.CommandMap;

import javax.activation.MailcapCommandMap;


public class MailSend {

    


    public static void main(String args[]) {

        try{

            mailSend("send@abc.com","receive@abc.com","subject","contents","");

        }catch(Exception e){

            

        }

    }

    

    public static String mailSend(String strFrAddr, String strToAddr, String strSubJect, String strHtml, String strFileNm ) throws Exception {



        String ret = "";


        try{

            sendMail(strFrAddr, strToAddr,strSubJect,strHtml,strFileNm );

        }

        catch (Exception e) {

            System.out.println(e.getMessage());

            ret = e.toString();

               for (StackTraceElement ele:e.getStackTrace()){


                        ret += "\n"+ele.toString();

                }

        }

        

        return ret;

        

    }

    

    


    public static void sendMail(String strFrAddr, String strToAddr, String strSubJect, String strHtml, String strFileNm) throws Exception {


        String host = "000.000.000.000"; //smtp host ip

        

        String from = "send@abc.com";

        

        if(!strFrAddr.equalsIgnoreCase(""))

        {

            from = strFrAddr;

        }

        String strToAddress = strToAddr;


        String content  = strHtml;

        


        Properties props = new Properties();


        ((Hashtable<Object, Object>) props).put("mail.smtp.host", host);  

        ((Hashtable<Object, Object>) props).put("mail.host", host);  

       


        Session session = Session.getDefaultInstance(props, null); 


        MimeMessage message = new MimeMessage(session); 

        InternetAddress addr = new InternetAddress(from, "Send Name", "euc-kr");    

        

        message.setFrom(addr); 

        message.setSubject(MimeUtility.encodeText(strSubJect, "euc-kr", "B"));         

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(strToAddress)); 



        MimeMultipart multipart = new MimeMultipart();         

                 

        MimeBodyPart messageBodyPart = new MimeBodyPart(); 


/*

javax.mail.SendFailedException: Sending failed;

  nested exception is: 

javax.mail.MessagingException: IOException while sending message;

  nested exception is: 

javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 

boundary="----=_Part_0_2110029122.1521717178336"

javax.mail.Transport.send0(Transport.java:219)

javax.mail.Transport.send(Transport.java:81)

에러 발생하여 아래 추가함.

*/

        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();

        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");

        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");

        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");

        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");

        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");

        CommandMap.setDefaultCommandMap(mc);

/* 추가 여기까지 */


        //messageBodyPart.setText(content);     

        messageBodyPart.setContent(content , "text/html;charset=euc-kr");     

        multipart.addBodyPart(messageBodyPart);         

        

        message.setContent(multipart); 


        Transport.send(message); 


    }

}



/


--Step 2. 

EXEC DBMS_JAVA.GRANT_PERMISSION('SCHEMA','java.util.PropertyPermission','*','read,write');

EXEC DBMS_JAVA.GRANT_PERMISSION('SCHEMA','java.net.SocketPermission','*','connect, resolve');


--Step 3. 

CREATE OR REPLACE FUNCTION FN_JAVA_MAILSEND(

    PV_FRADDR VARCHAR2,

    PV_TOADDR VARCHAR2,

    PV_SUBJECT VARCHAR2,

    PV_HTML VARCHAR2,

    PV_FILENM VARCHAR2

RETURN VARCHAR2


AS LANGUAGE JAVA


NAME 'MailSend.mailSend(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String) return java.lang.String';

/





--Step 4.  Test

SELECT FN_JAVA_MAILSEND('send@abc.com','receive@abc.com','subject','contents','') FROM DUAL;

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함