是否可以在Java ME 8.0上创建smtp客户端?

时间:2014-11-27 00:37:26

标签: java

我正在尝试使用JAVA ME Embeded最新版本(8.0)来连接到gmail和阅读电子邮件来构建覆盆子pi的应用程序。

但是,我无法在此平台上找到对此类操作的任何原生支持。任何人都可以告诉我它是否可以做到?

Java版链接: http://www.oracle.com/technetwork/java/embedded/javame/embed-me/overview/index.html (使用java me embeded sdk在Eclipse上开发)

1 个答案:

答案 0 :(得分:0)

是的,你可以!它需要一点点工作,但它绝对可能。

首先,您需要找到here的javax.mail.jar。

接下来,你需要写一些内容来写一封电子邮件(更多信息可以在上面的链接中找到):

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.*;

public class EmailProgram {
    public static void main(String[] args) {
        String nameString, emailString;
        nameString = JOptionPane.showInputDialog("Enter your name", "John Doe");
        emailString = JOptionPane.showInputDialog("Enter your email", "john@doe.com");

        final String username = "REDACTED EMAIL ADDRESS";
        final String password = "REDACTED PASSWORD";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("REDACTED EMAIL ADDRESS"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emailString));
            message.setSubject("Your Grade");
            message.setText("Hi " + nameString + "!\n" + "Your grade has been calculated. It is ");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
}