Sending e-mail from Oracle Server in 10g

本文介绍如何在Oracle 10g中配置并使用UTL_MAIL包发送电子邮件。文章详细说明了安装UTL_MAIL的过程及配置SMTP_OUT_SERVER参数的方法,并提供了发送邮件的示例。
 
 

ORACLE DATABASE ADMINISTRATOR

Sending e-mail from Oracle Server


Gregory Williams
06.22.2006
Rating: -3.66- (out of 5)


In order to send e-mail within 10g you must install and set up the UTL_MAIL package.

UTL_MAIL isn't installed when the database is installed because the SMTP_OUT_SERVER parameter must be configured. Listing 1 shows how to install UTL_MAIL and the results from the script. You must connect to the database as user SYS and run the two scripts identified in the listing.

Listing 1. Installation of UTL_MAIL

SQL> connect sys/password as sysdba
Connected. 

SQL> @$ORACLE_HOME/rdbms/admin/utlmail.sql 

Package created. 

Synonym created. 

SQL> @$ORACLE_HOME /rdbms/admin/prvtmail.plb 

Package body created. 

No errors. 

Next, the SMTP_OUT_SERVER parameter must be configured. You must connect to SYS and then use the alter system command to configure SMTP_OUT_SERVER parameter as shown here:

SQL> alter system set smtp_out_server = '<ip-address:port' scope=Both; 

System altered.

That's it! The installation and setup for UTL_MAIL is complete. The UTL_MAIL package only has one procedure, called Send, for sending e-mail (see Listing 2 for the syntax for the Send procedure). This package bundles the e-mail message and sends it to the UTL_SMTP package, and then the e-mail message is sent to the SMTP server.

Listing 2. Syntax for UTL_MAIL.SEND

UTL_MAIL.SEND( sender IN VARCHAR2, 
                                 recipients IN VARCHAR2,
                                cc IN VARCHAR2 DEFAULT NULL,
                                bcc IN VARCHAR2 DEFAULT NULL,
                                subject IN VARCHAR2 DEFAULT NULL,
                                message IN VARCHAR2,
                                mime_type IN VARCHAR2 DEFAULT
                                'text/plain; charset=us-ascii',
                                priority IN PLS_INTEGER DEFAULT  NULL);

Examples: I've created a database trigger that will send an e-mail using UTL_MAIL (see Listing 3).

Listing 3. Database shutdown using UTL_MAIL

CREATE OR REPLACE TRIGGER SCOTT.db_shutdown
before shutdown on database
begin
 utl_mail.send(
   sender => 'gjwilliams01@yahoo.com',
   recipients => ' gjwilliams01@yahoo.com',
   subject => 'Testing utl_mail',
   message => 'The receipt of this email means'||
    ' that shutting down the database'||
    ' works for UTL_MAIL '
   );
end;
/

Conclusion

There are two other procedures within the UTL_MAIL package that make life easy: UTL_MAIL.ATTACH_VARCHAR2 and UTL_MAIL.ATTACH_RAW. These procedures are for varchar2 and raw attachments, respectively.

