gpt4 book ai didi

java - Android 中使用 Javamail 自动发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:37 26 4
gpt4 key购买 nike

我正在尝试自动发送电子邮件,无需用户在我的 Android 应用程序中进行确认。我正在尝试使用 Javamail,但是当我在手机上尝试我的应用程序时,它与典型的“应用程序已停止,再次打开”Android 消息很接近。我的代码是:

发送邮件类别:

package com.android.mdw.demo;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.util.Log;

public class EnviarEmail {
public static String emailEnvio = "XXX@gmail.com";
public static String passwordEmailEnvio = "@XXX";

public void EnviarEmail(String Mensaje, String Asunto, String email) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailEnvio, passwordEmailEnvio);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailEnvio));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email));
message.setSubject(Asunto);
message.setText(Mensaje);

Transport.send(message);

} catch (MessagingException e) {
Log.w("ERROR", "No se ha podido enviar el email de confirmación.");
}
}
}

通知邮件类别:

package com.android.mdw.demo;

public class EmailAviso {
public void EmailAviso () {
String Mensaje = "CUERPO";
String Asunto = "ASUNTO";
String email = "AQUIENVA@gmail.com";

EnviarEmail EnviarEmail = new EnviarEmail();
EnviarEmail.EnviarEmail(Mensaje, Asunto, email);

}
}

主类摘录:

boolean PRUEBA=true;
boolean PRUEBA1=true;

if(PRUEBA==true && PRUEBA1==true) {
EmailAviso EmailAviso = new EmailAviso();
EmailAviso.EmailAviso();
}

我在 AS 中收到的日志是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.mdw.demo, PID: 18001
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getLocalHost(InetAddress.java:816)
at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
at javax.mail.Transport.send(Transport.java:117)
at com.android.mdw.demo.EnviarEmail.EnviarEmail(EnviarEmail.java:41)
at com.android.mdw.demo.EmailAviso.EmailAviso(EmailAviso.java:10)
at com.android.mdw.demo.Main.onSensorChanged(Main.java:77)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:699)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

最佳答案

您正在尝试从不允许的主线程发送电子邮件,请尝试创建单独的工作线程来发送电子邮件。尝试如下所示:

    new Thread(new Runnable() {
@Override
public void run() {
if(PRUEBA==true && PRUEBA1==true) {
EmailAviso EmailAviso = new EmailAviso();
EmailAviso.EmailAviso();
}
}
}).start();

关于java - Android 中使用 Javamail 自动发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55794199/

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