使用android应用程序发送邮件

时间:2014-09-04 22:07:47

标签: android email gmail

我正在编写一个Android应用程序,假设在某些时候发送电子邮件。我在谷歌创建了一个新的电子邮件帐户。用户名是:pickup.friend.carpool。这是我的邮件发送课程:

public class MailSending {

    private static String HOST = "smtp.gmail.com";
    private static String USER = "pickup.friend.carpool@gmail.com";
    private static String PASSWORD = ""; //My password
    private static String PORT = "465";
    private static String FROM = "mail.com";
    private static String TO;
    private static String STARTTLS = "true";
    private static String AUTH = "true";
    private static String DEBUG = "true";
    private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static String SUBJECT;
    private static String TEXT;

    public void send(String to) {
        TO = to;
        SUBJECT = "subject";
        TEXT = "body of mail";

        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.user", USER);
        props.put("mail.smtp.auth", AUTH);
        props.put("mail.smtp.starttls.enable", STARTTLS);
        props.put("mail.smtp.debug", DEBUG);
        props.put("mail.smtp.socketFactory.port", PORT);
        props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        try {

            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);

            MimeMessage message = new MimeMessage(session);
            message.setText(TEXT);
            message.setSubject(SUBJECT);
            message.setFrom(new InternetAddress(FROM));
            message.addRecipient(RecipientType.TO, new InternetAddress(TO));
            message.saveChanges();

            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, USER, PASSWORD);

            transport.sendMessage(message, message.getAllRecipients());

            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这就是我在其他活动中称这个课程的方式:

(new MailSending()).send("e-mail receiver");

每次程序进入邮件发送类时,它都会写入写入的行:message.saveChanges(); 在我的AndroidMainfest文件中,我只获得了互联网许可。 也许我的AndroidMainfest文件有权限问题?还有什么可能是问题?

1 个答案:

答案 0 :(得分:0)

除了互联网许可之外你好, 为什么不尝试添加以下权限?

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我希望它有效..