【问题标题】:Send email from gmail using PL/SQL, Oracle使用 PL/SQL、Oracle 从 gmail 发送电子邮件
【发布时间】:2014-11-28 22:26:28
【问题描述】:

我正在尝试构建一个程序以将电子邮件从“Gmail”帐户发送到另一个收据。你能帮我制定一个程序来完成这个目标吗?我需要的只是调用发送“gmail”的程序。如果需要抄送邮件。我已经提到了我尝试过的下面的代码。谢谢!

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; 

【问题讨论】:

  • 请在问题中提及您的代码,即使它不完整
  • 我已经提到了代码。
  • 我不会发送任何邮件。但它执行正确。
  • @chathu - 这意味着问题不是您的程序,而是交付过程中的问题。例如,您的电子邮件很可能被垃圾邮件过滤器拒绝。

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


【解决方案1】:

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

所有细节都在那里:

  1. https://developers.google.com/gmail/api/auth/about-auth
  2. https://developers.google.com/gmail/api/guides/sending

该方法包括使用 Google API 编写 Web 服务,然后从数据库中调用该 Web 服务。

【讨论】:

    猜你喜欢
    • 2022-10-25
    • 2019-07-24
    • 1970-01-01
    • 2013-10-17
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多