2012-12-07 52 views
0

我使用JavaMail發送我的錯誤報告...一切正常,但Gmail說,一些奇怪的應用程序(我的應用程序)試圖發送郵件.. 。然後,我必須點擊一個鏈接並重新啓動應用程序,之後這個應用程序被允許發送郵件...JavaMail和Gmail - 永久允許我的應用程序發送郵件

如果我將我的應用程序發送給其他用戶會發生什麼?他們的發送嘗試都會失敗嗎?市場應用程序和普通應用程序有區別嗎?在已簽名的應用和未簽名的應用之間?

+0

您是否使用設備上配置的相同帳戶? – ThePCWizard

+0

不,我爲我的應用程序創建了一個gmail帳戶,並使用它來發送報告......實際上,直到現在,我只是閱讀了許多聲明,無法從設備所有者郵件發送郵件,只要他曾經允許它通過提供他的憑據...不會介意使用設備的郵件帳戶,如果這可能很容易.. – prom85

回答

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

我沒有這種形式的問題..也許這會幫助你:) – Rodolfo

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("[email protected]", "dipakkeshariya"); 
        } 
       }); 

       try { 
        Message message = new MimeMessage(session); 
        message.setFrom(new InternetAddress("[email protected]")); 
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
        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)

相關問題