【问题标题】:Sending an e-mail without using the built-in app不使用内置应用程序发送电子邮件
【发布时间】:2015-11-05 05:53:14
【问题描述】:

我需要在没有用户输入的情况下自动发送带有附件的电子邮件。我以前读过这两个答案。

Sending Email in Android using JavaMail API without using the default/built-in app

Android: Send e-mail with attachment automatically in the background.

但现在我没有收到任何电子邮件,而且我已经在 gmail 中启用了不太安全的应用程序。这是我的代码:

public class Mail {
private String mailhost;
private String user;
private String password;
private Session session;
private String serverport;

public void main(String user, String password, String subject, String body){


    sendFromGMail(user, password, subject,body);
}

private void sendFromGMail(String from, String pass, String subject, String body){
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getInstance(props);
    MimeMessage message = new MimeMessage(session);
    try {
        InternetAddress adressTo = new InternetAddress(from);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, adressTo);

        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch(MessagingException e){
        Log.e("Not Sent", e.getMessage(), e);
    }
}

}

这就是我使用它的方式:

 try {
            Mail mail = new Mail();
            mail.main(gmailusername, gmailpassword, "Test", "Test2");
            this.publishProgress("Email Sent");
        }catch(Exception e){
            Log.e("SendMail", e.getMessage(), e);
            this.publishProgress("Email not sent");
        }

编辑:我不想像标题所建议的那样使用内置应用程序。只想说清楚。

Java 调试:

08-12 15:08:41.152  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: setDebug: JavaMail version 1.4.1
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: useEhlo true, useAuth true
08-12 15:08:41.162  24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

【问题讨论】:

  • 那些错误是......?
  • @Nouman Ghaffar 你读过我的问题吗?我已经读过这个,这不是我要找的。不完全。
  • @ChristopheRoberge:我们可以使用 javaMail API 来接收电子邮件吗?我正在制作一个应用程序。阻止电子邮件。你认为 JavaMail 有可能吗?

标签: android email gmail jakarta-mail stack-overflow


【解决方案1】:

修复所有这些common mistakes,包括摆脱 Authenticator。

在你的构造函数中去掉对 super() 的调用,你不需要它。

此外,摆脱 ByteArrayDataSource 类。你不需要你自己的,因为它comes with JavaMail。另外,您的程序甚至没有使用它。

如果仍然无法正常工作,请使用您的新代码和新的失败详细信息更新您的帖子。

【讨论】:

  • 为我的新实际代码更改了它。摆脱了另一个。这是我在没有附件的情况下使用的,只是为了让它工作。现在,它没有崩溃或抛出任何错误,但我仍然没有收到任何电子邮件,并且我在 gmail 中启用了不太安全的应用程序。
  • JavaMail debug output 显示什么?
  • 您使用的是 JavaMail 的旧版本。 Upgrade to 1.5.4.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2021-06-08
相关资源
最近更新 更多