使用PL / SQL,Oracle从gmail发送电子邮件

时间:2014-11-28 04:40:17

标签: oracle email stored-procedures oracle11g job-scheduling

我正在尝试建立一个程序来发送来自Gmail' Gmail'帐户到另一张收据。你能帮我制定一个完成这个目标的程序吗?我需要的只是调用程序发送' gmail'。如果有必要cc邮件。我已经提到过我尝试过的代码。谢谢!

CREATE OR REPLACE PROCEDURE sendMail (
smtpHost IN VARCHAR2,
smtpPort IN PLS_INTEGER DEFAULT 25,
mailFrom IN VARCHAR2,
rcptTo IN VARCHAR2,
--ccs IN t_ccs,
messageSubject IN VARCHAR2,
messageBody IN VARCHAR2,
username IN VARCHAR2,
password IN VARCHAR2)
IS
l_conn UTL_SMTP.connection;
l_ccs VARCHAR2(2000);

l_encoded_username VARCHAR2(2000);
l_encoded_password VARCHAR2(2000);
BEGIN
--open connection
--l_conn := UTL_SMTP.open_connection(smtpHost, smtpPort);
--UTL_SMTP.helo(l_conn, smtpHost);


l_encoded_username := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(username)));
l_encoded_password := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(password)));
l_conn := UTL_SMTP.open_connection(smtpHost, smtpPort);
UTL_SMTP.ehlo(l_conn, smtpHost);--DO NOT USE HELO
UTL_SMTP.command(l_conn, 'AUTH', 'LOGIN');
UTL_SMTP.command(l_conn, l_encoded_username);
UTL_SMTP.command(l_conn, l_encoded_password);

--prepare headers
UTL_SMTP.mail(l_conn, mailFrom);
UTL_SMTP.rcpt(l_conn, rcptTo);

/*if we have multiple recipients or CCs, we must call UTL_SMTP.rcpt once for each one
however, we shall specify that there are CCs in the mail header in order for them to appear as such*/
/*IF ccs IS NOT NULL THEN
FOR i IN ccs.FIRST..ccs.LAST LOOP
UTL_SMTP.rcpt(l_conn, ccs(i));--add recipient
l_ccs:=l_ccs||ccs(i)||',';--mark as CC
END LOOP;
--now remove the trailing comma at the end of l_ccs
l_ccs:=substr(l_ccs,0,length(l_ccs)-1 );
END IF; */

--start multi line message
UTL_SMTP.open_data(l_conn);

--prepare mail header
/*DO NOT USE MON instead of MM in the date pattern if you run the script on machines with different locales as it will be misunderstood
and the mail date will appear as 01/01/1970*/
UTL_SMTP.write_data(l_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MM-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_conn, 'To: ' || rcptTo || UTL_TCP.crlf);
UTL_SMTP.write_data(l_conn, 'Cc: ' || l_ccs || UTL_TCP.crlf);
UTL_SMTP.write_data(l_conn, 'From: ' || mailFrom || UTL_TCP.crlf);
UTL_SMTP.write_data(l_conn, 'Subject: ' || messageSubject || UTL_TCP.crlf || UTL_TCP.crlf);

--include the message body
UTL_SMTP.write_data(l_conn, messageBody || UTL_TCP.crlf || UTL_TCP.crlf);

--send the email
UTL_SMTP.close_data(l_conn);
UTL_SMTP.quit(l_conn);
END; 

1 个答案:

答案 0 :(得分:0)

它不起作用,因为要使用Google发送电子邮件,您必须使用正确的Google API:

所有细节都在那里:

  1. https://developers.google.com/gmail/api/auth/about-auth
  2. https://developers.google.com/gmail/api/guides/sending
  3. 该方法包括使用Google API编写Web服务,然后从数据库调用Web服务。