【问题标题】:JavaMail and Gmail - Permanent allow my app to send mailsJavaMail 和 Gmail - 永久允许我的应用发送邮件
【发布时间】:2012-11-25 12:46:39
【问题描述】:

我正在使用 JavaMail 发送我的错误报告...一切正常,但 Gmail 说,一些奇怪的应用程序(我的应用程序)试图发送邮件...然后我必须单击一个链接并启动再次应用,之后允许此应用发送邮件...

如果我将我的应用发送给其他用户会怎样?他们的发送尝试会全部失败吗?市场应用程序和普通应用程序之间有区别吗?在已签名的应用程序和未签名的应用程序之间?

【问题讨论】:

  • 您使用的是设备上配置的同一个帐户吗??
  • 不,我已经为我的应用程序创建了一个 gmail 帐户并使用它来发送报告...实际上,直到现在,我刚刚阅读了许多声明,无法从设备所有者那里发送邮件邮件,只有当他曾经通过提供他的凭据来允许它时...不介意使用设备邮件帐户,如果这可能如此简单..

标签: android security permissions gmail jakarta-mail


【解决方案1】:

然后试试这个:

public void envioEmail(final String from, final String mailhost, final String user, final String password, final Boolean auth, final String destinatario, final String assunto, final String mensagem) throws MessagingException, IOException {
    new Thread(){
        @Override
        public void run() {
            Properties props = System.getProperties();
            if (mailhost != null) props.put("mail.smtp.host", mailhost);
            if (auth) props.put("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.starttls.enable", "true");

            Authenticator authh = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, password);
                }
            };
            javax.mail.Session session = javax.mail.Session.getInstance(props, authh);
            javax.mail.Message msg = new MimeMessage(session);
            try {
                msg.setFrom(new InternetAddress(from));
                msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(destinatario));
                msg.setSubject(assunto);
                msg.setSentDate(new Date());
                msg.setText(mensagem);

                SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp");
                try {
                    if (auth){
                        t.connect(mailhost, user, password);
                        t.sendMessage(msg, msg.getAllRecipients());
                    }else{
                        t.connect();
                        t.sendMessage(msg, msg.getAllRecipients());
                    }
                } finally {
                    t.close();
                }
                flag = true;
                atualizaTelaConexao("E-Mail enviado com sucesso!", ctx);
            } catch (MessagingException e) {
                flag = false;
                atualizaTelaConexao("Erro ao enviar E-Mail! Verifique as configuracoes de e-mail", ctx);
            }
        }
    }.start();
}

【讨论】:

  • 我没有这个表格的问题。也许这对你有帮助:)
【解决方案2】:

试试这个代码。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button login = (Button) findViewById(R.id.mBtnSubmit);
        login.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                Properties props = new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.port", "465");

                Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("dipakkeshariya@android.com", "dipakkeshariya");
                    }
                });

                try {
                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("dipak.keshariya@android.com"));
                    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("dipak.keshariya@mobileappdeveloper.com"));
                    message.setSubject("Testing Subject");
                    message.setContent("Hi Dipak Keshariya (Android Developer)", "text/html; charset=utf-8");

                    Transport.send(message);

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

更多信息请参见下面的链接。

Android – Send Email Via GMail (Actually Via SMTP)

【讨论】:

    猜你喜欢
    • 2022-07-17
    • 2014-09-13
    • 2022-08-14
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    相关资源
    最近更新 更多