我是Toad,SQLPlus和Oracle的新手。我试图使用UTL_SMTP包运行以下查询。
DECLARE
v_From VARCHAR2(80) := 'noreply@myemail.com';
v_Recipient VARCHAR2(80) := 'MyEmail@myemail.com';
v_Subject VARCHAR2(80) := 'Test with attachment';
v_Mail_Host VARCHAR2(30) := 'smtpserver.mycompany.com';
v_Mail_Conn utl_smtp.Connection;
crlf VARCHAR2(2) := chr(13)||chr(10);
BEGIN
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_Recipient || crlf ||
'MIME-Version: 1.0'|| crlf || -- Use MIME mail standard
'Content-Type: multipart/mixed;'|| crlf ||
' boundary="-----SECBOUND"'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
'Content-Transfer_Encoding: 7bit'|| crlf ||
crlf ||
'some message text'|| crlf || -- Message body
'more message text'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="excel.csv"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="TextFile.txt"'|| crlf ||
crlf ||
'Hello World'|| crlf || -- Content of attachment
crlf ||
'-------SECBOUND--' -- End MIME mail
);
utl_smtp.Quit(v_mail_conn);
EXCEPTION
WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
raise_application_error(-20000, 'Unable to send mail: '||sqlerrm);
END;
/
如果我在SQL Plus中运行该程序包(Connect / as SYSDBA),它运行正常,我收到附带附件的电子邮件。
如果我在Toad(Connected as SYSTEM)中运行包,则会收到以下错误消息。
[Error] ORA-06550: line 6, column 17:
PLS-00201: identifier 'UTL_SMTP' must be declared
ORA-06550: line 6, column 17:
我正在运行本地Oracle Database 11g Express Edition版本11.2.0.2.0 64位
答案 0 :(得分:1)
套餐有哪些特权?这些是默认12c安装的权限:
select grantee, privilege from dba_tab_privs where table_name = 'UTL_SMTP';
GRANTEE PRIVILEGE
------- ---------
PUBLIC EXECUTE
APEX_040200 EXECUTE
如果该PUBLIC拨款被撤销,那么即使SYSTEM也无法使用该套餐。您可以通过以SYS身份登录并执行grant execute on sys.utl_smtp to public;
来解决此问题。如果授予被撤销,您可能想知道原因。某些组织可能有反对使用该程序包的策略,或者可能要求将其授予特定用户。
另外,请考虑使用更新,更简单的软件包UTL_MAIL。