DECLARE
v_mail_conn UTL_SMTP.CONNECTION;
v_sender_email_address VARCHAR2(40);
v_receiver_email_address VARCHAR2(40);
gv_smtp_domain VARCHAR2(40);
v_email_server VARCHAR2(40) := 'smtp-na-auth.sanofi.com'; /*Mail server should be given as per the email server
configured.*/
v_port NUMBER := 25; /*this is the email port*/
v_mesg VARCHAR2(4000);
crlf VARCHAR2(2) := CHR(13) || CHR(10);
username varchar2(1000):= 'PS108300';
passwd varchar2(50):= 'mQGq10fQn#b';
tx_timeout PLS_INTEGER := 30;
BEGIN
v_sender_email_address := 'Shridhar.Umarani@sanofi.com'; /*Sender email address*/
v_receiver_email_address := 'Hemala.Mayilsamy@sanofi.com'; /*Receiver email address*/
v_mail_conn := utl_smtp.open_connection(v_email_server, v_port,tx_timeout); /*1. To open/establish a connection to an
SMTP server*/
utl_smtp.starttls(v_mail_conn);
UTL_SMTP.AUTH(v_mail_conn, username, passwd, schemes => 'HCRS');
gv_smtp_domain := 'sanofi.com';
utl_smtp.helo(v_mail_conn, gv_smtp_domain); /*2. Initial handshaking*/
utl_smtp.mail(v_mail_conn, v_sender_email_address); /* 3. Initiates a mail transaction with the server*/
utl_smtp.rcpt(v_mail_conn, v_receiver_email_address); /*4. specifies the recipient of an e-mail*/
/*5. specifies the Subject, Recipients & body of an e-mail */
v_mesg := 'From:' || v_sender_email_address || crlf || 'Subject:' ||
'An email generated successfully using UTL_SMTP.DATA' || crlf;
v_mesg := v_mesg || 'TO:' || v_receiver_email_address || crlf;
v_mesg := v_mesg || 'CC::' || v_receiver_email_address || crlf;
v_mesg := v_mesg || 'BCC::' || v_receiver_email_address || crlf;
v_mesg := v_mesg || '' || crlf || 'Hi' || crlf || '' || crlf ||
'How are you? ' || crlf || '' || crlf ||
'An email generated successfully using UTL_SMTP with the method UTL_SMTP.DATA ' || crlf ||''|| crlf ||
'Thanks' || crlf || 'XXXXXXXXX007';
UTL_SMTP.DATA(v_mail_conn, v_mesg); /*6. This procedure would actually trigger the email to the recipients*/
UTL_SMTP.QUIT(v_mail_conn);
DBMS_OUTPUT.PUT_LINE('An email sent successfully using UTL_SMTP with the method UTL_SMTP.DATA');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error occurred while sending an email:' ||
SQLERRM);
END;
/