How to send an Email through Oracle

Source: Internet
Author: User

In Procedure, p_receiver is the recipient address. Multiple recipients are separated by commas,
P_sub is the mail title, p_txt is the mail content HTML ). The Code is as follows:

 
 
  1. Createorreplaceprocedurebsd_sendemail (p_receivervarchar2, p_subvarchar2, p_txtvarchar2)
  2. Is 
  3. P_uservarchar2 (30): ='';
  4. P_passvarchar2 (30): ='';
  5. P_sendorvarchar2 (20): ='Gni';
  6. P_servervarchar2 (20): ='Mail _ Server_IP';
  7. P_portnumber: = 25;
  8. P_need_smtpnumber: = 0;
  9. P_subjectvarchar2 (4000 );
  10. Rochelle crlfvarchar2 (2): = utl_tcp.crlf;
  11. Rochelle sendoraddressvarchar2 (4000 );
  12. Rochelle splitevarchar2 (10): ='+ +';
  13. Boundaryconstantvarchar2 (256): ='----- Bysuk';
  14. First_boundaryconstantvarchar2 (256): ='--'| Boundary | l_crlf;
  15. Last_boundaryconstantvarchar2 (256): ='--'| Boundary |'--'| L_crlf;
  16. Multipart_mime_typeconstantvarchar2 (256): ='Multipart/mixed; boundary = "'| Boundary |'"';
  17.  
  18. Typeaddress_lististableofvarchar2 (100) indexbybinary_integer;
  19. My_address_listaddress_list;
  20. --------------------------------------- Separate email address ---------------------------------------------- 
  21. Procedurep_splite_str (p_strvarchar2, p_splite_flagintdefault1)Is 
  22. Rochelle addrvarchar2 (254): ='';
  23. Rochelle lenint;
  24. Rochelle strvarchar2 (4000 );
  25. Jint: = 0;-- Indicates the number of email addresses or attachments 
  26. Begin 
  27. /* Process the list of received mail addresses, including removing spaces, converting; and so on */
  28. Rochelle STR: = trim (rtrim (Replace(Replace(P_str,';',','),'',''),','));
  29. Rochelle Len: = length (l_str );
  30. Foriin1.. l_lenloop
  31. Ifsubstr (l_str, I, 1) <>','Then 
  32. L_addr: = l_addr | substr (l_str, I, 1 );
  33. Else 
  34. J: = j + 1;
  35. Ifp_splite_flag = 1 then-- Process the email address 
  36. -- '<>' Must be added before and after the email address; otherwise, many mailboxes cannot send emails. 
  37. Rochelle ADDR: ='<'| L_addr |'>';
  38. -- Call the mail sending Process 
  39. My_address_list (j): = l_addr;
  40. Endif;
  41. Rochelle ADDR: ='';
  42. Endif;
  43. Ifi = l_lenthen
  44. J: = j + 1;
  45. Ifp_splite_flag = 1 then
  46. -- Call the mail sending Process 
  47. Rochelle ADDR: ='<'| L_addr |'>';
  48. My_address_list (j): = l_addr;
  49. Endif;
  50. Endif;
  51. Endloop;
  52. End;
  53. ------------------------------------------------ Write the mail header and content ------------------------------------------ 
  54. Procedurewrite_data (p_conninoutnocopyutl_smtp.Connection,
  55. P_nameinvarchar2,
  56. P_valueinvarchar2,
  57. P_splitevarchar2default':',
  58. P_crlfvarchar2defaultl_crlf)Is 
  59. Begin 
  60. /* Utl_raw.cast_to_raw is very important for Solving Chinese garbled characters */
  61. Utl_smtp.write_raw_data (p_conn, utl_raw.cast_to_raw (Convert(P_name | p_splite | p_value | p_crlf,'Zhs16cgb231280')));
  62. End;
  63. ---------------------------------------- Write the mime mail tail ----------------------------------------------------- 
  64. Procedureend_boundary (conninoutnocopyutl_smtp.Connection, Lastinbooleandefaultfalse)Is 
  65. Begin 
  66. Utl_smtp.write_data (conn, utl_tcp.crlf );
  67. If (Last)Then 
  68. Utl_smtp.write_data (conn, last_boundary );
  69. Endif;
  70. End;
  71. --------------------------------------------- The process of actually sending emails -------------------------------------------- 
  72. Procedurep_email (p_sendoraddress2varchar2,-- Sending Address 
  73. P_receiveraddress2varchar2)-- Accept address 
  74. Is 
  75. Rochelle connutl_smtp.Connection;-- Define a connection 
  76. Begin 
  77. /* Initialize the email server information and connect to the email server */
  78. Rochelle Conn: = utl_smtp.open_connection (p_server, p_port );
  79. Utl_smtp.helo (l_conn, p_server );
  80. /* Smtp server logon check */
  81. Ifp_need_smtp = 1 then
  82. Utl_smtp.command (l_conn,'Authlogin','');
  83. Utl_smtp.command (l_conn, utl_raw.cast_to_varchar2 (utl_encode.base64_encode (utl_raw.cast_to_raw (p_user ))));
  84. Utl_smtp.command (l_conn, utl_raw.cast_to_varchar2 (utl_encode.base64_encode (utl_raw.cast_to_raw (p_pass ))));
  85. Endif;
  86.  
  87. /* Set the Sending address and Receiving address */
  88. Utl_smtp.mail (l_conn, p_sendoraddress2 );
  89. Utl_smtp.rcpt (l_conn, p_receiveraddress2 );
  90.  
  91. /* Set the mail header */
  92. Utl_smtp.open_data (l_conn );
  93. /* Set the date */
  94. -- Write_data (l_conn, 'date', to_char (sysdate-1/3, 'ddmonyyhh24: mi: ss ')); 
  95. /* Set the sender */
  96. Write_data (l_conn,'From', P_sendor );
  97. /* Set the recipient */
  98. Write_data (l_conn,'To', P_receiver );
  99. /* Set the Email Subject */
  100. Selectreplace ('=? GB2312? B? '| Utl_raw.cast_to_varchar2 (utl_encode.base64_encode (rawtohex (p_sub) |'? =', Utl_tcp.crlf,'') Intop_subjectfromdual;
  101. Write_data (l_conn,'Subobject', P_subject );
  102.  
  103. Write_data (l_conn,'Content-type', Multipart_mime_type );
  104. Utl_smtp.write_data (l_conn, utl_tcp.crlf );
  105. Utl_smtp.write_data (l_conn, first_boundary );
  106. Write_data (l_conn,'Content-type','Text/html; charset = gb2312');
  107. -- Empty one line. Otherwise, the body content is not displayed. 
  108. Utl_smtp.write_data (l_conn, utl_tcp.crlf );
  109. /* Set the mail body
  110. Returns the separator to chr (10 ). This is mainly used to call this process in shell. If there are multiple rows, merge the contents of multiple rows into one row and separate them with l_splite.
  111. Then replace chr (10) with l_crlf ). This step is required. Otherwise, you cannot send emails with multiple lines in the mail body.
  112. */
  113. Write_data (l_conn,'',Replace(Replace(P_txt, l_splite, chr (10), chr (10), l_crlf ),'','');
  114. End_boundary (l_conn );
  115.  
  116. /* Close Data Writing */
  117. Utl_smtp.close_data (l_conn );
  118. /* Close the connection */
  119. Utl_smtp.quit (l_conn );
  120.  
  121. End;
  122. --------------------------------------------- Main process ----------------------------------------------------- 
  123. Begin 
  124. Rochelle sendoraddress: ='<'| P_sendor |'>';
  125. P_splite_str (p_receiver );-- Process the email address 
  126. Forkin1.. my_address_list.countloop
  127. P_email (l_sendoraddress, my_address_list (k ));
  128. Endloop;
  129. /* Process the email address and separate the email by commas */
  130. End;

Test the performance of PL/SQL:

Go to Outlook and check if Oracle has sent an Email:

Haha! Receive the data in time. You can send related data at the underlying layer, hoping it will be useful to you!

In addition, I do not know if the 11G function has been used to send emails directly using Oracle ~

  1. OOP concepts in Oracle databases
  2. Prospective role in Oracle Database Maintenance
  3. Use Resource Manager to optimize Oracle Performance
  4. Oracle retrieval data consistency and transaction recovery
  5. Design Method of super large Oracle database application system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.