PROCEDURE P_SPLITE_STR (P_STR VARCHAR2, P_SPLITE_FLAG INT DEFAULT 1 )
IS
L_ADDR VARCHAR2 (254) := '';
L_LEN INT;
L_STR VARCHAR2 (4000);
J INT := 0; --表示郵件地址或者附件的個數
BEGIN
/*處理接收郵件地址列表,包括去空格、將;轉換為,等*/
L_STR :=
TRIM (RTRIM (REPLACE (REPLACE (P_STR, ';', ','), ' ', ''), ','));
L_LEN := LENGTH (L_STR);
FOR I IN 1 .. L_LEN
LOOP
IF SUBSTR (L_STR, I, 1) <> ','
THEN
L_ADDR := L_ADDR || SUBSTR (L_STR, I, 1);
ELSE
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN --表示處理郵件地址
--前後需要加上'<>',否則很多郵箱將不能發送郵件
L_ADDR := '<' || L_ADDR || '>';
--調用郵件發送過程
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN --表示處理附件名稱
MY_ACCT_LIST (J) := L_ADDR;
END IF;
L_ADDR := '';
END IF;
IF I = L_LEN
THEN
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN
--調用郵件發送過程
L_ADDR := '<' || L_ADDR || '>';
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN
MY_ACCT_LIST (J) := L_ADDR;
END IF;
END IF;
END LOOP;
END;
------------------------------------------------寫郵件頭和郵件內容------------------------------------------
PROCEDURE WRITE_DATA (P_CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
P_NAME IN VARCHAR2,
P_VALUE IN VARCHAR2,
P_SPLITE VARCHAR2 DEFAULT ':' ,
P_CRLF VARCHAR2 DEFAULT L_CRLF )
IS
BEGIN
/* utl_raw.cast_to_raw 對解決中文亂碼問題很重要*/
UTL_SMTP.WRITE_RAW_DATA (
P_CONN,
UTL_RAW.CAST_TO_RAW (
CONVERT (P_NAME || P_SPLITE || P_VALUE || P_CRLF, 'ZHS16GBK')
)
);
END;
----------------------------------------寫MIME郵件尾部-----------------------------------------------------
PROCEDURE END_BOUNDARY (CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
LAST IN BOOLEAN DEFAULT FALSE )
IS
BEGIN
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
IF (LAST)
THEN
UTL_SMTP.WRITE_DATA (CONN, LAST_BOUNDARY);
END IF;
END;
----------------------------------------------發送附件----------------------------------------------------
PROCEDURE ATTACHMENT (
CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
MIME_TYPE IN VARCHAR2 DEFAULT 'text/plain' ,
INLINE IN BOOLEAN DEFAULT TRUE ,
FILENAME IN VARCHAR2 DEFAULT 't.txt' ,
TRANSFER_ENC IN VARCHAR2 DEFAULT '7 bit' ,
DT_NAME IN VARCHAR2 DEFAULT '0'
)
IS
L_FILENAME VARCHAR2 (1000);
BEGIN
--寫附件頭
UTL_SMTP.WRITE_DATA (CONN, FIRST_BOUNDARY);
--設置附件格式
WRITE_DATA (CONN, 'Content-Type', MIME_TYPE);
--如果文件名稱非空,表示有附件
DROP_DIRECTORY (DT_NAME);
--創建directory
CREATE_DIRECTORY (DT_NAME, GET_FILE (FILENAME, 1));
--得到附件文件名稱
L_FILENAME := GET_FILE (FILENAME, 2);
IF (INLINE)
THEN
WRITE_DATA (CONN,
'Content-Disposition',
'inline; filename="' || L_FILENAME || '"');
ELSE
WRITE_DATA (CONN,
'Content-Disposition',
'attachment; filename="' || L_FILENAME || '"');
END IF;
--設置附件的轉換格式
IF (TRANSFER_ENC IS NOT NULL)
THEN
WRITE_DATA (CONN, 'Content-Transfer-Encoding', TRANSFER_ENC);
END IF;
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
--begin 貼附件內容
IF TRANSFER_ENC = 'bit 7'
THEN
--如果是文本類型的附件
BEGIN
L_FILE_HANDLE := UTL_FILE.FOPEN (DT_NAME, L_FILENAME, 'r'); --打開文件
--把附件分成多份,這樣可以發送超過32K的附件
LOOP
UTL_FILE.GET_LINE (L_FILE_HANDLE, L_LINE);
L_MESG := L_LINE || L_CRLF;
WRITE_DATA (CONN,
'',
L_MESG,
'',
'');
END LOOP;
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
EXCEPTION
WHEN OTHERS
THEN
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
NULL;
END; --結束文本類型附件的處理
ELSIF TRANSFER_ENC = 'base64'
THEN
--如果是二進制類型的附件
BEGIN