我正在使用 JavaMail 发送我的错误报告......一切正常,但 Gmail 说,一些奇怪的应用程序(我的应用程序)试图发送邮件......然后我必须单击一个链接并再次启动应用程序,之后这个应用程序被允许发送邮件......
如果我将我的应用程序发送给其他用户会发生什么?他们的发送尝试会全部失败吗?市场应用程序和普通应用程序之间有区别吗?在已签名的应用程序和未签名的应用程序之间?
我正在使用 JavaMail 发送我的错误报告......一切正常,但 Gmail 说,一些奇怪的应用程序(我的应用程序)试图发送邮件......然后我必须单击一个链接并再次启动应用程序,之后这个应用程序被允许发送邮件......
如果我将我的应用程序发送给其他用户会发生什么?他们的发送尝试会全部失败吗?市场应用程序和普通应用程序之间有区别吗?在已签名的应用程序和未签名的应用程序之间?
试试这个代码。
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);
}
}
});
}
}
有关更多信息,请参见下面的链接。
然后试试这个:
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();
}