[Pljava-dev] emailing with pl/java

Roberts, Jon Jon.Roberts at asurion.com
Mon May 5 15:14:39 UTC 2008


I made some progress.  I extracted the contents of mail.jar and
activation.jar and removed the mailcap entry from my code.  I then
created a new class that works.  I then created a new jar file including
everything (including the mailcap stuff) that works from a command line.

java -jar smtp.jar smtp_server jon.roberts at asurion.com
jon.roberts at asurion.com test test

My classpath only has pljava in it and this emails me just fine. 

I then loaded this into PostgreSQL and created a function to wrap it.

select * from sqlj.install_jar('file:///c://java3//smtp.jar',
'smtp_jar', true);
select sqlj.set_classpath('control', 'smtp_jar');

select * from sqlj.get_classpath('control');

CREATE OR REPLACE FUNCTION control.fn_smtp(p_smtp_server varchar, p_to
varchar, p_from varchar, p_subject varchar, p_body varchar) returns
varchar as
'smtp.send(java.lang.String, java.lang.String, java.lang.String,
java.lang.String, java.lang.String)'
  LANGUAGE 'java';


Then I run it from 
select control.fn_smtp('smtp_server', 'jon.roberts at asurion.com', 
                       'jon.roberts at asurion.com', 'Test Subject', 
                       'test');


It returns: "ERROR: IOException while sending message"

The debug information is:

