2022-07-18

ORA-29279: SMTP permanent error: 530 Authentication required

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



GOAL

The UTL_SMTP package is designed for sending electronic mail (E-Mail) over simple mail transfer protocol (SMTP) as specified by RFC821. Some mail servers require username and password to be supplied for authentication. If the username and password is not supplied when the SMTP server expects it, then call to the UTL_SMTP will fail with "ORA-29279: SMTP permanent error: 530 Authentication required".

The sample code below shows how to include the username/password for the mail server.

SOLUTION

create or replace procedure
testmail
(
  fromm varchar2,
  too   varchar2,
  sub   varchar2,
  body  varchar2,
  port  number
)
is
objConnection utl_smtp.connection;
vrData varchar2(32000);
username varchar2(20) := '<username>';
password varchar2(20) := '<password>';
begin
  objConnection := utl_smtp.open_connection('<your domain server name>',port);
  utl_smtp.helo(objConnection, '<your domain name server>');
  utl_smtp.command(objConnection, 'AUTH LOGIN');
  utl_smtp.command(objConnection,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(username))));
  utl_smtp.command(objConnection,utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(password))));

  utl_smtp.mail(objConnection, fromm);
  utl_smtp.rcpt(objConnection, too);
  utl_smtp.open_data(objConnection);
  /*** Sending the header information */
  utl_smtp.write_data(objConnection, 'From: '||fromm || utl_tcp.crlf);
  utl_smtp.write_data(objConnection, 'To: '||too || utl_tcp.crlf);

  utl_smtp.write_data(objConnection, 'Subject: ' || sub || utl_tcp.crlf);
  utl_smtp.write_data(objConnection, 'MIME-Version: ' || '1.0' || utl_tcp.crlf);
  utl_smtp.write_data(objConnection, 'Content-Type: ' || 'text/html;');

  utl_smtp.write_data(objConnection, 'Content-Transfer-Encoding: ' || '"8Bit"' ||utl_tcp.crlf);
  utl_smtp.write_data(objConnection,utl_tcp.crlf);
  utl_smtp.write_data(objConnection,utl_tcp.crlf||'<HTML>');
  utl_smtp.write_data(objConnection,utl_tcp.crlf||'<BODY>');
  utl_smtp.write_data(objConnection,utl_tcp.crlf||'<FONT COLOR="red" FACE="Courier New">'||body||'</FONT>');
  utl_smtp.write_data(objConnection,utl_tcp.crlf||'</BODY>');
  utl_smtp.write_data(objConnection,utl_tcp.crlf||'</HTML>');
  utl_smtp.close_data(objConnection);
  utl_smtp.quit(objConnection);
exception
  when utl_smtp.transient_error OR utl_smtp.permanent_error then
    utl_smtp.quit(objConnection);
    dbms_output.put_line(sqlerrm);
  when others then
    utl_smtp.quit(objConnection);
    dbms_output.put_line(sqlerrm);
end testmail;
/

Call the procedure by replacing the values in angular bracket as appropriately.

declare
vdate Varchar2(25);
begin
  vdate := to_char(sysdate,'dd-mon-yyyy HH:MI:SS AM');
  testmail('<sender_mailid>', '<recipient_mailid>', '<subject_line>','This is a UTL_SMTP-generated email at '|| vdate,25);
end;
/

Niciun comentariu:

Trimiteți un comentariu