E-Mail的發送其實是發送一連串的字串,無論是圖片、程式、文字,連附件也是如此,
所以收到的Mail原始碼是一連串的字串,再依據特定關鍵字來區分是主旨、附件、本文
格式…等等。所以如果有任何的問題,可以參考Mail的原始碼
範例一:
-----------------------------------------------------------------------
DECLARE
mail_conn utl_smtp.connection;
mailhost varchar(20);
mailport number(2);
mailsubject varchar(200);
mailbody varchar(500);
BEGIN
--Mail Server IP
mailhost := '192.168.1.100';
--Mail Server SMTP Port
mailport := 25;
--連接Mail Server
mail_conn := utl_smtp.open_connection(mailhost, mailport);
--送出helo指令,連接上Mail Server後必須先送上這一個指令
utl_smtp.helo(mail_conn,mailhost);
--送出指令 AUTH LOGIN ,告訴Mail Server 要使用認證
utl_smtp.command(mail_conn, 'AUTH LOGIN');
--送出指令,這裡是送出帳號,必須把字串轉成Base64編碼
utl_smtp.command(mail_conn,demo_base64.encode(utl_raw.cast_to_raw('cha')));
--送出指令,這裡是送出密碼,必須把字串轉成Base64編碼
utl_smtp.command(mail_conn,demo_base64.encode(utl_raw.cast_to_raw('11270810')));
--本文
mailbody :='tttttteeeeeesssssstttttt,我家門前有小河,後面有山坡。';
--於本文的前面加上時間、寄件人、收件人、主旨,此格式是依照Mail的原始碼來
--作變化,這樣才可以於Mail Client端顯示出寄件人、收件人、主旨、時間。
--CHR(13) || CHR (10) 為斷行字元
mailbody :='Date: ' || to_char(SYSDATE, 'dd Mon yy hh24:mi:ss') || chr(13) || chr(10) ||
'From: ' || 'cha@quintain.com.tw' || chr(13) || chr(10) ||
'subject: ' || 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa' || chr(13) || chr(10) ||
'To: ' || 'cha@quintqain.com.tw' || chr(13) || chr(10) || mailbody;
--告訴Mail Server寄件者
utl_smtp.mail(mail_conn,'
--告訴Mail Server收件者
utl_smtp.rcpt(mail_conn,'
--告訴Mail Server準備傳送資料,開啟傳送的通道
utl_smtp.open_data(mail_conn);
--開始傳送編碼後的資料,如果資料太大,可以分開傳送,以免緩衝區不夠
utl_smtp.write_raw_data(mail_conn,utl_raw.cast_to_raw('Content-Type: text/plain;' || CHR(13) || CHR(10)) );
utl_smtp.write_raw_data(mail_conn,utl_raw.cast_to_raw('charset="big5" ' || CHR(13) || CHR(10)) );
utl_smtp.write_raw_data(mail_conn,utl_raw.cast_to_raw('Content-Transfer-Encoding: base64' || CHR(13) || CHR(10)));
utl_smtp.write_raw_data(mail_conn,utl_raw.cast_to_raw(mailbody));
--告訴Mail Server準備傳送資料,關閉傳送的通道
utl_smtp.close_data(mail_conn);
--與Mail Server離線
utl_smtp.quit(mail_conn);
END;
範例二:
-----------------------------------------------------------------------------
V_MAILHOST := '211.20.204.194';
V_MAILPORT :=25;
V_MAIL_CONN := UTL_SMTP.OPEN_CONNECTION(V_MAILHOST,V_MAILPORT);
UTL_SMTP.HELO(V_MAIL_CONN,V_MAILHOST);
UTL_SMTP.COMMAND(V_MAIL_CONN,'AUTH LOGIN');
UTL_SMTP.COMMAND(V_MAIL_CONN,DEMO_BASE64.ENCODE(UTL_RAW.CAST_TO_RAW('ORACLE')));
UTL_SMTP.COMMAND(V_MAIL_CONN,DEMO_BASE64.ENCODE(UTL_RAW.CAST_TO_RAW('oracle')));
V_MAILBODY := :MAIL.D_MESSAGE;
V_MAILBODY := 'FROM: ' || :D_SEND || CHR(13) || CHR(10) ||
'SUBJECT: ' || :D_SUBJECT || CHR(13) || CHR(10) ||
'TO: ' || :D_RECEIVE || CHR(13) || CHR(10) ||
'CC: ' || :D_ADDRESS || CHR(13) || CHR(10) ||
'MIME-Version: 1.0' || CHR(13) || CHR(10) ||
'Content-Type: text/HTML;' || CHR(13) || CHR(10) || V_MAILBODY;
UTL_SMTP.MAIL(V_MAIL_CONN,'
UTL_SMTP.RCPT(V_MAIL_CONN,'
UTL_SMTP.RCPT(V_MAIL_CONN,'
UTL_SMTP.OPEN_DATA(V_MAIL_CONN);
--UTL_SMTP.WRITE_RAW_DATA(V_MAIL_CONN,UTL_RAW.CAST_TO_RAW('FROM: ORACLE@QUINTAIN.COM.TW'));
--UTL_SMTP.WRITE_RAW_DATA(V_MAIL_CONN,UTL_RAW.CAST_TO_RAW('SUBJECT: TEST主旨'));
UTL_SMTP.WRITE_RAW_DATA(V_MAIL_CONN,UTL_RAW.CAST_TO_RAW(V_MAILBODY));
UTL_SMTP.CLOSE_DATA(V_MAIL_CONN);
UTL_SMTP.QUIT(V_MAIL_CONN);