Android JavaMail和Gmail-永久性允许我的应用程序发送邮件
我正在使用JavaMail发送我的错误报告。。。一切正常,但Gmail说,某个奇怪的应用程序(我的应用程序)试图发送邮件。。。然后我必须点击一个链接并再次启动该应用程序,之后该应用程序可以发送邮件 如果我将我的应用程序发送给其他用户,会发生什么情况?他们的发送尝试都会失败吗?市场应用程序和普通应用程序之间有区别吗?在已签名的应用程序和未签名的应用程序之间?然后尝试以下操作:Android JavaMail和Gmail-永久性允许我的应用程序发送邮件,android,security,permissions,gmail,jakarta-mail,Android,Security,Permissions,Gmail,Jakarta Mail,我正在使用JavaMail发送我的错误报告。。。一切正常,但Gmail说,某个奇怪的应用程序(我的应用程序)试图发送邮件。。。然后我必须点击一个链接并再次启动该应用程序,之后该应用程序可以发送邮件 如果我将我的应用程序发送给其他用户,会发生什么情况?他们的发送尝试都会失败吗?市场应用程序和普通应用程序之间有区别吗?在已签名的应用程序和未签名的应用程序之间?然后尝试以下操作: public void envioEmail(final String from, final String mailho
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();
}
试试这个代码
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);
}
}
});
}
}
有关更多信息,请参阅下面的链接
您使用的是设备上配置的同一帐户吗?不,我已经为我的应用程序创建了一个gmail帐户,并使用它发送报告。。。事实上,直到现在,我才读到很多声明说,只有在设备所有者通过提供凭据允许发送邮件的情况下,才可能从设备所有者的邮件发送邮件。。。如果可能的话,我不介意使用设备邮件帐户。我没有这个表格的问题。。也许这对你有帮助:)