C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test>python run.py 2025-11-01 23:26:35,185 - INFO - 开始运行测试.... 2025-11-01 23:26:35,187 - INFO - 使用环境配置:test 2025-11-01 23:26:35,187 - INFO - 基础url:http://127.0.0.1:5000 2025-11-01 23:26:35,187 - INFO - 日志路径: ./logs/test.log 2025-11-01 23:26:35,190 - INFO - 开始执行测试.. 开始运行测试 使用环境配置:test 基础url:http://127.0.0.1:5000 日志路径: ./logs/test.log 已创建报告目录 ============================= test session starts ============================= platform win32 -- Python 3.13.0, pytest-7.4.3, pluggy-1.6.0 -- C:\Users\Admin\AppData\Local\Programs\Python\Python313\python.exe cachedir: .pytest_cache metadata: {'Python': '3.13.0', 'Platform': 'Windows-11-10.0.26100-SP0', 'Packages': {'pytest': '7.4.3', 'pluggy': '1.6.0'}, 'Plugins': {'allure-pytest': '2.13.2', 'Faker': '37.5.3', 'html': '4.1.1', 'metadata': '3.1.1'}, 'CI': 'true', 'BUILD_NUMBER': '82', 'BUILD_ID': '82', 'BUILD_URL': 'http://localhost:8080/job/auto-api-test/82/', 'NODE_NAME': 'built-in', 'JOB_NAME': 'auto-api-test', 'BUILD_TAG': 'jenkins-auto-api-test-82', 'EXECUTOR_NUMBER': '0', 'JENKINS_URL': 'http://localhost:8080/', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk-21', 'WORKSPACE': 'C:\\ProgramData\\Jenkins\\.jenkins\\workspace\\auto-api-test', 'GIT_COMMIT': '7cdab6ad7e5dc35917f742387810251caa778788', 'GIT_URL': 'https://gitee.com/weike1234/api_auto_test.git', 'GIT_BRANCH': 'origin/master'} rootdir: C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test plugins: allure-pytest-2.13.2, Faker-37.5.3, html-4.1.1, metadata-3.1.1 collecting ... collected 2 items testcases/test_user_api.py::TestUserAPI::test_login[case0] PASSED [ 50%] testcases/test_user_api.py::TestUserAPI::test_login[case1] PASSED [100%]2025-11-01 23:26:35,548 - INFO - 测试执行完成,退出码0 2025-11-01 23:26:35,548 - INFO - 已生成环境配置 2025-11-01 23:26:35,548 - INFO - 测试执行器退出,退出码:None ============================== 2 passed in 0.11s ============================== 测试执行完成,退出码0 C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test>exit 0 [auto-api-test] $ C:\ProgramData\Jenkins\.jenkins\allure-2.35.1\bin\allure.bat generate C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test\reports\allure-results -c -o C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test\allure-report Report successfully generated to C:\ProgramData\Jenkins\.jenkins\workspace\auto-api-test\allure-report Allure report was successfully generated. Creating artifact for the build. Artifact was added to the build. Checking for post-build Performing post-build step Checking if email needs to be generated Email was triggered for: Always Sending email for trigger: Always Sending mail from default account using custom from address 1943564708@qq.com messageContentType = text/html; charset=UTF-8 Adding recipients from project recipient list Analyzing: 1943564708@qq.com Looking for: 1943564708@qq.com starting at: 0 firstFoundIdx: 0 firstFoundIdx-substring: 1943564708@qq.com => found type: 0 Analyzing: 1943564708@qq.com Looking for: 1943564708@qq.com starting at: 0 firstFoundIdx: 0 firstFoundIdx-substring: 1943564708@qq.com => found type: 0 Analyzing: 1943564708@qq.com Looking for: 1943564708@qq.com starting at: 0 firstFoundIdx: 0 firstFoundIdx-substring: 1943564708@qq.com => found type: 0 Adding recipients from trigger recipient list Successfully created MimeMessage Sending email to: 1943564708@qq.com DEBUG: getProvider() returning jakarta.mail.Provider[TRANSPORT,smtp,org.eclipse.angus.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=smtp.qq.com, user=我的4060TI$, password=<null> DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false 220 newxmesmtplogicsvrszb20-0.qq.com XMail Esmtp QQ Mail Server. DEBUG SMTP: connected to host "smtp.qq.com", port: 465 EHLO host.docker.internal 250-newxmesmtplogicsvrszb20-0.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250-AUTH=LOGIN 250-MAILCOMPRESS 250-SMTPUTF8 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "73400320" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "MAILCOMPRESS", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: protocolConnect login, host=smtp.qq.com, user=weike1234, password=<non-null> DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 DEBUG SMTP: Using mechanism LOGIN DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN failed AuthenticationFailedException message: 535 Login fail. Account is abnormal, service is not open, password is incorrect, login frequency limited, or system is busy. More information at https://help.mail.qq.com/detail/108/1023 这是什么问题
11-02
我改了还是报错from 为1943564708@qq.com Adding recipients from trigger recipient list Successfully created MimeMessage Sending email to: 1943564708@qq.com DEBUG: getProvider() returning jakarta.mail.Provider[TRANSPORT,smtp,org.eclipse.angus.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=smtp.qq.com, user=我的4060TI$, password=<null> DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL false 220 newxmesmtplogicsvrszb20-0.qq.com XMail Esmtp QQ Mail Server. DEBUG SMTP: connected to host "smtp.qq.com", port: 465 EHLO host.docker.internal 250-newxmesmtplogicsvrszb20-0.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250-AUTH=LOGIN 250-MAILCOMPRESS 250-SMTPUTF8 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "73400320" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "MAILCOMPRESS", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: protocolConnect login, host=smtp.qq.com, user=weike1234, password=<non-null> DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 DEBUG SMTP: Using mechanism LOGIN DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN failed AuthenticationFailedException message: 535 Login fail. Account is abnormal, service is not open, password is incorrect, login frequency limited, or system is busy. More information at https://help.mail.qq.com/detail/108/1023 Finished: SUCCESS
11-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值