Hello everybody,

I am currently trying to create a program that is supposed to send e-mail. Below is the piece of code I wrote :

import javax.mail.*;
import javax.mail.internet.*;
 
import java.util.Properties;
 
import java.io.PrintWriter;
import java.io.StringWriter;
 
public class Mail {
    public static String send() throws Exception{
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "mySMTPURL");
 
      MimeMessage message = new MimeMessage(Session.getDefaultInstance(props,null));
      message.setSubject("Testing javamail plain");
      message.setContent("This is a test", "text/plain;charset=\"iso-8859-1\"");
      message.setFrom(new InternetAddress("myAddressFrom"));
      message.addRecipient(Message.RecipientType.TO,
                          new InternetAddress("myAddressTo"));
 
      if (message instanceof MimeMessage){
        try{
          Transport.send(message);
          return new String("Success");
        } catch (Exception e){
          StringWriter sw = new StringWriter();
          PrintWriter pw = new PrintWriter(sw);
          e.printStackTrace(pw);
          return sw.toString();
        }
      }
      else{
        return new String("Not an instance of MimeMessage");
      }
    }
}

But when I execute it I got the below error message

javax.mail.MessagingException: SMTP can only send RFC822 messages
	at javax.mail.Transport.send0(Transport.java:219)
	at javax.mail.Transport.send(Transport.java:81)
	at Mail.send(Mail:24)

After looking into javax.mail.Transport source code, I saw that this exception is raised when the message to be sent is not an instance of the class MimeMessage. However, if I add the check in the code as shown above, the method send is still invoked. I really don't understand why this is not working. Can anyone of you help me please ?

For additional information, please note that I am executing this piece of code from a Java source compiled on an Oracle 11g2 database (which has JVM in version 1.5 if I'm not wrong).

Thanks in advance for your help.