用ORACLE 存储过程发送HTML邮件,支持文本和HTML两种格式,增加防中文乱码

CREATE OR REPLACE procedure html_email(
    p_to            in varchar2,
    p_from          in varchar2,   
    p_subject       in varchar2,
    p_text          in varchar2 default null,
    p_html          in varchar2 default null,
    p_smtp_hostname in varchar2,
    p_smtp_portnum  in varchar2)
is
    l_boundary      varchar2(255) default 'a1b2c3d4e3f2g1';
    l_connection    utl_smtp.connection;
    l_body_html     clob := empty_clob;  --This LOB will be the email message
    l_offset        number;
    l_ammount       number;
    l_temp          varchar2(32767) default null;


    V_DB_NLS_CHARACTERSET VARCHAR2(40);    --(防乱码处理) 
begin
    l_connection := utl_smtp.open_connection( p_smtp_hostname, p_smtp_portnum );
    utl_smtp.helo( l_connection, p_smtp_hostname );
    
 --      v_conn := UTL_SMTP.open_connection(v_smtphost, v_smtpport);
    --是用 ehlo() 而不是 helo() 函数
    --否则会报:ORA-29279: SMTP 永久性错误: 503 5.5.2 Send hello first.
 --   UTL_SMTP.ehlo(v_conn, 'misdb.szyuto.com'); 
 
    -- smtp服务器登录校验
    UTL_SMTP.command(l_connection, 'AUTH LOGIN');            
    UTL_SMTP.command(l_connection,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw('****@QQ.com'))));
    UTL_SMTP.command(l_connection,UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw('****'))));
    


                        
    utl_smtp.mail( l_connection, p_from );
    utl_smtp.rcpt( l_connection, p_to );
    l_temp := l_temp || 'MIME-Version: 1.0' ||  chr(13) || chr(10);
    l_temp := l_temp || 'To: ' || p_to || chr(13) || chr(10);
    l_temp := l_temp || 'From: ' || p_from || chr(13) || chr(10);
    l_temp := l_temp || 'Subject: ' || p_subject || chr(13) || chr(10);
    
    l_temp := l_temp || 'Reply-To: ' || p_from ||  chr(13) || chr(10);
    --  --(防乱码处理) 
    l_temp := l_temp || 'Content-Type: multipart/alternative;charset=gb2312; boundary=' || 
                         chr(34) || l_boundary ||  chr(34) || chr(13) || 
                         chr(10);
  
    dbms_output.put_line(l_temp);
    ----------------------------------------------------
    -- Write the headers
    dbms_lob.createtemporary( l_body_html, false, 10 );
    dbms_lob.write(l_body_html,length(l_temp),1,l_temp);


    ----------------------------------------------------
    -- Write the text boundary
    l_offset := dbms_lob.getlength(l_body_html) + 1;
    /*
    *modify date:2006-05-09 
    *cause      :the html mail only is source code,can't be HTML format.
    *           :because the mail header format is wrong,less the chr(13)chr(10)
    *modifier   :zhuzl
    *Thank for zengnan;
    *
    *I should study the mail p-- 
    */
    l_temp   := chr(13)||chr(10)||'--' || l_boundary || chr(13)||chr(10);
    l_temp   := l_temp || 'content-type: text/plain; charset=us-ascii' || 
                  chr(13) || chr(10) || chr(13) || chr(10);
    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);
    ----------------------------------------------------
    -- Write the plain text portion of the email
    l_offset := dbms_lob.getlength(l_body_html) + 1;
    dbms_lob.write(l_body_html,length(p_text),l_offset,p_text);
    ----------------------------------------------------
    -- Write the HTML boundary
    l_temp   := chr(13)||chr(10)||chr(13)||chr(10)||'--' || l_boundary || 
                    chr(13) || chr(10);
    l_temp   := l_temp || 'content-type: text/html;' || 
                   chr(13) || chr(10) || chr(13) || chr(10);
    l_offset := dbms_lob.getlength(l_body_html) + 1;
    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);
    ----------------------------------------------------
    -- Write the HTML portion of the message
    l_offset := dbms_lob.getlength(l_body_html) + 1;
    dbms_lob.write(l_body_html,length(p_html),l_offset,p_html);
    ----------------------------------------------------
    -- Write the final html boundary
    l_temp   := chr(13) || chr(10) || '--' ||  l_boundary || '--' || chr(13);
    l_offset := dbms_lob.getlength(l_body_html) + 1;
    dbms_lob.write(l_body_html,length(l_temp),l_offset,l_temp);


    ----------------------------------------------------
    -- Send the email in 1900 byte chunks to UTL_SMTP
    l_offset  := 1;
    l_ammount := 1900;
    utl_smtp.open_data(l_connection);
   --(防乱码处理) 
      SELECT VALUE  
    INTO V_DB_NLS_CHARACTERSET  
    FROM NLS_DATABASE_PARAMETERS  
   WHERE PARAMETER = 'NLS_CHARACTERSET';


 /*                                       
    UTL_SMTP.WRITE_RAW_DATA(l_connection,  
                          UTL_RAW.CAST_TO_RAW(CONVERT(l_body_html,  
                                                      'ZHS16GBK',  
                                                      V_DB_NLS_CHARACTERSET)));
   */  --                                                 
    while l_offset < dbms_lob.getlength(l_body_html) loop
     --   utl_smtp.write_data(l_connection,
      --                      dbms_lob.substr(l_body_html,l_ammount,l_offset));
                      
        --邮件输出(防乱码处理)  
             UTL_SMTP.WRITE_RAW_DATA(l_connection,  
                          UTL_RAW.CAST_TO_RAW(CONVERT(dbms_lob.substr(l_body_html,l_ammount,l_offset),                                                      
                                                     'ZHS16GBK',  
                                                      V_DB_NLS_CHARACTERSET)));          


                         
        l_offset  := l_offset + l_ammount ;
        l_ammount := least(1900,dbms_lob.getlength(l_body_html) - l_ammount);
    end loop;
    utl_smtp.close_data(l_connection);
    utl_smtp.quit( l_connection );
    dbms_lob.freetemporary(l_body_html);
end;
/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值