PLSQL NOTE--------network utl_smtp package

use utl_smtp to send mail

create or replace procedure mail_send( message   in varchar2,
                                       sender    in varchar2,
                                       recipient in varchar2,
                                       subject   in varchar2 default ‘my mail test‘) is
    mailhost varchar2(30) := ‘smtp.163.com‘;
    c        utl_smtp.connection;
    msg      varchar2(1000);
begin
    msg := ‘Date: ‘ || to_char(sysdate - 1, ‘dd mon yy hh24:mi:ss‘) || UTL_TCP.CRLF ||
           ‘From: <‘ || sender || ‘>‘ || UTL_TCP.CRLF ||
           ‘subject: ‘ || subject || UTL_TCP.CRLF ||
           ‘To: <‘ || recipient || ‘>‘ || UTL_TCP.CRLF ||
           ‘‘ || UTL_TCP.CRLF || message;
    dbms_output.put_line(msg);
    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(‘&username‘))));
    utl_smtp.command(c, utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(‘&password‘))));
    utl_smtp.mail(c, sender);
    utl_smtp.rcpt(c, recipient);
    utl_smtp.open_data(c);
    utl_smtp.write_raw_data(c, utl_raw.cast_to_raw(msg));
    utl_smtp.close_data(c);
    utl_smtp.quit(c);
EXCEPTION
    WHEN others THEN
    BEGIN
        dbms_output.put_line (‘send_message error: ‘||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE );
        dbms_output.put_line (‘send_message error: ‘||sqlerrm );
        utl_smtp.quit(c);
        exception when others then
            BEGIN
                dbms_output.put_line (‘exception error: ‘||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE );
                dbms_output.put_line (‘exception error: ‘||sqlerrm );
            end;
    END;
END;
/

Notes
1.port need to change according to server setting. For example gmail smtp port is 465.
2.use utl_smtp.command to use Third-party mailbox.
3.addresses of sender and receiver must append string "<" and ">" .
4.use utl_smtp.write_raw_data instead of write_data to solve chinese character garbled.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

use utl_tcp to send (not tested)

CREATE OR REPLACE PROCEDURE SEND_MAIL (
   msg_from    varchar2 := ‘[email protected]‘,    ----- MAIL BOX SENDING THE EMAIL
  msg_to      varchar2 := ‘[email protected]‘,    ----- MAIL BOX RECIEVING THE EMAIL
   msg_subject varchar2 := ‘Output file TEST1‘,          ----- EMAIL SUBJECT
   msg_text    varchar2 := ‘THIS IS THE TEXT OF THE EMAIL MESSAGE.‘,
   v_output1   varchar2 := ‘THIS IS THE TEXT OF THE ATTACHMENT FILE. THIS TEXT WILL BE IN A TEXT FILE ATTACHED TO THE EMAIL.‘)
 IS
   c  utl_tcp.connection;
   rc integer;
   crlf VARCHAR2(2):= CHR(13)||CHR(10);
   mesg VARCHAR2(32767);
 BEGIN
   c := utl_tcp.open_connection(‘196.35.140.18‘, 25);       ----- OPEN SMTP PORT CONNECTION
   rc := utl_tcp.write_line(c, ‘HELO 196.35.140.18‘);       ----- PERFORMS HANDSHAKING WITH SMTP SERVER
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘EHLO 196.35.140.18‘);       ----- PERFORMS HANDSHAKING, INCLUDING EXTRA INFORMATION
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘MAIL FROM: ‘||msg_from);    ----- MAIL BOX SENDING THE EMAIL
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘RCPT TO: ‘||msg_to);        ----- MAIL BOX RECIEVING THE EMAIL
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘DATA‘);                     ----- EMAIL MESSAGE BODY START
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘Date: ‘||TO_CHAR( SYSDATE, ‘dd Mon yy hh24:mi:ss‘ ));
   rc := utl_tcp.write_line(c, ‘From: ‘||msg_from||‘ <‘||msg_from||‘>‘);
   rc := utl_tcp.write_line(c, ‘MIME-Version: 1.0‘);
   rc := utl_tcp.write_line(c, ‘To: ‘||msg_to||‘ <‘||msg_to||‘>‘);
   rc := utl_tcp.write_line(c, ‘Subject: ‘||msg_subject);
   rc := utl_tcp.write_line(c, ‘Content-Type: multipart/mixed;‘);  ----- INDICATES THAT THE BODY CONSISTS OF MORE THAN ONE PART
   rc := utl_tcp.write_line(c, ‘ boundary="-----SECBOUND"‘);       ----- SEPERATOR USED TO SEPERATE THE BODY PARTS
   rc := utl_tcp.write_line(c, );                                ----- DO NOT REMOVE THIS BLANK LINE - PART OF MIME STANDARD
   rc := utl_tcp.write_line(c, ‘-------SECBOUND‘);
   rc := utl_tcp.write_line(c, ‘Content-Type: text/plain‘); ----- 1ST BODY PART. EMAIL TEXT MESSAGE
   rc := utl_tcp.write_line(c, ‘Content-Transfer-Encoding: 7bit‘);
   rc := utl_tcp.write_line(c, );
   rc := utl_tcp.write_line(c, msg_text); ----- TEXT OF EMAIL MESSAGE
   rc := utl_tcp.write_line(c, );
   rc := utl_tcp.write_line(c, ‘-------SECBOUND‘);
   rc := utl_tcp.write_line(c, ‘Content-Type: text/plain;‘); ----- 2ND BODY PART.
   rc := utl_tcp.write_line(c, ‘ name="Test.txt"‘);
   rc := utl_tcp.write_line(c, ‘Content-Transfer_Encoding: 8bit‘);
   rc := utl_tcp.write_line(c, ‘Content-Disposition: attachment;‘); ----- INDICATES THAT THIS IS AN ATTACHMENT
   rc := utl_tcp.write_line(c, ‘ filename="Test.txt"‘); ----- SUGGESTED FILE NAME FOR ATTACHMENT
   rc := utl_tcp.write_line(c, );
   rc := utl_tcp.write_line(c, v_output1);
   rc := utl_tcp.write_line(c, ‘-------SECBOUND--‘);
   rc := utl_tcp.write_line(c, );
   rc := utl_tcp.write_line(c, ‘.‘); ----- EMAIL MESSAGE BODY END
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   rc := utl_tcp.write_line(c, ‘QUIT‘); ----- ENDS EMAIL TRANSACTION
   dbms_output.put_line(utl_tcp.get_line(c, TRUE));
   utl_tcp.close_connection(c); ----- CLOSE SMTP PORT CONNECTION
   END;
   /
