【问题标题】:Email external file using Oracle Forms 11g使用 Oracle Forms 11g 通过电子邮件发送外部文件
【发布时间】:2017-12-21 11:49:17
【问题描述】:

我已配置电子邮件设置,并且能够通过 Oracle Forms 11g 发送电子邮件。现在我需要在发送的电子邮件中附加保存在我计算机中某处的 PDF。我经历了不同的程序,但无法成功发送 PDF 文件。

谢谢。

编辑:添加邮件发送程序代码

create or replace procedure send4 (p_sender IN VARCHAR2, p_recipient IN VARCHAR2, p_subject IN VARCHAR2, p_message IN VARCHAR2)
IS
crlf         VARCHAR2(2)  := chr(13)||chr(10);
l_mailhost VARCHAR2(255) := <IP ADDRESS>;
v_connection             UTL_SMTP.connection;

BEGIN
V_CONNECTION := utl_smtp.open_connection(l_mailhost, 25);
utl_smtp.Helo(V_CONNECTION, l_mailhost);
utl_smtp.Mail(V_CONNECTION, p_sender);
utl_smtp.Rcpt(V_CONNECTION, p_recipient);
utl_smtp.Data(V_CONNECTION,
'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: '   || p_sender || crlf ||
'Subject: '|| p_subject || crlf ||
'To: '     || p_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_message|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="file.txt"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="attachment.txt"'|| crlf ||
crlf ||
p_message|| crlf || -- Content of attachment
crlf ||
'-------SECBOUND--'         -- End MIME mail
);
UTL_SMTP.quit(v_connection);
EXCEPTION
WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
raise_application_error(-20000, 'Unable to send mail', TRUE);
END;

【问题讨论】:

  • 哪台电脑? Forms 运行的中间层服务器?还是用户的电脑,本地客户端?
  • 那么情况是,当报告以 PDF 格式生成然后保存时(它也应该通过电子邮件发送)所以我猜它需要在服务器端完成,对吧?该报告包含图表和所有内容。
  • 你应该提供更多关于你用来发送邮件的信息吗?
  • 我已经使用调用发送电子邮件的程序编辑了帖子。再次记住,我不希望将文本转换为 pdf 然后作为附件发送,而是需要通过电子邮件发送生成的报告。谢谢。

标签: oracle oracle11g email-attachments oracleforms


【解决方案1】:

如果您使用 Oracle Reports 11g 创建 PDF,您应该查看 DISTRIBUTION 设置。您可以显示 PDF 以及来自同一报告的电子邮件。否则,请考虑从您的 Oracle Form 启动一个批处理文件,该文件从命令行调用报告和电子邮件。

