Dear Readers
You may have requirements to maybe e-mail a text file which you generate using some process via a concurrent program.
In such case below code can help you out. You just need to pass the below
1. Email ID to who mail is to be sent
2. Subject of mail
3. Body of mail
4. Attachment flag (whether or not an attachment is there, pass N to get simple mail and no attachment, pass Y to send the attachment also)
5. Attachment directory : the directory where the text file resides
6. File Name: Name of text file
Hope this helps
Cheers
A
You may have requirements to maybe e-mail a text file which you generate using some process via a concurrent program.
In such case below code can help you out. You just need to pass the below
1. Email ID to who mail is to be sent
2. Subject of mail
3. Body of mail
4. Attachment flag (whether or not an attachment is there, pass N to get simple mail and no attachment, pass Y to send the attachment also)
5. Attachment directory : the directory where the text file resides
6. File Name: Name of text file
PROCEDURE send_email_notif
(
p_email_id IN VARCHAR2,
p_subject IN VARCHAR2,
p_body IN VARCHAR2,
p_attachment IN VARCHAR2,
p_attach_dir IN VARCHAR2,
p_attach_file IN VARCHAR2
)
IS
lv_from VARCHAR2(80) := 'someone@somedomain.com';
lv_recipient VARCHAR2(80) := p_email_id;
lv_subject VARCHAR2(80) := p_subject;
lv_mail_host VARCHAR2(30) := 'localhost';
lv_mail_conn utl_smtp.Connection;
crlf VARCHAR2(2) :=
chr(13)||chr(10);
lv_line VARCHAR2(2000);
lv_file Utl_File.file_type;
BEGIN
lv_mail_conn := utl_smtp.Open_Connection(lv_mail_host, 25);
utl_smtp.Helo(lv_mail_conn, lv_mail_host);
utl_smtp.Mail(lv_mail_conn, lv_from);
utl_smtp.Rcpt(lv_mail_conn, lv_recipient);
IF p_attachment = 'N'
THEN
utl_smtp.Data(lv_mail_conn,
'Date: ' || to_char(sysdate, 'Dy,
DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || lv_from || crlf ||
'Subject: '|| lv_subject || crlf ||
'To: ' || lv_recipient || crlf ||
crlf
|| p_body || crlf
);
ELSE
Utl_Smtp.open_data(lv_mail_conn);
Utl_Smtp.write_data(lv_mail_conn, 'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || lv_from || crlf ||
'Subject: '|| lv_subject || crlf ||
'To: ' || lv_recipient || crlf ||
'MIME-Version: 1.0'|| crlf || -- Use MIME mail standard
'Content-Type:
multipart/mixed;'||
crlf ||
' boundary="-----SECBOUND"'|| crlf ||
crlf
||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
'Content-Transfer_Encoding:
7bit'|| crlf ||
crlf
|| p_body || crlf ||
crlf
||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="' || p_attach_file || '"'|| crlf ||
'Content-Transfer_Encoding:
8bit'|| crlf ||
'Content-Disposition:
attachment;'||
crlf ||
' filename="' || p_attach_file || '"'|| crlf ||
crlf);
lv_file := Utl_File.fopen(p_attach_dir, p_attach_file, 'R');
LOOP
BEGIN
Utl_File.get_line(lv_file, lv_line);
EXCEPTION
WHEN OTHERS THEN
EXIT;
END;
Utl_Smtp.write_data(lv_mail_conn, lv_line || crlf);
END LOOP;
Utl_Smtp.write_data(lv_mail_conn, crlf || '-------SECBOUND--');
Utl_File.fclose(lv_file);
Utl_Smtp.close_data(lv_mail_conn);
END IF;
utl_smtp.Quit(lv_mail_conn);
END send_email_notif;
Hope this helps
Cheers
A
Hi Anand,
ReplyDeleteI want to send mail using UTL_SMTP and want to include BCC as well.
Pls suggest.
Thanks,
Shashikant
This comment has been removed by the author.
ReplyDeleteHi Shashi
ReplyDeleteI would suggest trying something like the below to send e-mails to multiple IDs. One in TO, One in CC and One in BCC
I hope this helps to resolve your query
DECLARE
lv_from VARCHAR2(80) := 'utl_smtp_bcc@mycompany.com';
lv_to VARCHAR2(80) := 'someone.to@mycompany.com';
lv_cc VARCHAR2(80) := 'someone_cc@gmail.com';
lv_bcc VARCHAR2(80) := 'someone.bcc@mycompany.com';
lv_subject VARCHAR2(80) := 'Test BCC Email UTL_SMTP';
lv_Mail_Host VARCHAR2(30) := 'localhost';
lv_Mail_Conn utl_smtp.Connection;
crlf VARCHAR2(2) := chr(13)||chr(10);
BEGIN
lv_Mail_Conn := utl_smtp.Open_Connection(lv_Mail_Host, 25);
utl_smtp.Helo(lv_Mail_Conn, lv_Mail_Host);
utl_smtp.Mail(lv_Mail_Conn, lv_From);
utl_smtp.Rcpt(lv_Mail_Conn, lv_to);
utl_smtp.Rcpt(lv_Mail_Conn, lv_cc);
utl_smtp.Rcpt(lv_Mail_Conn, lv_bcc);
utl_smtp.Data(lv_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || lv_From || crlf ||
'Subject: '|| lv_Subject || crlf ||
'To: ' || lv_to || crlf ||
'Cc: ' || lv_cc || crlf ||
'Bcc: ' || lv_bcc || crlf ||
crlf ||
'Dear User,' || crlf || crlf || 'I sent this email from UTL_SMTP to see if the CC and BCC are working fine'
);
utl_smtp.Quit(lv_mail_conn);
END;
Cheers
A
Thanks Anand for your quick response.
ReplyDelete