简体   繁体   中英

Gmail smtp, Blackberry, BB, socketConnection, send email via gmail smtp programmatically

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

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

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("your@email.at.gmail", "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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM