JavaMail和Gmail - 永久允许我的应用程序发送邮件

时间:2012-12-07 10:38:23

标签: android security permissions gmail javamail

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

如果我将我的应用发送给其他用户,会发生什么?他们的发送尝试都会失败吗?市场应用和普通应用之间有区别吗?在签名的应用程序和未签名的应用程序之间?

2 个答案:

答案 0 :(得分:0)

然后试试这个:

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();
}

答案 1 :(得分:0)

试试这段代码。

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)