时间: 2024-11-15 09:44:04

PLSQL NOTE--------network utl_smtp package的相关文章

plsql的程序包package

9. 程序包--PACKAGE 9.1 包的定义和编译 包:一个PLSQL相关对象的逻辑分组和单个对象存储在数据库对象中的数据单元.相关的PLSQL对象包括:常量.变量.游标.异常.SP.FUN 包由两部分组成: 规范部分(包头.调用接口)  +  主体部分(包体.实现部分) (1) 包头的创建: create or replace package org_Master is     max_sites_for_an_org number;    type rc is ref cursor;  

PLSQL NOTE --------like 与转义字符

SQL> create table test 2 (id integer, 3 name varchar2(90)); Table created. SQL> insert into test values(100,'aassdd'); 1 row created. SQL> insert into test values(120,null); 1 row created. SQL> insert into test values(110,'aa_see'); 1 row crea

PLSQL note

sql%count 最近一次sql执行的件数SUBSTR(string , int i) // i番目から最後までの文字列を切り取るSUBSTR(string , int i, int j) // i番目からj文二の文字列を切り取るNVL(para, string) // paraはnullであれば.string を戻るFLOOR( i) // iより小さく.一番大きい整数を戻るCEIL(i) // iより大きく.一番小さい整数を戻るROUND(i) // 四捨五入TO_CHAR() //sel

Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email

Check SMTP Server Availability for ORA-29278 or ORA-29279 errors using UTL_SMTP to Send Email. (文档 ID 604763.1) 转到底部 修改时间:2014-5-13类型:PROBLEM 为此文档评级 通过电子邮件发送此文档的链接 在新窗口中打开文档 In this Document   Symptoms   Cause   Solution   References APPLIES TO: PL/S

How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)

APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1]Information in this document applies to any platform.***Checked for relevance on 07-Apr-2014*** GOAL The UTL_SMTP package is designed for sending electronic mails (emails) over Sim

How to Use the UTL_MAIL Package

APPLIES TO: PL/SQL - Version 10.1.0.2 and laterInformation in this document applies to any platform."Checked for relevance on 06-Mar-2012" GOAL This note shows how to send emails with the UTL_MAIL package provided staring in Oracle 10g. SOLUTION

How to Send an Email Using UTL_SMTP with Authenticated Mail Server

In this Document   Goal   Solution   References APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1] Information in this document applies to any platform. ***Checked for relevance on 07-Apr-2014*** GOAL The UTL_SMTP package is desig

How Network Load Balancing Technology Works--reference

http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balancing Terms and Definitions Network Load Balancing Architecture Network Load Balancing Protocols Application Compatibility with Network Load Balancing

oracle-Expdp/impdp命令

建立逻辑路径 create or replace directory dumpdir as 'c:\'; grant read,write on directory dumpdir to scott; 倒入/出 expdp newdr/[email protected] directory=test dumpfile=test_score1.dmp logfile=tes t.log parallel=2 schemas=newdr 错误 ORA-00054 resource busy and