Here is the code, sorry I didn't clean it up but it should be readable.
It's in Groovy, but it's very much like Java. You need the 2 jars: mail.jarand
activation.jar. Replace yahoo by gmail, I tested it on both. Double check
the STMP params, I can't remember if there are exactly the same for GMail
(port)

I put the constants in the file ApplicationConfig.groovy with this:

class ApplicationConfig {
static final EMAIL_SERVER  = "smtp.mail.yahoo.com"
static final EMAIL_PROTOCOL  = "smpts"
static final EMAIL_DEBUG  = true
static final EMAIL_SECURE  = true
static final EMAIL_USERNAME  = "yourusername"
static final EMAIL_PASSWORD  = "yourpassword"
static final EMAIL_FROM  = "[EMAIL PROTECTED] <[EMAIL PROTECTED]>"
static final EMAIL_REPLYTO  = "yourusername.com <[EMAIL PROTECTED]>"
}

The file with the mail code is below. Change the "from", "to", "cc" stuff
for your own use. in my app I use "bcc" only. Check the java Mail API docs
too. Hope this helps.

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.*;

class Notification {

static synchronized String sendEmail(to,subject,body) {
String mailhost = ApplicationConfig.EMAIL_SERVER;

String mailer = "GroovySend"
String protocol = ApplicationConfig.EMAIL_PROTOCOL
boolean debug = ApplicationConfig.EMAIL_DEBUG;
boolean auth = false;
boolean ssl = false;
boolean tls = false;

 String from = ApplicationConfig.EMAIL_FROM
 String replyto = ApplicationConfig.EMAIL_REPLYTO
 String cc = null
//  String cc = ApplicationConfig.EMAIL_REPLYTO
String bcc = to; // Changed 19-Feb-07 to use bcc instead of 'to'
to = ApplicationConfig.EMAIL_REPLYTO  // send a copy to 'to'

 String user = ApplicationConfig.EMAIL_USERNAME
 String password = ApplicationConfig.EMAIL_PASSWORD

 if (ApplicationConfig.EMAIL_SECURE)
   {
    auth = true
    ssl = true
    tls = true
   }

   System.out.println("sendEmail")
  Properties props = System.getProperties();
   System.out.println("mailhost: " + mailhost);
 props.put("mail.smtp.host", mailhost);

   protocol = ssl ? "smtps" : "smtp"
  if (auth)
     {
      System.out.println("auth: true protocol: " + protocol);
      props.put("mail." + protocol + ".auth", "true");
     }
  // Get a Session object
  Session session = Session.getInstance(props, null);
  if (debug) session.setDebug(true);

  // construct the message
  Message msg = new MimeMessage(session);

   System.out.println("from: " + from);
   msg.setFrom(new InternetAddress(from));

   msg.setReplyTo(InternetAddress.parse(replyto, false));  // strict =
false
   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));

   System.out.println("subject: " + subject);
   msg.setSubject(subject);

   System.out.println("body: " + body);
   msg.setText(body);

  msg.setHeader("X-Mailer", mailer);
   System.out.println("msg.setSendDate");
  msg.setSentDate(new Date());

  SMTPTransport t = (SMTPTransport)session.getTransport(ssl ? "smtps" :
"smtp");
   System.out.println("ssl: " + ssl);

   if (tls) t.setStartTLS(true);  // MUST BE HERE to activate TLS
   System.out.println("getStartTLS: " + t.getStartTLS());

  try
     {
   if (auth)
         {
         System.out.println("connecting with auth mailhost: " + mailhost +
" user: " + user + " password: " + password);
       t.connect(mailhost, user, password);
       }
   else
         {
       t.connect();
       }
   t.sendMessage(msg, msg.getAllRecipients());
     System.out.println("\nMail was sent successfully.");
     return 'email sent successfully!'
     }
   catch (Exception e)
     {
    System.out.println("Send failed: " + e.toString());
    }
   finally
     {
     System.out.println("Response: " + t.getLastServerResponse());
     t.close();
     return 'Error sending email!'
     }
   }
}




On 6/14/07, Guy <[EMAIL PROTECTED]> wrote:

Thanks for the reply!

do you happen to have code that actually does this? Like i said, i am
new to javaMail and i am having difficulty setting up the SSL.

It would be much appreciated

Guy

