views:

464

answers:

2

Hello!

Somebody can help me how can i use the gmail smtp server programmatically via socketConnection. My question is how i can write write a TSL/SSL authentication because i can`t communicate with the server?? Somebody did it from java on blackberry ?

Thank You

Alex

A: 

There's a lib for sending mail in Java, and it's called JavaMail. I don't know if it can be used with Blackberry, but if latter is the case, just use this class:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtils {
    private MailUtils() {
    }

    public static void sendSSLMessage(String recipients[], String subject,
                           String message, String from) throws MessagingException {
        boolean debug = true;
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("[email protected]", "your password");
                    }
                }
        );
        session.setDebug(debug);
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
}
mindas
This won't work on BlackBerry - those javax.mail.* classes aren't available in J2ME.
Marc Novakowski
Ok, sorry then. Do you want me to remove my answer then?
mindas
Martc Novakowski have right, i tried everything what i come to my mind... i saw in the net one solution but im not sure it will work and it is a little bit expensive..
Alex
+3  A: 

How about an open source email client for the blackberry. It has no problems using gmail's smtp server and handle's TSL/SSL authentication without a problem.

It happens to be the most popular open-source email client available for the blackberry that RIM has yet to discover.

Here is a page from which you can download it and try it, or get all the source code: http://www.logicprobe.org/proj/logicmail

Vernon
Hi Vernom, thank you for your reply but i must send email programmatically within my application via smpt (the application is not an email client)
Alex
That's the whole point of open-source. LogicMail does everything you're trying to do, and you can look at its source code. You basically have two problems to solve: opening SSL network connections and speaking the SMTP protocol.
octo
What about the license ? Can i use it in a non open source project? I read about the BSD license but it is not clear to me if i can use or not in closed source projects
Alex
Thank you for your idea. its working :)
Alex