DEBUG:  StartTransactionCommand
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR,
xid/subid/cid: 0/1/0, nestlvl: 1, children: <>
DEBUG:  find_in_dynamic_libpath: trying "C:/Program
Files/PostgreSQL/8.3/lib/pljava"
DEBUG:  find_in_dynamic_libpath: trying "C:/Program
Files/PostgreSQL/8.3/share/pljava/pljava"
DEBUG:  find_in_dynamic_libpath: trying "C:/Program
Files/PostgreSQL/8.3/lib/pljava.dll"
DEBUG:  Not using integer_datetimes
DEBUG:  Added JVM option string "-Djava.class.path=C:\Program
Files\PostgreSQL\8.3\share\pljava.jar;.;C:\Program
Files\PostgreSQL\8.3\share\pljava\pljava.jar"
DEBUG:  Added JVM option string
"-Dsqlj.defaultconnection=jdbc:default:connection"
DEBUG:  Added JVM option string "vfprintf"
DEBUG:  Added JVM option string "-Xrs"
DEBUG:  Creating JavaVM
DEBUG:  JavaVM created
DEBUG:  Getting Backend class pljava.jar
DEBUG:  Backend class was there
DEBUG:  05 May 08 10:12:19 org.postgresql.pljava.internal.Backend Using
SecurityManager for trusted language
DEBUG:  className = 'smtp', methodName = 'send', parameters =
'java.lang.String,java.lang.String,java.lang.String,java.lang.String,jav
a.lang.String', returnType = 'null'
DEBUG:  05 May 08 10:12:19 org.postgresql.pljava.sqlj.Loader Creating
typeMappings for schema control
DEBUG:  Loading class smtp
DEBUG:  Obtaining method smtp.send
(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String
;Ljava/lang/String;)Ljava/lang/String;
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: load HOME
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport new
MailcapFile: file C:\Documents and Settings\LocalService\.mailcap
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: load SYS
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport new
MailcapFile: file C:\Program Files\Java\jdk1.6.0_02\jre\lib\mailcap
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: load JAR
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: !anyLoaded
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: not loading mailcap file: /META-INF/mailcap
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: load DEF
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport new
MailcapFile: InputStream
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
parse: image/gif;;
x-java-view=com.sun.activation.viewers.ImageViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Type: image/gif
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Command: view, Class: com.sun.activation.viewers.ImageViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
parse: image/jpeg;;
x-java-view=com.sun.activation.viewers.ImageViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Type: image/jpeg
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Command: view, Class: com.sun.activation.viewers.ImageViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
parse: text/*;;
x-java-view=com.sun.activation.viewers.TextViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Type: text/*
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Command: view, Class: com.sun.activation.viewers.TextViewer
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
parse: text/*;;
x-java-edit=com.sun.activation.viewers.TextEditor
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Type: text/*
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Command: edit, Class: com.sun.activation.viewers.TextEditor
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
Merging commands for type text/*
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: successfully loaded mailcap file:
/META-INF/mailcap.default
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
MailcapCommandMap: createDataContentHandler for text/plain
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
search DB #1
DEBUG:  05 May 08 10:12:19 com.sun.activation.registries.LogSupport
search fallback DB #1
DEBUG:  CommitTransactionCommand
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR,
xid/subid/cid: 0/1/0, nestlvl: 1, children: <>


It finds the mailcap for text/plain and it looks like it works but then
it throws the error and it doesn't email me.  

What gives?  Why doesn't this work in pl/java but works just fine from
the command line?


Jon
> -----Original Message-----
> From: pljava-dev-bounces at pgfoundry.org [mailto:pljava-dev-
> bounces at pgfoundry.org] On Behalf Of Roberts, Jon
> Sent: Friday, May 02, 2008 3:46 PM
> To: pljava-dev at pgfoundry.org
> Subject: [Pljava-dev] emailing with pl/java
> 
> I can't get this to work inside of PostgreSQL.
> 
> 1.  Java code
> 
> import javax.mail.*;
> import javax.mail.internet.*;
> import java.util.*;
> import javax.activation.*;
> 
> public class SMTP {
> 
> 
> 	public static void main(String args[])
> 	{
> 		try
>     	{
>     		String smtpServer=args[0];
> 			String to=args[1];
> 			String from=args[2];
> 			String subject=args[3];
> 			String body=args[4];
> 			String output = send(smtpServer, to, from,
> subject, body);
> 		}
> 	    catch (Exception ex)
> 	    {
> 			System.out.println("Usage: java SMTP smtpServer
> toAddress fromAddress subjectText bodyText");
> 		}
> 
> 	    System.exit(0);
> 	}
> 
> 	public static String send(String smtpServer, String to, String
> from, String subject, String body)
> 	{
> 
> 		try
> 		{
> 
> 			MailcapCommandMap mc =
> (MailcapCommandMap)CommandMap.getDefaultCommandMap();
> 			mc.addMailcap("text/plain; ;
> x-java-content-handler=com.sun.mail.handlers.text_plain");
> 			CommandMap.setDefaultCommandMap(mc);
> 
> 			Properties props = System.getProperties();
> 			props.setProperty("mail.smtp.host", smtpServer);
> 
> 			Session session =
> Session.getDefaultInstance(props, null);
> 
> 			Message msg = new MimeMessage(session);
> 
> 			msg.setFrom(new InternetAddress(from));
> 
> 			msg.setRecipients(Message.RecipientType.TO,
> InternetAddress.parse(to, false));
> 
> 			msg.setSubject(subject);
> 
> 			msg.setText(body);
> 
> 			Transport.send(msg);
> 
> 			return "Success";
> 
> 		}
>     	catch (Exception ex)
>     	{
>     		return "ERROR: " + ex.getMessage().replace("'", "");
>     	}
>   	}
> }
> 
> 2.  Set classpath
> C:\java_test>echo %CLASSPATH%
> .;C:\Program
> Files\PostgreSQL\8.3\share\pljava\pljava.jar;c:\java_test\activatio
> n.jar;c:\java_test\mail.jar
> 
> 3.  Edit manifest.txt
> C:\java_test>type manifest.txt
> Main-Class: SMTP
> Class-Path: activation.jar mail.jar
> 
> 4.  Compile Java
> C:\java_test>javac SMTP.java
> 
> 5.  Create a Jar
> C:\java_test>jar cfm SMTP.jar manifest.txt SMTP.class
> 
> 6.  Test from a command line
> C:\java_test>java -jar SMTP.jar
> Usage: java SMTP smtpServer toAddress fromAddress subjectText bodyText
> 
> I also tested with my email address and it works just fine.
> 
> 7.  Load into PostgreSQL
> select * from sqlj.install_jar('file:///c://java_test//SMTP.jar',
> 'smtp_jar', false);
> select * from
sqlj.install_jar('file:///c://java_test//activation.jar',
> 'activation_jar', false);
> select * from sqlj.install_jar('file:///c://java_test//mail.jar',
> 'mail_jar', false);
> 
> select sqlj.set_classpath('control',
> 'smtp_jar:activation_jar:mail_jar');
> 
> select * from sqlj.get_classpath('control')
> 
> CREATE OR REPLACE FUNCTION control.fn_smtp(p_smtp_server varchar, p_to
> varchar, p_from varchar, p_subject varchar, p_body varchar) returns
> varchar as
> 'SMTP.send(java.lang.String, java.lang.String, java.lang.String,
> java.lang.String, java.lang.String)'
>   LANGUAGE 'java';
> 
> 
> select control.fn_smtp('smtp_server', 'from at foo.bar',
>                        'to at foo.bar', 'Test Subject',
>                        'Test body');
> 
> I get this error: "ERROR: IOException while sending message"
> 
> 
> I then put PostgreSQL in full debug and I captured this:
> 
> DEBUG:  02 May 08 15:36:10 com.sun.activation.registries.LogSupport
> Can't load DCH com.sun.mail.handlers.text_plain
> java.lang.ClassNotFoundException: com/sun/mail/handlers/text_plain
> 	at java.lang.Class.forName0(Native Method)
> 	at java.lang.Class.forName(Class.java:169)
> 	at
>
javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandM
> ap.java:578)
> 	at
>
javax.activation.MailcapCommandMap.createDataContentHandler(MailcapComma
> ndMap.java:535)
> 	at
>
javax.activation.DataHandler.getDataContentHandler(DataHandler.java:596)
> 	at javax.activation.DataHandler.writeTo(DataHandler.java:300)
> 	at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:261)
> 	at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1321)
> 	at
> javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2074)
> 	at
> javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
> 	at javax.mail.Transport.send(Transport.java:117)
> 	at SMTP.send(SMTP.java:53)
> 
> 
> I'm guessing the problem is this line:
> 
> mc.addMailcap("text/plain; ;
> x-java-content-handler=com.sun.mail.handlers.text_plain");
> 
> This class is in the mail.jar file and it is installed with
> sqlj.install_jar.   Why can't pl/java find this class?  Mail.jar is
> loaded into the db.  It finds other classes loaded into the db like
> javax.mail.* but it can't seem to resolve this one that is handled in
a
> String.
> 
> 
> 
> Jon
> _______________________________________________
> Pljava-dev mailing list
> Pljava-dev at pgfoundry.org
> http://pgfoundry.org/mailman/listinfo/pljava-dev



More information about the Pljava-dev mailing list