Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29769

How to send email using PL/SQL?

$
0
0

Hi friends I have an assignment in which my task is to write a pl/sql procedure used to send email which uses gmail as mail server.

I write this procedure

CREATE OR REPLACE PROCEDURE send_mail (p_to        IN VARCHAR2,
                                       p_from      IN VARCHAR2,
                                       p_message   IN VARCHAR2,
                                       p_smtp_host IN VARCHAR2,
                                       p_smtp_domain IN varchar2, 
                                       p_smtp_port IN NUMBER DEFAULT 465) AS
  l_mail_conn   UTL_SMTP.connection;
BEGIN
  l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
  UTL_SMTP.ehlo(l_mail_conn, p_smtp_domain);  
  UTL_SMTP.command(l_mail_conn,'AUTH LOGIN');  
  UTL_SMTP.command(l_mail_conn,utl_encode.base64_encode(utl_raw.cast_to_raw('mygmail@gmail.com')));
  UTL_SMTP.command(l_mail_conn,utl_encode.base64_encode(utl_raw.cast_to_raw('mypassword')));
  UTL_SMTP.mail(l_mail_conn, p_from);
  UTL_SMTP.rcpt(l_mail_conn, p_to);
  UTL_SMTP.data(l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf);
  UTL_SMTP.quit(l_mail_conn);
END;
/

Procedure created successfully

TO execute this procedure I used following code

BEGIN
  send_mail(p_to        => 'someone@gmail.com',
            p_from      => 'mygmail@gmail.com',
            p_message   => 'This is a test message.',
            p_smtp_host => 'smtp.gmail.com',
        p_smtp_domain => 'gmail.com'
);
END;
/

After executing I got following error

ERROR at line 1:
ORA-29278: SMTP transient error: 421 Service not available
ORA-06512: at "SYS.UTL_SMTP", line 17
ORA-06512: at "SYS.UTL_SMTP", line 96
ORA-06512: at "SYS.UTL_SMTP", line 138
ORA-06512: at "ZAHID.SEND_MAIL", line 11
ORA-06512: at line 2

If I changed the port number 25 then I got this error:

ERROR at line 1:
ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. t9sm10042472wjf.41
- gsmtp
ORA-06512: at "SYS.UTL_SMTP", line 17
ORA-06512: at "SYS.UTL_SMTP", line 98
ORA-06512: at "SYS.UTL_SMTP", line 158
ORA-06512: at "ZAHID.SEND_MAIL", line 13
ORA-06512: at line 2

My oracle version is Oracle9i, I searched a lot of for solution of above errors but not successful, please review my code and helped me to solve it, I shall be very thankful to you.


Viewing all articles
Browse latest Browse all 29769

Trending Articles