01 Apex 发送邮件


CREATE OR REPLACE PROCEDURE send_mail (
	p_from IN VARCHAR2, 
	p_to IN VARCHAR2, 
	p_cc IN VARCHAR2, 
	p_subject IN varchar2, 
	p_msg IN clob
)
AS
	mailhost VARCHAR2(30) := 'IP';
	c utl_smtp.connection;
	msg clob;
	v_user varchar2(60) := 'account';
	v_p_cc varchar2(200) := p_cc;
	v_pass varchar2(60) := '00000000';
	v_p_cc_count number(8) := 0;
	v_db_nls_characterset varchar2(40);
BEGIN
	c := utl_smtp.open_connection(mailhost, 25);
	utl_smtp.ehlo(c, mailhost);
	UTL_SMTP.command(c, 'AUTH LOGIN');
	UTL_SMTP.command(c, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_user))));
	UTL_SMTP.command(c, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(v_pass))));
	utl_smtp.mail(c, '<' || p_from || '>');
	IF instr(p_to, ';') = 0 THEN
		utl_smtp.rcpt(c, '<' || p_to || '>');
	ELSE
		utl_smtp.rcpt(c, p_to);
	END IF;
	v_p_cc_count := regexp_count(p_cc, ',');
	IF regexp_count(p_cc, ',') > 0 THEN
		FOR i IN 1..v_p_cc_count + 1
		LOOP
			IF instr(v_p_cc, ',') > 0 THEN
				utl_smtp.rcpt(c, '<' || substr(v_p_cc, 0, instr(v_p_cc, ',') - 1) || '>');
				v_p_cc := substr(v_p_cc, instr(v_p_cc, ',') + 1, length(v_p_cc));
			ELSE
				utl_smtp.rcpt(c, '<' || v_p_cc || '>');
			END IF;
		END LOOP;
	ELSE
		utl_smtp.rcpt(c, '<' || p_cc || '>');
	END IF;
	utl_smtp.rcpt(c, 'mail');
	utl_smtp.open_data(c);
	SELECT value
	INTO v_db_nls_characterset
	FROM nls_database_parameters
	WHERE PARAMETER = 'NLS_CHARACTERSET';
	IF 1 = 1 THEN
		msg := p_msg;
		UTL_SMTP.write_data(c, 'From:' || p_from || utl_tcp.CRLF);
		UTL_SMTP.write_data(c, 'To:' || p_to || utl_tcp.crlf);
		UTL_SMTP.write_data(c, 'Cc:' || p_cc || utl_tcp.crlf);
		UTL_SMTP.write_data(c, 'Bcc:mail' || utl_tcp.crlf);
		UTL_SMTP.write_raw_data(c, UTL_RAW.cast_to_raw('Subject:' || p_subject || utl_tcp.CRLF));
		UTL_SMTP.write_raw_data(c, UTL_RAW.cast_to_raw(convert('Content-Type:text/html;charset=UTF-8' || utl_tcp.CRLF, 'ZHS16GBK')));
		UTL_SMTP.write_raw_data(c, UTL_RAW.cast_to_raw(convert('X-CM-HeaderCharset: UTF-8' || utl_tcp.CRLF, 'ZHS16GBK')));
		UTL_SMTP.write_raw_data(c, UTL_RAW.cast_to_raw(convert('Content-Language: zh-cn' || utl_tcp.CRLF, 'ZHS16GBK')));
		UTL_SMTP.write_data(c, utl_tcp.CRLF);
		UTL_SMTP.write_raw_data(c, UTL_RAW.cast_to_raw(convert(msg, 'ZHS16GBK', v_db_nls_characterset)));
	END IF;
	utl_smtp.close_data(c);
	utl_smtp.quit(c);
EXCEPTION
	WHEN OTHERS THEN dbms_output.put_line(dbms_utility.format_error_stack);
END;


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部