Home » SQL & PL/SQL » SQL & PL/SQL » Excel attachment by utl_smtp (Oracle 11g, windows xp 32 bit)
Excel attachment by utl_smtp [message #602409] |
Wed, 04 December 2013 03:41 |
|
sudeshna_bio08
Messages: 11 Registered: December 2013 Location: india
|
Junior Member |
|
|
Dear all,
I am using Oracle database 11g, os: windows xp.
I am trying to send email by utl_smtp with excel attachment.
I have installed stunnel.exe. telnet localhost 1925 working.
code:
create or replace
PROCEDURE send_mail_not
IS
v_mailconn UTL_SMTP.connection;
v_err VARCHAR2 (1000);
v_message VARCHAR2 (3000);
crlf VARCHAR2 (2) := CHR (13) || CHR (10);
v_attachment VARCHAR2 (200);
p_from_mail_id VARCHAR2 (200);
p_to_mail_id VARCHAR2 (200);
p_subject VARCHAR2 (200);
nls_charset varchar2(255);
BEGIN
select value
into nls_charset
from nls_database_parameters
where parameter = 'NLS_CHARACTERSET';
p_from_mail_id := 'MYADI26@GMAIL.COM';
p_to_mail_id := 'SUDESHNA_BIO08@YAHOO.CO.IN';
p_subject := 'error msg';
v_attachment := 'EXCEL ATTACHMENT';
v_mailconn := UTL_SMTP.open_connection ('localhost', 1925);
UTL_SMTP.helo (v_mailconn, 'localhost');
UTL_SMTP.mail (v_mailconn, 'myadi26@gmail.com');
utl_smtp.command(V_mailconn, 'auth login');
utl_smtp.command(v_mailconn,utl_encode.text_encode('myadi26@gmail.com', nls_charset, 1));
utl_smtp.command(v_mailconn, utl_encode.text_encode('*****', nls_charset, 1));
UTL_SMTP.rcpt (v_mailconn, 'SUDESHNA_BIO08@YAHOO.CO.IN');
v_message := 'dfdfjkdflkdmf';
DBMS_OUTPUT.put_line ('Inside email procedure');
v_message :=
'Date: '
|| TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss')
|| crlf
|| 'From: '
|| p_from_mail_id
|| crlf
|| 'Subject: '
|| p_subject
|| crlf
|| 'To: '
|| p_to_mail_id
|| crlf
|| 'Mime-version: 1.0'
|| UTL_TCP.crlf
|| -- use Mime mail standard
'Content-type: multipart/mixed;'
|| UTL_TCP.crlf
|| 'boundary="-----SECBOUND"'
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/html;'
|| UTL_TCP.crlf
|| 'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_message
|| UTL_TCP.crlf
|| -- message body
UTL_TCP.crlf
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/plain;'
|| UTL_TCP.crlf
|| ' name="excel.log"'
|| UTL_TCP.crlf
|| -- file name that will hold the attached text
'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| 'Content-Disposition: attachment;'
|| UTL_TCP.crlf
|| ' filename="sampleexcel.xls"'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_attachment
|| UTL_TCP.crlf
|| -- attachment
UTL_TCP.crlf
|| '-------SECBOUND--' -- end mime mail
;
UTL_SMTP.write_data (v_mailconn, v_message);
UTL_SMTP.close_data (v_mailconn);
UTL_SMTP.quit (v_mailconn);
DBMS_OUTPUT.put_line ('v_message');
EXCEPTION
WHEN OTHERS
THEN
v_err := SQLERRM;
DBMS_OUTPUT.PUT_LINE(V_ERR);
END send_mail_not;
procedure created. But when I am calling the procedure it is throwing error:
ORA-29279: SMTP permanent error: 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 sy10sm155560412pac.15 - gsmtp
pls help
|
|
|
|
Re: Excel attachment by utl_smtp [message #602423 is a reply to message #602420] |
Wed, 04 December 2013 04:52 |
|
sudeshna_bio08
Messages: 11 Registered: December 2013 Location: india
|
Junior Member |
|
|
Thanks for reply .But in the procedure, I have already mentioned uid & pwd of sender.
UTL_SMTP.helo (v_mailconn, 'localhost');
UTL_SMTP.mail (v_mailconn, 'myadi26@gmail.com');
utl_smtp.command(V_mailconn, 'auth login');
utl_smtp.command(v_mailconn,utl_encode.text_encode('myadi26@gmail.com', nls_charset, 1));
utl_smtp.command(v_mailconn, utl_encode.text_encode('*****', nls_charset, 1));
UTL_SMTP.rcpt (v_mailconn, 'SUDESHNA_BIO08@YAHOO.CO.IN');
|
|
|
|
Re: Excel attachment by utl_smtp [message #602435 is a reply to message #602431] |
Wed, 04 December 2013 06:18 |
|
sudeshna_bio08
Messages: 11 Registered: December 2013 Location: india
|
Junior Member |
|
|
ora-29279 problem is solved.I am getting mail but without any attachment.
Kindly check this code:
create or replace
package body oracle_mail_p1
is
-- Write a MIME header
procedure write_mime_header (
p_conn in out nocopy utl_smtp.connection
, p_name in varchar2
, p_value in varchar2
)
is
begin
utl_smtp.write_data ( p_conn
, p_name || ': ' || p_value || utl_tcp.crlf
);
end;
procedure mail (
p_sender in varchar2
, p_recipient in varchar2
, p_subject in varchar2
)
is
crlf VARCHAR2 (500) := CHR (13) || CHR (10);
p_message varchar2(500);
l_conn utl_smtp.connection;
nls_charset varchar2(555);
v_attachment VARCHAR2 (1000);
V_dir varchar2(20) := 'MYDIR'; --a directory object created at C:\hello
begin
-- get characterset
select value
into nls_charset
from nls_database_parameters
where parameter = 'NLS_CHARACTERSET';
-- establish connection and autheticate
l_conn := utl_smtp.open_connection (g_smtp_host, g_smtp_port);
utl_smtp.ehlo(l_conn, g_smtp_domain);
utl_smtp.command(l_conn, 'auth login');
utl_smtp.command(l_conn,utl_encode.text_encode('myadi26@gmail.com', nls_charset, 1));
utl_smtp.command(l_conn, utl_encode.text_encode('*****', nls_charset, 1));
-- set from/recipient
utl_smtp.command(l_conn, 'MAIL FROM: <'||p_sender||'>');
utl_smtp.command(l_conn, 'RCPT TO: <'||p_recipient||'>');
-- write mime headers
utl_smtp.open_data (l_conn);
write_mime_header (l_conn, 'From', p_sender);
write_mime_header (l_conn, 'To', p_recipient);
write_mime_header (l_conn, 'Subject', p_subject);
write_mime_header (l_conn, 'Content-Type', 'text/plain');
write_mime_header (l_conn, 'X-Mailer', g_mailer_id);
utl_smtp.write_data (l_conn, utl_tcp.crlf);
-- write message body
p_message :=
'Date: '
|| TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss')
|| crlf
|| 'From: '
|| p_SENDER
|| crlf
|| 'Subject: '
|| p_subject
|| crlf
|| 'To: '
|| p_RECIPIENT
|| crlf
|| 'Mime-version: 1.0'
|| UTL_TCP.crlf
|| -- use Mime mail standard
'Content-type: multipart/mixed;'
|| UTL_TCP.crlf
|| 'boundary="-----SECBOUND"'
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/html;'
|| UTL_TCP.crlf
|| 'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| P_message
|| UTL_TCP.crlf
|| -- message body
UTL_TCP.crlf
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: application/excel;'
|| UTL_TCP.crlf
|| ' name="excel.log"'
|| UTL_TCP.crlf
|| -- file name that will hold the attached text
'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| 'Content-Disposition: attachment;'
|| UTL_TCP.crlf
|| V_DIR||','||' filename="sampleexcel.xls"'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
||V_attachment
|| UTL_TCP.crlf
|| -- attachment
UTL_TCP.crlf
|| '-------SECBOUND--' -- end mime mail
;
utl_smtp.write_data (l_conn, p_message);
utl_smtp.write_data(l_conn,' filename="' || 'C:\hello\'||'sampleexcel.xls' || '"' ||UTL_TCP.CRLF);
utl_smtp.close_data (l_conn);
-- end connection
utl_smtp.quit (l_conn);
exception
when others
then
begin
utl_smtp.quit(l_conn);
exception
when others then
null;
end;
raise_application_error(-20000,'Failed to send mail due to the following error: ' || sqlerrm);
end;
end;
*BlackSwan added {code} tags. Please do so yourself in the future.
see URL below to learn how to do so
http://www.orafaq.com/forum/t/174502/
[Updated on: Wed, 04 December 2013 06:23] by Moderator Report message to a moderator
|
|
|
|
Re: Excel attachment by utl_smtp [message #602502 is a reply to message #602462] |
Thu, 05 December 2013 00:38 |
|
sudeshna_bio08
Messages: 11 Registered: December 2013 Location: india
|
Junior Member |
|
|
sir, in my procedure 1 , I have mentioned 2nd parameter of utl_smtp.helo as localhost.
UTL_SMTP.helo (v_mailconn, 'localhost');
change into
UTL_SMTP.ehlo (v_mailconn, 'gmail.com'); --2nd parameter as domain name
restart stunnel.exe,now it is working but without any attachment.
create or replace PROCEDURE send_mail_without_attach
IS
v_mailconn UTL_SMTP.connection;
v_err VARCHAR2 (1000);
v_message VARCHAR2 (3000);
crlf VARCHAR2 (2) := CHR (10) || CHR (13);
v_attachment VARCHAR2 (200);
p_from_mail_id VARCHAR2 (200);
p_to_mail_id VARCHAR2 (200);
p_subject VARCHAR2 (200);
nls_charset varchar2(255);
BEGIN
select value
into nls_charset
from nls_database_parameters
where parameter = 'NLS_CHARACTERSET';
p_from_mail_id := 'myadi26@gmail.com';
p_to_mail_id := 'SUDESHNA_BIO08@YAHOO.CO.IN';
p_subject := 'error msg';
v_attachment := 'EXCEL ATTACHMENT from send_mail procedure';
v_mailconn := UTL_SMTP.open_connection ('localhost', 1925);
[b]UTL_SMTP.ehlo (v_mailconn, 'gmail.com');
[/b] utl_smtp.command(V_mailconn, 'auth login');
utl_smtp.command(v_mailconn,utl_encode.text_encode('myadi26@gmail.com', nls_charset, 1));
utl_smtp.command(v_mailconn, utl_encode.text_encode('*****', nls_charset, 1));
utl_smtp.command(v_mailconn, 'MAIL FROM: <'||'myadi26@gmail.com'||'>');
utl_smtp.command(v_mailconn, 'RCPT TO: <'||'sudeshna_bio08@yahoo.co.in'||'>');
utl_smtp.open_data (v_mailconn);
v_message := 'excel attachment from oracle 11g';
DBMS_OUTPUT.put_line ('Inside email procedure');
v_message :=
'Date: '
|| TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss')
|| crlf
|| 'From: '
|| p_from_mail_id
|| crlf
|| 'Subject: '
|| p_subject
|| crlf
|| 'To: '
|| p_to_mail_id
|| crlf
|| 'Mime-version: 1.0'
|| UTL_TCP.crlf
|| -- use Mime mail standard
'Content-type: multipart/mixed;'
|| UTL_TCP.crlf
|| 'boundary="-----SECBOUND"'
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/html;'
|| UTL_TCP.crlf
|| 'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_message
|| UTL_TCP.crlf
|| -- message body
UTL_TCP.crlf
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/plain;'
|| UTL_TCP.crlf
|| ' name="excel.log"'
|| UTL_TCP.crlf
|| -- file name that will hold the attached text
'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| 'Content-Disposition: attachment;'
|| UTL_TCP.crlf
|| ' filename="C:\hello\sampleexcel.xls"'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_attachment
|| UTL_TCP.crlf
|| -- attachment
UTL_TCP.crlf
|| '-------SECBOUND--' -- end mime mail
;
UTL_SMTP.write_data (v_mailconn, v_message);
UTL_SMTP.close_data (v_mailconn);
UTL_SMTP.quit (v_mailconn);
--DBMS_OUTPUT.put_line ('v_message');
EXCEPTION
WHEN OTHERS
THEN
v_err := SQLERRM;
DBMS_OUTPUT.PUT_LINE(V_ERR);
END send_mail_without_attach;
please post your suggestion.
|
|
|
Re: Excel attachment by utl_smtp [message #602540 is a reply to message #602502] |
Thu, 05 December 2013 07:32 |
|
sudeshna_bio08
Messages: 11 Registered: December 2013 Location: india
|
Junior Member |
|
|
I have modified my code.
Edited as below:
create or replace
PROCEDURE test_proc(p_file_txt varchar2)
IS
v_mailconn UTL_SMTP.connection;
v_err VARCHAR2 (1000);
v_message VARCHAR2 (3000);
crlf VARCHAR2 (2) := CHR (13) || CHR (10);
v_attachment VARCHAR2 (200);
p_from_mail_id VARCHAR2 (200);
p_to_mail_id VARCHAR2 (200);
p_subject VARCHAR2 (200);
nls_charset varchar2(255);
l_type varchar2(900);
cn_boundary CONSTANT VARCHAR2(250) := '--NextPart='||to_char( SYSDATE
, 'yyyymmdd-hh24miss');
--cn_last_boundary CONSTANT VARCHAR2(250) := crlf||'--'||cn_boundary||'--'||crlf;
PROCEDURE add_attachment_txt
( p_conn IN OUT nocopy utl_smtp.connection
, p_directory IN VARCHAR2
, p_file IN VARCHAR2
) IS
l_handle utl_file.file_type;
l_line VARCHAR2(32767);
l_dir VARCHAR2(250) := 'TEMP_DIR_'||to_char(SYSDATE, 'YYYYMMDDHH24MISS');
cn_read CONSTANT VARCHAR2(1) := 'R';
cn_first_boundary CONSTANT VARCHAR2(250) := crlf||'--'||cn_boundary||crlf;
cn_truncated CONSTANT VARCHAR2(250) := '*** truncated ***';
BEGIN
IF p_file IS NOT NULL THEN
utl_smtp.write_data(v_mailconn, cn_first_boundary);
utl_smtp.write_data(v_mailconn, crlf);
execute immediate 'create or replace directory '||l_dir||' as '||p_directory;
execute immediate 'grant read on directory '||l_dir||' to public';
l_handle := utl_file.fopen(l_dir, p_file, cn_read);
LOOP
BEGIN
l_line := cn_truncated;
utl_file.get_line(l_handle, l_line);
EXCEPTION
WHEN NO_DATA_FOUND THEN EXIT;
END;
utl_smtp.write_data(v_mailconn, l_line||crlf);
END LOOP;
utl_file.fclose(l_handle);
execute immediate 'drop directory '||l_dir;
utl_smtp.write_data(v_mailconn, crlf);
END IF;
EXCEPTION
WHEN OTHERS THEN
utl_file.fclose(l_handle);
END add_attachment_txt;
FUNCTION get_directory
( p_string IN VARCHAR2
) RETURN VARCHAR2 IS
l_return VARCHAR2(2000);
BEGIN
IF p_string IS NOT NULL THEN
l_return := p_string;
END IF;
RETURN(l_return);
END get_directory;
FUNCTION get_file
( p_string IN VARCHAR2
) RETURN VARCHAR2 IS
l_return VARCHAR2(2000);
BEGIN
IF p_string IS NOT NULL THEN
l_return :=p_string;
END IF;
RETURN(l_return);
END;
procedure add_attachment
( p_conn IN OUT utl_smtp.connection
, p_file_txt IN VARCHAR2)
is
begin
add_attachment_txt(p_conn, get_directory('C:\hello\'), get_file(p_file_txt));
end add_attachment;
BEGIN
select value
into nls_charset
from nls_database_parameters
where parameter = 'NLS_CHARACTERSET';
p_from_mail_id := 'myadi26@gmail.com';
p_to_mail_id := 'SUDESHNA_BIO08@YAHOO.CO.IN';
p_subject := 'excel attachment from test_proc procedure';
v_attachment := 'CSV,file,attachement';
l_type := 'text/plain';
v_mailconn := UTL_SMTP.open_connection ('localhost', 1925);
UTL_SMTP.ehlo (v_mailconn, 'gmail.com');
utl_smtp.command(V_mailconn, 'auth login');
utl_smtp.command(v_mailconn,utl_encode.text_encode('myadi26@gmail.com', nls_charset, 1));
utl_smtp.command(v_mailconn, utl_encode.text_encode('*****', nls_charset, 1));
utl_smtp.command(v_mailconn, 'MAIL FROM: <'||'myadi26@gmail.com'||'>');
utl_smtp.command(v_mailconn, 'RCPT TO: <'||'sudeshna_bio08@yahoo.co.in'||'>');
utl_smtp.open_data (v_mailconn);
v_message := 'excel attachment from oracle 11g';
DBMS_OUTPUT.put_line ('Inside email procedure');
v_message :=
'Date: '
|| TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss')
|| crlf
|| 'From: '
|| p_from_mail_id
|| crlf
|| 'Subject: '
|| p_subject
|| crlf
|| 'To: '
|| p_to_mail_id
|| crlf
|| 'Mime-version: 1.0'
|| UTL_TCP.crlf
|| -- use Mime mail standard
'Content-type: multipart/mixed;'
|| UTL_TCP.crlf
|| 'boundary="-----SECBOUND"'
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: text/plain;'
|| UTL_TCP.crlf
|| 'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_message
|| UTL_TCP.crlf
|| -- message body
UTL_TCP.crlf
|| UTL_TCP.crlf
|| '-------SECBOUND'
|| UTL_TCP.crlf
|| 'Content-Type: application/excel;'
|| UTL_TCP.crlf
|| ' name="excel.log"'
|| UTL_TCP.crlf
|| -- file name that will hold the attached text
'Content-Transfer_Encoding: 8bit'
|| UTL_TCP.crlf
|| 'Content-Disposition: attachment;'
|| ' filename="a.txt"'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
|| v_attachment
|| UTL_TCP.crlf
|| -- attachment
UTL_TCP.crlf
|| '-------SECBOUND--' -- end mime mail
;
UTL_SMTP.write_data (v_mailconn, v_message);
add_attachment(v_mailconn,p_file_txt);
UTL_SMTP.close_data (v_mailconn);
UTL_SMTP.quit (v_mailconn);
--DBMS_OUTPUT.put_line ('v_message');
EXCEPTION
WHEN OTHERS
THEN
v_err := SQLERRM;
DBMS_OUTPUT.PUT_LINE(V_ERR);
END;
/
exec test_proc('sampleexcel.xls');
i am getting mail along with attachment symbol but no attached file is there..
please help
|
|
|
|
|
|
|
Re: Excel attachment by utl_smtp [message #602612 is a reply to message #602610] |
Thu, 05 December 2013 23:15 |
|
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
With UTL_SMTP you are responsible to to properly format the message so that the attachment conforms to the SMTP standard.
With UTL_MAIL the procedure handles the lower level details for you.
Your problem is really not an Oracle problem, but it is an SMTP coding problem.
|
|
|
Goto Forum:
Current Time: Tue May 07 22:31:55 CDT 2024
|