【讨论】:

    【解决方案2】:

    UTL_SMTP 无法承受大附件,这里有解决办法,您可以附加文件 创建java源

    CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "SendMail" AS
      import java.util.*;
      import java.io.*;
      import javax.mail.*;
      import javax.mail.internet.*;
      import javax.activation.*;
      public class SendMail {
         // Sender, Recipient, CCRecipient, and BccRecipient are
         // commaseparated lists of addresses.
         // Body can span multiple CR/LF-separated lines.
         // Attachments is a ///-separated list of file names.
         public static int Send(String SMTPServer,
                                String Sender,
                                String Recipient,
                                String CcRecipient,
                                String BccRecipient,
                                String Subject,
                                String Body,
                                String ErrorMessage[],
                                String Attachments) {
            // Error status
            int ErrorStatus = 0;
    
            // Create some properties and get the default Session
            Properties props = System.getProperties();
            props.put("mail.akadia.com", SMTPServer);
            Session session = Session.getDefaultInstance(props, null);
    
            try {
               // Create a message
               MimeMessage msg = new MimeMessage(session);
    
               // extracts the senders and adds them to the message
               // Sender is a comma-separated list of e-mail addresses as per RFC822
               {
                  InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
                  msg.addFrom(TheAddresses);
               }
    
               // Extract the recipients and assign them to the message.
               // Recipient is a comma-separated list of e-mail addresses as per RFC822.
               {
                  InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
                  msg.addRecipients(Message.RecipientType.TO,TheAddresses);
               }
    
               // Extract the Cc-recipients and assign them to the message.
               // CcRecipient is a comma-separated list of e-mail addresses as per RFC822.
               if (null != CcRecipient) {
                  InternetAddress[] TheAddresses = InternetAddress.parse(CcRecipient);
                  msg.addRecipients(Message.RecipientType.CC,TheAddresses);
               }
    
               // Extract the Bcc-recipients and assign them to the message.
               // BccRecipient is a comma-separated list of e-mail addresses as per RFC822.
               if (null != BccRecipient) {
                  InternetAddress[] TheAddresses = InternetAddress.parse(BccRecipient);
                  msg.addRecipients(Message.RecipientType.BCC,TheAddresses);
               }
    
               // Subject field
               msg.setSubject(Subject);
    
               // Create the Multipart to be added the parts to
               Multipart mp = new MimeMultipart();
    
               // Create and fill the first message part
               {
                  MimeBodyPart mbp = new MimeBodyPart();
                  mbp.setText(Body);
    
                  // Attach the part to the multipart
                  mp.addBodyPart(mbp);
               }
    
               // Attach the files to the message
               if (null != Attachments) {
                  int StartIndex = 0, PosIndex = 0;
                  while (-1 != (PosIndex = Attachments.indexOf("///",StartIndex))) {
                     // Create and fill other message parts;
                     MimeBodyPart mbp = new MimeBodyPart();
                     FileDataSource fds =
                     new FileDataSource(Attachments.substring(StartIndex,PosIndex));
                     mbp.setDataHandler(new DataHandler(fds));
                     mbp.setFileName(fds.getName());
                     mp.addBodyPart(mbp);
                     PosIndex += 3;
                     StartIndex = PosIndex;
                  }
                  // Last, or only, attachment file
                  if (StartIndex < Attachments.length()) {
                     MimeBodyPart mbp = new MimeBodyPart();
                     FileDataSource fds = new FileDataSource(Attachments.substring(StartIndex));
                     mbp.setDataHandler(new DataHandler(fds));
                     mbp.setFileName(fds.getName());
                     mp.addBodyPart(mbp);
                  }
               }
    
               // Add the Multipart to the message
               msg.setContent(mp);
    
               // set the Date: header
               msg.setSentDate(new Date());
    
               // Send the message
               Transport.send(msg);
            } catch (MessagingException MsgException) {
               ErrorMessage[0] = MsgException.toString();
               Exception TheException = null;
               if ((TheException = MsgException.getNextException()) != null)
                 ErrorMessage[0] = ErrorMessage[0] + "\n" + TheException.toString();
                 ErrorStatus = 1;
            }
            return ErrorStatus;
         } // End Send Class
      } // End of public class SendMail
    /
    

    创建包体以使用上面编译的java

    CREATE OR REPLACE PACKAGE SendMailJPkg AS
       -- EOL is used to separate text line in the message body
       EOL CONSTANT STRING(2) := CHR(13) || CHR(10);
    
       TYPE ATTACHMENTS_LIST IS TABLE OF VARCHAR2(4000);
    
       -- High-level interface with collections
       FUNCTION SendMail(SMTPServerName IN STRING,
                         Sender IN STRING,
                         Recipient IN STRING,
                         CcRecipient IN STRING DEFAULT '',
                         BccRecipient IN STRING DEFAULT '',
                         Subject IN STRING DEFAULT '',
                         Body IN STRING DEFAULT '',
                         ErrorMessage OUT STRING,
                         Attachments IN ATTACHMENTS_LIST DEFAULT NULL) RETURN NUMBER;
    END SendMailJPkg;
    /
    CREATE OR REPLACE PACKAGE BODY SendMailJPkg AS
       PROCEDURE ParseAttachment(Attachments IN ATTACHMENTS_LIST,
                                 AttachmentList OUT VARCHAR2) IS
       AttachmentSeparator CONSTANT VARCHAR2(12) := '///';
       BEGIN
          -- Boolean short-circuit is used here
          IF Attachments IS NOT NULL AND Attachments.COUNT > 0 THEN
             AttachmentList := Attachments(Attachments.FIRST);
             -- Scan the collection, skip first element since it has been
             -- already processed;
             -- accommodate for sparse collections;
             FOR I IN Attachments.NEXT(Attachments.FIRST) .. Attachments.LAST LOOP
                AttachmentList := AttachmentList || AttachmentSeparator || Attachments(I);
             END LOOP;
          ELSE
             AttachmentList := '';
          END IF;
       END ParseAttachment;
    
       -- Forward declaration
       FUNCTION JSendMail(SMTPServerName IN STRING,
                          Sender IN STRING,
                          Recipient IN STRING,
                          CcRecipient IN STRING,
                          BccRecipient IN STRING,
                          Subject IN STRING,
                          Body IN STRING,
                          ErrorMessage OUT STRING,
                          Attachments IN STRING) RETURN NUMBER;
    
       -- High-level interface with collections
       FUNCTION SendMail(SMTPServerName IN STRING,
                         Sender IN STRING,
                         Recipient IN STRING,
                         CcRecipient IN STRING,
                         BccRecipient IN STRING,
                         Subject IN STRING,
                         Body IN STRING,
                         ErrorMessage OUT STRING,
                         Attachments IN ATTACHMENTS_LIST) RETURN NUMBER IS
          AttachmentList VARCHAR2(4000) := '';
          AttachmentTypeList VARCHAR2(2000) := '';
       BEGIN
          ParseAttachment(Attachments,AttachmentList);
          RETURN JSendMail(SMTPServerName,
                           Sender,
                           Recipient,
                           CcRecipient,
                           BccRecipient,
                           Subject,
                           Body,
                           ErrorMessage,
                           AttachmentList);
       END SendMail;
    
       -- JSendMail's body is the java function SendMail.Send()
       -- thus, no PL/SQL implementation is needed
       FUNCTION JSendMail(SMTPServerName IN STRING,
                          Sender IN STRING,
                          Recipient IN STRING,
                          CcRecipient IN STRING,
                          BccRecipient IN STRING,
                          Subject IN STRING,
                          Body IN STRING,
                          ErrorMessage OUT STRING,
                          Attachments IN STRING) RETURN NUMBER IS
       LANGUAGE JAVA
       NAME 'SendMail.Send(java.lang.String,
                           java.lang.String,
                           java.lang.String,
                           java.lang.String,
                           java.lang.String,
                           java.lang.String,
                           java.lang.String,
                           java.lang.String[],
                           java.lang.String) return int';
    END SendMailJPkg;
    /
    

    使用以下代码进行测试

    var ErrorMessage VARCHAR2(4000);
    var ErrorStatus NUMBER;
    
    -- enable SQL*PLUS output;
    SET SERVEROUTPUT ON
    -- redirect java output into SQL*PLUS buffer;
    exec dbms_java.set_output(5000);
    BEGIN
       :ErrorStatus := SendMailJPkg.SendMail(
                    SMTPServerName => 'localhost',
                    Sender    => 'martin.zahn@akadia.com',
                    Recipient => 'martin.zahn@akadia.com',
                    CcRecipient => '',
                    BccRecipient => '',
                    Subject   => 'This is the subject line: Test JavaMail',
                    Body => 'This is the body: Hello, this is a test' ||
                             SendMailJPkg.EOL || 'that spans 2 lines',
                    ErrorMessage => :ErrorMessage,
                    Attachments  => SendMailJPkg.ATTACHMENTS_LIST(
                                       'C:\Users\Zahn\Work\sendmail.sql',
                                       'C:\Users\Zahn\Work\ferien-2002.txt'
                                    )
       );
    END;
    /
    print
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2017-07-29
      • 2013-10-10
      • 2016-04-13
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      相关资源
      最近更新 更多