gpt4 book ai didi

java - 电子邮件发送Android Studio

转载 作者:行者123 更新时间:2023-12-03 18:07:01 28 4
gpt4 key购买 nike

我正在尝试向我的Android应用添加发送电子邮件功能。我有一个应用程序可以发送以Eclipse编写的电子邮件,但我想将我的应用程序移至Android Studio。我的代码与Eclipse中的代码相同,但是当我想发送邮件时,我的应用程序停止工作。我为此使用mail.jar,activation.jar和Additional.jar。

这是我的代码:

private void sendMail(String email, String subject, String messageBody)
{
Session session = createSessionObject();
try {
Message message = createMessage(email, subject, messageBody, session);
new SendMailTask().execute(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, senderName));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
message.setSubject(subject);
message.setText(messageBody);
return message;
}

private Session createSessionObject() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");

return Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}

private class SendMailTask extends AsyncTask<Message, Void, Void> {
private ProgressDialog progressDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Sending mail", true, false);
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}

@Override
protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}


我认为我收到此错误,是因为Android Studio无法看到activation.jar和Additional.jar文件。到达此行时,我的应用程序停止工作:

Message message = new MimeMessage(session);


如何将这些jar文件正确添加到我的Android Studio项目中?

编辑:

我通过向build.gradle添加行来解决此问题:

compile files('libs/mail.jar')
compile files('libs/additionnal.jar')
compile files('libs/activation.jar')


但是现在当我单击“发送电子邮件”按钮时,在日志猫中出现错误:

08-13 18:39:47.497 19034-20338/krzosik.mailsenderapp W/System.err: javax.mail.MessagingException: Could not convert socket to TLS;
08-13 18:39:47.510 19034-20338/krzosik.mailsenderapp W/System.err: nested exception is:
08-13 18:39:47.511 19034-20338/krzosik.mailsenderapp W/System.err: javax.net.ssl.SSLHandshakeException: Connection closed by peer
08-13 18:39:47.512 19034-20338/krzosik.mailsenderapp W/System.err: at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1339)
08-13 18:39:47.513 19034-20338/krzosik.mailsenderapp W/System.err: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:420)
08-13 18:39:47.513 19034-20338/krzosik.mailsenderapp W/System.err: at javax.mail.Service.connect(Service.java:310)
08-13 18:39:47.513 19034-20338/krzosik.mailsenderapp W/System.err: at javax.mail.Service.connect(Service.java:169)
08-13 18:39:47.513 19034-20338/krzosik.mailsenderapp W/System.err: at javax.mail.Service.connect(Service.java:118)
08-13 18:39:47.514 19034-20338/krzosik.mailsenderapp W/System.err: at javax.mail.Transport.send0(Transport.java:188)
08-13 18:39:47.514 19034-20338/krzosik.mailsenderapp W/System.err: at javax.mail.Transport.send(Transport.java:118)
08-13 18:39:47.514 19034-20338/krzosik.mailsenderapp W/System.err: at krzosik.mailsenderapp.MainActivity$SendMailTask.doInBackground(MainActivity.java:100)
08-13 18:39:47.514 19034-20338/krzosik.mailsenderapp W/System.err: at krzosik.mailsenderapp.MainActivity$SendMailTask.doInBackground(MainActivity.java:82)
08-13 18:39:47.515 19034-20338/krzosik.mailsenderapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-13 18:39:47.515 19034-20338/krzosik.mailsenderapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-13 18:39:47.515 19034-20338/krzosik.mailsenderapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-13 18:39:47.515 19034-20338/krzosik.mailsenderapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-13 18:39:47.516 19034-20338/krzosik.mailsenderapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-13 18:39:47.516 19034-20338/krzosik.mailsenderapp W/System.err: at java.lang.Thread.run(Thread.java:818)
08-13 18:39:47.516 19034-20338/krzosik.mailsenderapp W/System.err: Caused by: javax.net.ssl.SSLHandshakeException: Connection closed by peer
08-13 18:39:48.009 19034-19041/krzosik.mailsenderapp W/art: Suspending all threads took: 199.811ms
08-13 18:39:48.032 19034-20338/krzosik.mailsenderapp W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
08-13 18:39:48.032 19034-20338/krzosik.mailsenderapp W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
08-13 18:39:48.032 19034-20338/krzosik.mailsenderapp W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:623)
08-13 18:39:48.033 19034-20338/krzosik.mailsenderapp W/System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:585)
08-13 18:39:48.033 19034-20338/krzosik.mailsenderapp W/System.err: at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
08-13 18:39:48.033 19034-20338/krzosik.mailsenderapp W/System.err: at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1336)
08-13 18:39:48.034 19034-20338/krzosik.mailsenderapp W/System.err: ... 14 more


但是,当我将我的应用程序放到平板电脑上时,一切正常,邮件已发送。如何使用Android Studio在模拟器中测试此功能,为什么会出现此错误?在Eclipse中,一切仍然正常。

这是我的SendMailTask​​代码:

private class SendMailTask extends AsyncTask<Message, Void, Void> {
private ProgressDialog progressDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Sending mail", true, false);
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}

@Override
protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}


错误是由线引起的

Transport.send(messages[0]);

最佳答案

1)确保您的android设备时间设置为当前日期和时间。

2)确保您的防病毒程序没有干扰,并确保将排除项添加到防火墙中。

3)试试这个:

properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

关于java - 电子邮件发送Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38935090/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com