On 6/14/07, Fred Janon <[EMAIL PROTECTED]> wrote:
> I had the same issue a couple of months ago. The Commons-Mail library
> doesn't seem to support SSL and it's needed to connect to Google or
Yahoo
> SMTP mail. I had to call the javax.mail library directly, it turned out
not
> that difficult.
>
> Fred
>
> On 6/14/07, Guy <[EMAIL PROTECTED]> wrote:
> >
> > ---------- Forwarded message ----------
> > From: Guy <[EMAIL PROTECTED]>
> > Date: Jun 14, 2007 11:16 AM
> > Subject: email1-0 exception reading response:
> > java.net.SocketException: Connection reset
> > To: [EMAIL PROTECTED]
> >
> >
> > Hi,
> >
> > i admit, i am new to the commons-mail project and this is a newbie
> > question, i am totally aware of it:
> >
> > i am trying to send a simple email using googles SMTP server, but i
> > keep on getting "exception reading response: java.net.SocketException:
> > Connection reset" errors.
> >
> > This is my code which is straightforward (the To address and password
> > are replaced of cource):
> >
> >        try
> >        {
> >            SimpleEmail email = new SimpleEmail();
> >
> >            email.setHostName("smtp.gmail.com");
> >            email.setSmtpPort(465);
> >            email.addTo("[EMAIL PROTECTED]", "recipientname");
> >            email.setFrom("[EMAIL PROTECTED]", "myname");
> >            email.setSubject("Test message");
> >            email.setMsg("This is a simple test of commons-email");
> >            email.setDebug(true);
> >            email.setAuthentication("[EMAIL PROTECTED]
","mypwd");
> >            email.send();
> >        }
> >        catch (Exception e)
> >        {
> >           e.printStackTrace();
> >        }
> >
> > This is the console output:
> >
> > BEGIN Thu Jun 14 09:03:16 CEST 2007
> > DEBUG: JavaMail version 1.4ea
> > DEBUG: java.io.FileNotFoundException: C:\Program
> > Files\Java\jdk1.5.0_07\jre\lib\javamail.providers (The system cannot
> > find the file specified)
> > DEBUG: URL
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/imap.jar!/META-INF/javamail.providers
> > DEBUG: successfully loaded resource:
> >
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/imap.jar!/META-INF/javamail.providers
> > DEBUG: URL
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/pop3.jar!/META-INF/javamail.providers
> > DEBUG: successfully loaded resource:
> >
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/pop3.jar!/META-INF/javamail.providers
> > DEBUG: URL
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/smtp.jar!/META-INF/javamail.providers
> > DEBUG: successfully loaded resource:
> >
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/smtp.jar!/META-INF/javamail.providers
> > DEBUG: successfully loaded resource:
/META-INF/javamail.default.providers
> > DEBUG: Tables of loaded providers
> > DEBUG: Providers Listed By Class Name:
> > {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider
[TRANSPORT,smtps,
> > com.sun.mail.smtp.SMTPSSLTransport,Sun
> > Microsystems, Inc],
> > com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,
> > com.sun.mail.smtp.SMTPTransport,Sun
> > Microsystems, Inc],
> > com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,
> > com.sun.mail.imap.IMAPSSLStore,Sun
> > Microsystems, Inc],
> > com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,
> > com.sun.mail.pop3.POP3SSLStore,Sun
> > Microsystems, Inc],
> > com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,
> > com.sun.mail.imap.IMAPStore,Sun
> > Microsystems, Inc],
> > com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,
> > com.sun.mail.pop3.POP3Store,Sun
> > Microsystems, Inc]}
> > DEBUG: Providers Listed By Protocol:
> > {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore
,Sun
> > Microsystems, Inc],
> > imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun
> > Microsystems, Inc],
> > smtps=javax.mail.Provider[TRANSPORT,smtps,
> > com.sun.mail.smtp.SMTPSSLTransport,Sun
> > Microsystems, Inc],
> > pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun
> > Microsy stems, Inc],
> > smtp=javax.mail.Provider[TRANSPORT,smtp,
com.sun.mail.smtp.SMTPTransport
> > ,Sun
> > Microsystems, Inc],
> > pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore
,Sun
> > Microsystems, Inc]}
> > DEBUG: successfully loaded resource:
> > /META-INF/javamail.default.address.map
> > DEBUG: URL
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/smtp.jar!/META-INF/javamail.address.map
> > DEBUG: successfully loaded resource:
> >
> >
jar:file:/D:/MyDoc/develop/Test/Mail/lib/smtp.jar!/META-INF/javamail.address.map
> > DEBUG: java.io.FileNotFoundException: C:\Program
> > Files\Java\jdk1.5.0_07\jre\lib\javamail.address.map (The system cannot
> > find the file specified)
> > DEBUG: getProvider() returning
> > javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun
> > Microsystems, Inc]
> > DEBUG SMTP: useEhlo true, useAuth true
> > DEBUG SMTP: useEhlo true, useAuth true
> > DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465,
isSSL
> > false
> > DEBUG SMTP: exception reading response: java.net.SocketException:
> > Connection reset
> > END Thu Jun 14 09:14:46 CEST 2007
> > org.apache.commons.mail.EmailException: Sending the email to the
> > following server failed : smtp.gmail.com:465
> >        at org.apache.commons.mail.Email.sendMimeMessage(Email.java
:873)
> >        at org.apache.commons.mail.Email.send(Email.java:898)
> >        at TestMail.main(TestMail.java:26)
> > Caused by: javax.mail.MessagingException: Exception reading response;
> > nested exception is:
> >        java.net.SocketException: Connection reset
> >        at com.sun.mail.smtp.SMTPTransport.readServerResponse(
> > SMTPTransport.java:1462)
> >        at com.sun.mail.smtp.SMTPTransport.openServer(
SMTPTransport.java
> > :1260)
> >        at com.sun.mail.smtp.SMTPTransport.protocolConnect(
> > SMTPTransport.java:370)
> >        at javax.mail.Service.connect(Service.java:297)
> >        at javax.mail.Service.connect(Service.java:156)
> >        at javax.mail.Service.connect(Service.java:105)
> >        at javax.mail.Transport.send0(Transport.java:168)
> >        at javax.mail.Transport.send(Transport.java:98)
> >        at org.apache.commons.mail.Email.sendMimeMessage(Email.java
:863)
> >        ... 2 more
> > Caused by: java.net.SocketException: Connection reset
> >        at java.net.SocketInputStream.read(SocketInputStream.java:168)
> >        at com.sun.mail.util.TraceInputStream.read(
TraceInputStream.java
> > :97)
> >        at java.io.BufferedInputStream.fill(BufferedInputStream.java
:218)
> >        at java.io.BufferedInputStream.read(BufferedInputStream.java
:235)
> >        at com.sun.mail.util.LineInputStream.readLine(
LineInputStream.java
> > :75)
> >        at com.sun.mail.smtp.SMTPTransport.readServerResponse(
> > SMTPTransport.java:1440)
> >        ... 10 more
> >
> >
> > Thanks in advance
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to