Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Oracle 11g: JavaMail can't send email outside the company's mail server

Ahmed HaroonMay 4 2017 — edited May 8 2017

hi all,

here is my code which copied from a website to test it, it is working fine on our own server but when trying to send to another server like: gmail, outlook etc. it failed.

JavaMail API ver. 1.5.5

loadjava -u sys/oracle@mytest -o -r -v -f -noverify -synonym -g public activation.zip

loadjava -u sys/oracle@mytest -o -r -v -f -noverify -synonym -g public javaxmail.zip

-- user = SYS

-- ==========

create or replace directory my_files as 'C:\app\orahome\oradata\attach_files\';

grant read, write on directory my_files to HR;

begin

  dbms_java.grant_permission(

    grantee => 'HR',

    permission_type => 'java.util.PropertyPermission',

    permission_name => '*',

    permission_action => 'read,write'

  );

  dbms_java.grant_permission(

    grantee => 'HR',

    permission_type => 'java.net.SocketPermission',

    permission_name => '*',

    permission_action => 'connect,resolve'

  );

  dbms_java.grant_permission(

    grantee => 'HR',

    permission_type => 'java.lang.RuntimePermission',

    permission_name => 'setFactory',

    permission_action => ''

  );

end;

-- user = HR

-- =========

create or replace and compile java source named "mail" as

   import java.io.*;

   import java.sql.*;

   import java.util.Properties;

   import java.util.Date;

   import javax.activation.*;

   import javax.mail.*;

   import javax.mail.internet.*;

   import oracle.jdbc.driver.*;

   import oracle.sql.*;

 

   public class mail

   {

     static String dftMime = "application/octet-stream";

     static String dftName = "filename.dat";

     public static oracle.sql.NUMBER

                          send(String from,

                               String to,

                               String cc,

                               String bcc,

                               String subject,

                               String body,

                               String SMTPHost,

                               oracle.sql.BLOB attachmentData,

                               String attachmentType,

                               String attachmentFileName)

     {

       int rc = 0;

 

       try

       {

         Properties props = System.getProperties();

         props.put("mail.smtp.host", SMTPHost);

         Message msg =

           new MimeMessage(Session.getDefaultInstance(props, null));

 

        msg.setFrom(new InternetAddress(from));

 

        if (to != null)

           msg.setRecipients(Message.RecipientType.TO,

                             InternetAddress.parse(to, false));

 

        if (cc != null)

           msg.setRecipients(Message.RecipientType.CC,

                             InternetAddress.parse(cc, false));

 

        if (bcc != null)

           msg.setRecipients(Message.RecipientType.BCC,

                             InternetAddress.parse(bcc, false));

 

        if ( subject != null)

             msg.setSubject(subject);

        else msg.setSubject("(no subject)");

 

         msg.setSentDate(new Date());

 

         if (attachmentData != null)

         {

           MimeBodyPart mbp1 = new MimeBodyPart();

           mbp1.setText((body != null ? body : ""));

           mbp1.setDisposition(Part.INLINE);

 

           MimeBodyPart mbp2 = new MimeBodyPart();

           String type =

               (attachmentType != null ? attachmentType : dftMime);

 

           String fileName = (attachmentFileName != null ?

                              attachmentFileName : dftName);

 

           mbp2.setDisposition(Part.ATTACHMENT);

           mbp2.setFileName(fileName);

 

           mbp2.setDataHandler(new

              DataHandler(new BLOBDataSource(attachmentData, type))

           );

 

           MimeMultipart mp = new MimeMultipart();

           mp.addBodyPart(mbp1);

           mp.addBodyPart(mbp2);

           msg.setContent(mp);

         }

         else

         {

           msg.setText((body != null ? body : ""));

         }

         Transport.send(msg);

         rc = 1;

       } catch (Exception e)

       {

         e.printStackTrace();

         rc = 0;

       } finally

       {

         return new oracle.sql.NUMBER(rc);

       }

     }

    // Nested class that implements a DataSource.

    static class BLOBDataSource implements DataSource

    {

      private BLOB   data;

      private String type;

  BLOBDataSource(BLOB data, String type)

      {

          this.type = type;

          this.data = data;

      }

      public InputStream getInputStream() throws IOException

      {

        try

        {

          if(data == null)

            throw new IOException("No data.");

          return data.getBinaryStream();

        } catch(SQLException e)

        {

          throw new

          IOException("Cannot get binary input stream from BLOB.");

        }

      }

      public OutputStream getOutputStream() throws IOException

      {

        throw new IOException("Cannot do this.");

      }

      public String getContentType()

      {

        return type;

      }

      public String getName()

      {

        return "BLOBDataSource";

      }

    }

  }

/

create or replace function send(

       p_from                  in varchar2,

       p_to                    in varchar2,

       p_cc                    in varchar2,

       p_bcc                   in varchar2,

       p_subject               in varchar2,

       p_body                  in varchar2,

       p_smtp_host             in varchar2,

       p_attachment_data       in blob,

       p_attachment_type       in varchar2,

       p_attachment_file_name  in varchar2) return number

   as

   language java name 'mail.send( java.lang.String,

                                  java.lang.String,

                                  java.lang.String,

                                  java.lang.String,

                                  java.lang.String,

                                  java.lang.String,

                                  java.lang.String,

                                  oracle.sql.BLOB,

                                  java.lang.String,

                                  java.lang.String

                                ) return oracle.sql.NUMBER';

/

create table att_files ( att_filename blob );

set serveroutput on size 1000000

declare

  l_blob  blob;

  l_bfile bfile;

begin

   insert into att_files values ( empty_blob() )

   returning att_filename into l_blob;

   l_bfile := bfilename( 'MY_FILES', 'javaxmail.zip' );

   dbms_lob.fileopen( l_bfile );

   dbms_lob.loadfromfile( l_blob, l_bfile,

                          dbms_lob.getlength( l_bfile ) );

   dbms_lob.fileclose( l_bfile );

end;

/

exec dbms_java.set_output( 1000000 );

declare

  ret_code number;

begin

  for i in (select att_filename from att_files )

  loop

    ret_code := send(

                 p_from => 'ahmed.haroon@myserver.com',

                 p_to => 'ahmed.haroon@myserver.com',

                 p_cc => 'friend.at.work@myserver.com',

                 p_bcc => NULL,

                 p_subject => 'Use the attached Zip file',

                 p_body => 'to send email with attachments....',

                 p_smtp_host => 'myserver.com',

                 p_attachment_data => i.att_filename,

                 p_attachment_type => 'application/winzip',

                 p_attachment_file_name => 'javaxmail.zip');

    if ret_code = 1 then

       dbms_output.put_line ('Successfully sent message...');

    else

       dbms_output.put_line ('Failed to send message...');

    end if;

  end loop;

end;

/

what i have missed or where i am wrong,

also please help, if i want to mention Multiples in p_to comma separated and same in CC, BCC will it work ? and also multiple attachments.

regards

This post has been answered by Mustafa KALAYCI on May 4 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 5 2017
Added on May 4 2017
23 comments
1,674 views