oracle 发送邮件 实现方法
CREATE OR REPLACE PROCEDURE PROCSENDEMAIL(P_TXT VARCHAR2,<BR> P_SUB VARCHAR2,<BR> P_SENDOR VARCHAR2,<BR> P_RECEIVER VARCHAR2,<BR> P_SERVER VARCHAR2,<BR> P_PORT NUMBER DEFAULT 25,<BR> P_NEED_SMTP INT DEFAULT 0,<BR> P_USER VARCHAR2 DEFAULT NULL,<BR> P_PASS VARCHAR2 DEFAULT NULL,<BR> P_FILENAME VARCHAR2 DEFAULT NULL,<BR> P_ENCODE VARCHAR2 DEFAULT 'bit 7')<BR> AUTHID CURRENT_USER IS<BR> /*<BR> 作用:用oracle发送邮件<BR> 主要功能:1、支持多收件人。<BR> 2、支持中文<BR> 3、支持抄送人<BR> 4、支持大于32K的附件<BR> 5、支持多行正文<BR> 6、支持多附件<BR> 7、支持文本附件和二进制附件<BR> 8、支持HTML格式<BR> 8、支持<BR> 作者:suk<BR> 参数说明:<BR> p_txt :邮件正文<BR> p_sub: 邮件标题<BR> p_SendorAddress : 发送人邮件地址<BR> p_ReceiverAddress : 接收地址,可以同时发送到多个地址上,地址之间用","或者";"隔开<BR> p_EmailServer : 邮件服务器地址,可以是域名或者IP<BR> p_Port :邮件服务器端口<BR> p_need_smtp:是否需要smtp认证,0表示不需要,1表示需要<BR> p_user:smtp验证需要的用户名<BR> p_pass:smtp验证需要的密码<BR> p_filename:附件名称,必须包含完整的路径,如"d:tempa.txt"。<BR> 可以有多个附件,附件名称只见用逗号或者分号分隔<BR> p_encode:附件编码转换格式,其中 p_encode='bit 7' 表示文本类型附件<BR> p_encode='base64' 表示二进制类型附件<BR> 注意:<BR> 1、对于文本类型的附件,不能用base64的方式发送,否则出错<BR> 2、对于多个附件只能用同一种格式发送<BR> */<BR> L_CRLF VARCHAR2(2) := UTL_TCP.CRLF;<BR> L_SENDORADDRESS VARCHAR2(4000);<BR> L_SPLITE VARCHAR2(10) := '++';<BR> BOUNDARY CONSTANT VARCHAR2(256) := '-----BYSUK';<BR> FIRST_BOUNDARY CONSTANT VARCHAR2(256) := '--' || BOUNDARY || L_CRLF;<BR> LAST_BOUNDARY CONSTANT VARCHAR2(256) := '--' || BOUNDARY || '--' ||<BR> L_CRLF;<BR> MULTIPART_MIME_TYPE CONSTANT VARCHAR2(256) := 'multipart/mixed; boundary="' ||<BR> BOUNDARY || '"';<BR> /* 以下部分是发送大二进制附件时用到的变量 */<BR> L_FIL BFILE;<BR> L_FILE_LEN NUMBER;<BR> L_MODULO NUMBER;<BR> L_PIECES NUMBER;<BR> L_FILE_HANDLE UTL_FILE.FILE_TYPE;<BR> L_AMT BINARY_INTEGER := 672 * 3; /* ensures proper format; 2016 */<BR> L_FILEPOS PLS_INTEGER := 1; /* pointer for the file */<BR> L_CHUNKS NUMBER;<BR> L_BUF RAW(2100);<BR> L_DATA RAW(2100);<BR> L_MAX_LINE_WIDTH NUMBER := 54;<BR> L_DIRECTORY_BASE_NAME VARCHAR2(100) := 'DIR_FOR_SEND_MAIL';<BR> L_LINE VARCHAR2(1000);<BR> L_MESG VARCHAR2(32767);<BR> /* 以上部分是发送大二进制附件时用到的变量 */<BR> TYPE ADDRESS_LIST IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;<BR> MY_ADDRESS_LIST ADDRESS_LIST;<BR> TYPE ACCT_LIST IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;<BR> MY_ACCT_LIST ACCT_LIST;<BR> -------------------------------------返回附件源文件所在目录或者名称--------------------------------------<BR> FUNCTION GET_FILE(P_FILE VARCHAR2,<BR> P_GET INT) RETURN VARCHAR2 IS<BR> --p_get=1 表示返回目录<BR> --p_get=2 表示返回文件名<BR> L_FILE VARCHAR2(1000);<BR> BEGIN<BR> IF INSTR(P_FILE, '') > 0 THEN<BR> --windows<BR> IF P_GET = 1 THEN<BR> L_FILE := SUBSTR(P_FILE, 1, INSTR(P_FILE, '', -1) - 1);<BR> ELSIF P_GET = 2 THEN<BR> L_FILE := SUBSTR(P_FILE, - (LENGTH(P_FILE) - INSTR(P_FILE, '', -1)));<BR> END IF;<BR> ELSIF INSTR(P_FILE, '/') > 0 THEN<BR> --linux/unix<BR> IF P_GET = 1 THEN<BR> L_FILE := SUBSTR(P_FILE, 1, INSTR(P_FILE, '/', -1) - 1);<BR> ELSIF P_GET = 2 THEN<BR> L_FILE := SUBSTR(P_FILE, - (LENGTH(P_FILE) - INSTR(P_FILE, '/', -1)));<BR> END IF;<BR> END IF;<BR> RETURN L_FILE;<BR> END;<BR> ---------------------------------------------删除directory------------------------------------<BR> PROCEDURE DROP_DIRECTORY(P_DIRECTORY_NAME VARCHAR2) IS<BR> BEGIN<BR> EXECUTE IMMEDIATE 'drop directory ' || P_DIRECTORY_NAME;<BR> EXCEPTION<BR> WHEN OTHERS THEN<BR> NULL;<BR> END;<BR> --------------------------------------------------创建directory-----------------------------------------<BR> PROCEDURE CREATE_DIRECTORY(P_DIRECTORY_NAME VARCHAR2,<BR> P_DIR VARCHAR2) IS<BR> BEGIN<BR> EXECUTE IMMEDIATE 'create directory ' || P_DIRECTORY_NAME || ' as ''' ||<BR> P_DIR || '''';<BR> EXECUTE IMMEDIATE 'grant read,write on directory ' || P_DIRECTORY_NAME ||<BR> ' to public';<BR> EXCEPTION<BR> WHEN OTHERS THEN<BR> RAISE;<BR> END;<BR> --------------------------------------------分割邮件地址或者附件地址-----------------------------------<BR> PROCEDURE P_SPLITE_STR(P_STR VARCHAR2,<BR> P_SPLITE_FLAG INT DEFAULT 1) IS<BR> L_ADDR VARCHAR2(254) := '';<BR> L_LEN INT;<BR> L_STR VARCHAR2(4000);<BR> J INT := 0; --表示邮件地址或者附件的个数<BR> BEGIN<BR> /*处理接收邮件地址列表,包括去空格、将;转换为,等*/<BR> L_STR := TRIM(RTRIM(REPLACE(REPLACE(P_STR, ';', ','), ' ', ''), ','));<BR> L_LEN := LENGTH(L_STR);<BR> FOR I IN 1 .. L_LEN LOOP<BR> IF SUBSTR(L_STR, I, 1) <> ',' THEN<BR> L_ADDR := L_ADDR || SUBSTR(L_STR, I, 1);<BR> ELSE<BR> J := J + 1;<BR> IF P_SPLITE_FLAG = 1 THEN --表示处理邮件地址 <BR> --前后需要加上'<>',否则很多邮箱将不能发送邮件<BR> L_ADDR := '<' || L_ADDR || '>';<BR> --调用邮件发送过程<BR> MY_ADDRESS_LIST(J) := L_ADDR;<BR> ELSIF P_SPLITE_FLAG = 2 THEN --表示处理附件名称<BR> MY_ACCT_LIST(J) := L_ADDR;<BR> END IF;<BR> L_ADDR := '';<BR> END IF;<BR> IF I = L_LEN THEN<BR> J := J + 1;<BR> IF P_SPLITE_FLAG = 1 THEN<BR> --调用邮件发送过程<BR> L_ADDR := '<' || L_ADDR || '>';<BR> MY_ADDRESS_LIST(J) := L_ADDR;<BR> ELSIF P_SPLITE_FLAG = 2 THEN<BR> MY_ACCT_LIST(J) := L_ADDR;<BR> END IF;<BR> END IF;<BR> END LOOP;<BR> END;<BR> ------------------------------------------------写邮件头和邮件内容------------------------------------------<BR> PROCEDURE WRITE_DATA(P_CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,<BR> P_NAME IN VARCHAR2,<BR> P_VALUE IN VARCHAR2,<BR> P_SPLITE VARCHAR2 DEFAULT ':',<BR> P_CRLF VARCHAR2 DEFAULT L_CRLF) IS<BR> BEGIN<BR> /* utl_raw.cast_to_raw 对解决中文乱码问题很重要*/<BR> UTL_SMTP.WRITE_RAW_DATA(P_CONN, UTL_RAW.CAST_TO_RAW(CONVERT(P_NAME ||<BR> P_SPLITE ||<BR> P_VALUE ||<BR> P_CRLF, 'ZHS16GBK')));<BR> END;<BR> ----------------------------------------写MIME邮件尾部-----------------------------------------------------<BR> PROCEDURE (CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,<BR> LAST IN BOOLEAN DEFAULT FALSE) IS<BR> BEGIN<BR> UTL_SMTP.WRITE_DATA(CONN, UTL_TCP.CRLF);<BR> IF (LAST) THEN<BR> UTL_SMTP.WRITE_DATA(CONN, LAST_BOUNDARY);<BR> END IF;<BR> END;<BR> ----------------------------------------------发送附件----------------------------------------------------<BR> PROCEDURE ATTACHMENT(CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,<BR> MIME_TYPE IN VARCHAR2 DEFAULT 'text/plain',<BR> INLINE IN BOOLEAN DEFAULT TRUE,<BR> FILENAME IN VARCHAR2 DEFAULT 't.txt',<BR> TRANSFER_ENC IN VARCHAR2 DEFAULT '7 bit',<BR> DT_NAME IN VARCHAR2 DEFAULT '0') IS<br><br> L_FILENAME VARCHAR2(1000);<BR> BEGIN<BR> --写附件头<BR> UTL_SMTP.WRITE_DATA(CONN, FIRST_BOUNDARY);<BR> --设置附件格式<BR> WRITE_DATA(CONN, 'Content-Type', MIME_TYPE);<BR> --如果文件名称非空,表示有附件<BR> DROP_DIRECTORY(DT_NAME);<BR> --创建directory<BR> CREATE_DIRECTORY(DT_NAME, GET_FILE(FILENAME, 1));<BR> --得到附件文件名称<BR> L_FILENAME := GET_FILE(FILENAME, 2);<BR> IF (INLINE) THEN<BR> WRITE_DATA(CONN, 'Content-Disposition', 'inline; filename="' ||<BR> L_FILENAME || '"');<BR> ELSE<BR> WRITE_DATA(CONN, 'Content-Disposition', 'attachment; filename="' ||<BR> L_FILENAME || '"');<BR> END IF;<BR> --设置附件的转换格式<BR> IF (TRANSFER_ENC IS NOT NULL) THEN<BR> WRITE_DATA(CONN, 'Content-Transfer-Encoding', TRANSFER_ENC);<BR> END IF;<br><br> UTL_SMTP.WRITE_DATA(CONN, UTL_TCP.CRLF);<br><br> --begin 贴附件内容<BR> IF TRANSFER_ENC = 'bit 7' THEN<BR> --如果是文本类型的附件<BR> BEGIN<BR> L_FILE_HANDLE := UTL_FILE.FOPEN(DT_NAME, L_FILENAME, 'r'); --打开文件<BR> --把附件分成多份,这样可以发送超过32K的附件<BR> LOOP<BR> UTL_FILE.GET_LINE(L_FILE_HANDLE, L_LINE);<BR> L_MESG := L_LINE || L_CRLF;<BR> WRITE_DATA(CONN, '', L_MESG, '', '');<BR> END LOOP;<BR> UTL_FILE.FCLOSE(L_FILE_HANDLE);<BR> (CONN);<BR> EXCEPTION<BR> WHEN OTHERS THEN<BR> UTL_FILE.FCLOSE(L_FILE_HANDLE);<BR> (CONN);<BR> NULL;<BR> END; --结束文本类型附件的处理<br><br> ELSIF TRANSFER_ENC = 'base64' THEN<BR> --如果是二进制类型的附件<BR> BEGIN<BR> --把附件分成多份,这样可以发送超过32K的附件<BR> L_FILEPOS := 1;--重置offset,在发送多个附件时,必须重置<BR> L_FIL := BFILENAME(DT_NAME, L_FILENAME);<BR> L_FILE_LEN := DBMS_LOB.GETLENGTH(L_FIL);<BR> L_MODULO := MOD(L_FILE_LEN, L_AMT);<BR> L_PIECES := TRUNC(L_FILE_LEN / L_AMT);<BR> IF (L_MODULO <> 0) THEN<BR> L_PIECES := L_PIECES + 1;<BR> END IF;<BR> DBMS_LOB.FILEOPEN(L_FIL, DBMS_LOB.FILE_READONLY);<BR> DBMS_LOB.READ(L_FIL, L_AMT, L_FILEPOS, L_BUF);<BR> L_DATA := NULL;<BR> FOR I IN 1 .. L_PIECES LOOP<BR> L_FILEPOS := I * L_AMT + 1;<BR> L_FILE_LEN := L_FILE_LEN - L_AMT;<BR> L_DATA := UTL_RAW.CONCAT(L_DATA, L_BUF);<BR> L_CHUNKS := TRUNC(UTL_RAW.LENGTH(L_DATA) / L_MAX_LINE_WIDTH);<BR> IF (I <> L_PIECES) THEN<BR> L_CHUNKS := L_CHUNKS - 1;<BR> END IF;<BR> UTL_SMTP.WRITE_RAW_DATA(CONN, UTL_ENCODE.BASE64_ENCODE(L_DATA));<BR> L_DATA := NULL;<BR> IF (L_FILE_LEN < L_AMT AND L_FILE_LEN > 0) THEN<BR> L_AMT := L_FILE_LEN;<BR> END IF;<BR> DBMS_LOB.READ(L_FIL, L_AMT, L_FILEPOS, L_BUF);<BR> END LOOP;<BR> DBMS_LOB.FILECLOSE(L_FIL);<BR> (CONN);<BR> EXCEPTION<BR> WHEN OTHERS THEN<BR> DBMS_LOB.FILECLOSE(L_FIL);<BR> (CONN);<BR> RAISE;<BR> END; --结束处理二进制附件<br><br> END IF; --结束处理附件内容<BR> DROP_DIRECTORY(DT_NAME);<BR> END; --结束过程ATTACHMENT<BR> ---------------------------------------------真正发送邮件的过程--------------------------------------------<BR> PROCEDURE P_EMAIL(P_SENDORADDRESS2 VARCHAR2, --发送地址<BR> P_RECEIVERADDRESS2 VARCHAR2) --接受地址<BR> IS<BR> L_CONN UTL_SMTP.CONNECTION; --定义连接<BR> BEGIN<BR> /*初始化邮件服务器信息,连接邮件服务器*/<BR> L_CONN := UTL_SMTP.OPEN_CONNECTION(P_SERVER, P_PORT);<BR> UTL_SMTP.HELO(L_CONN, P_SERVER);<BR> /* smtp服务器登录校验 */<BR> IF P_NEED_SMTP = 1 THEN<BR> UTL_SMTP.COMMAND(L_CONN, 'AUTH LOGIN', '');<BR> UTL_SMTP.COMMAND(L_CONN, UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.BASE64_ENCODE(UTL_RAW.CAST_TO_RAW(P_USER))));<BR> UTL_SMTP.COMMAND(L_CONN, UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.BASE64_ENCODE(UTL_RAW.CAST_TO_RAW(P_PASS))));<BR> END IF;<br><br> /*设置发送地址和接收地址*/<BR> UTL_SMTP.MAIL(L_CONN, P_SENDORADDRESS2);<BR> UTL_SMTP.RCPT(L_CONN, P_RECEIVERADDRESS2);<br><br> /*设置邮件头*/<BR> UTL_SMTP.OPEN_DATA(L_CONN);<br><br> WRITE_DATA(L_CONN, 'Date', TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));<BR> /*设置发送人*/<BR> WRITE_DATA(L_CONN, 'From', P_SENDOR);<BR> /*设置接收人*/<BR> WRITE_DATA(L_CONN, 'To', P_RECEIVER);<BR> /*设置邮件主题*/<BR> WRITE_DATA(L_CONN, 'Subject', P_SUB);<br><br> WRITE_DATA(L_CONN, 'Content-Type', MULTIPART_MIME_TYPE);<BR> UTL_SMTP.WRITE_DATA(L_CONN, UTL_TCP.CRLF);<BR> UTL_SMTP.WRITE_DATA(L_CONN, FIRST_BOUNDARY);<BR> WRITE_DATA(L_CONN, 'Content-Type', 'text/plain;charset=gb2312');<BR> --单独空一行,否则,正文内容不显示<BR> UTL_SMTP.WRITE_DATA(L_CONN, UTL_TCP.CRLF);<BR> /* 设置邮件正文<BR> 把分隔符还原成chr(10)。这主要是为了shell中调用该过程,如果有多行,则先把多行的内容合并成一行,
并用 l_splite分隔 然后用 l_crlf替换chr(10)。这一步是必须的,否则将不能发送邮件正文有多行的邮件<br><br> */<BR> WRITE_DATA(L_CONN, '', REPLACE(REPLACE(P_TXT, L_SPLITE, CHR(10)), CHR(10), L_CRLF), '', '');<BR> (L_CONN);<br><br> --如果文件名称不为空,则发送附件<BR> IF (P_FILENAME IS NOT NULL) THEN<BR> --根据逗号或者分号拆分附件地址<BR> P_SPLITE_STR(P_FILENAME, 2);<BR> --循环发送附件(在同一个邮件中)<BR> FOR K IN 1 .. MY_ACCT_LIST.COUNT LOOP<BR> ATTACHMENT(CONN => L_CONN, FILENAME => MY_ACCT_LIST(K), TRANSFER_ENC =>
P_ENCODE, DT_NAME => L_DIRECTORY_BASE_NAME ||<BR> TO_CHAR(K));<BR> END LOOP;<BR> END IF;<br><br> /*关闭数据写入*/<BR> UTL_SMTP.CLOSE_DATA(L_CONN);<BR> /*关闭连接*/<BR> UTL_SMTP.QUIT(L_CONN);<br><br> /*异常处理*/<BR> EXCEPTION<BR> WHEN OTHERS THEN<BR> NULL;<BR> RAISE;<br><br> END;<BR> ---------------------------------------------------主过程-----------------------------------------------------<BR>BEGIN<BR> L_SENDORADDRESS := '<' || P_SENDOR || '>';<BR> P_SPLITE_STR(P_RECEIVER);--处理邮件地址<BR> FOR K IN 1 .. MY_ADDRESS_LIST.COUNT LOOP<BR> P_EMAIL(L_SENDORADDRESS, MY_ADDRESS_LIST(K));<BR> END LOOP;<BR> /*处理邮件地址,根据逗号分割邮件*/<BR>EXCEPTION<BR> WHEN OTHERS THEN<BR> RAISE;<BR>END;<BR>
<PRE><BLOCKQUOTE> </BLOCKQUOTE>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END