gpt4 book ai didi

java - Android发送邮件SocketTimeoutException

转载 作者:行者123 更新时间:2023-12-01 13:44:37 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,我想在用户成功注册后向用户发送电子邮件。但我无法发送电子邮件。单击“提交”并作为成功登录的用户重定向到主页 Activity 后,我等待大约 10 秒,然后 logcat 中出现一条黄色错误消息,指出邮件尚未发送。

这是我触发发送电子邮件的地方:

public class Signup extends Activity implements OnUserCreatedListener {
@Override
public void onUserCreated(Integer userId) {

editor = sharedPrefs.edit();
editor.putBoolean("userLoggedInState", true);
editor.putInt("currentLoggedInUserId", userId);
editor.commit();

Intent signupSuccessHome = new Intent(getApplicationContext(), Home.class);
signupSuccessHome.putExtra("reqFrom", "signup");
startActivity(signupSuccessHome);

try {
new EmailSender(userEmail, Configurationz.Emailz.OFFICIAL_ADDRESS, Configurationz.Emailz.SUCCESSFUL_SIGNUP_SUBJECT, Configurationz.Emailz.SUCCESSFUL_SIGNUP_BODY(userName), null).execute();
} catch (Exception e) {
}
finish();
}

这是 EmailSender 实用程序类

public class EmailSender extends AsyncTask<String, Void, Boolean> {
String to;
String from;
String subject;
String message;
String[] attachments;
Mail mail = new net.asdasd.utilities.Mail();

public EmailSender(String to, String from, String subject, String message, String[] attachments) {
super();
this.to = to;
this.from = from;
this.subject = subject;
this.message = message;
this.attachments = attachments;
}

@Override
protected Boolean doInBackground(String... params) {

if (subject != null && subject.length() > 0) {
mail.setSubject(subject);
} else {
mail.setSubject("Subject");
}

if (message != null && message.length() > 0) {
mail.setBody(message);
} else {
mail.setBody("Message");
}
mail.setTo(to);
mail.setFrom(from);
try {
return mail.send();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

这是邮件类:

package net.asdasd.utilities;

import java.util.Date;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import net.shiftinpower.configuration.Configurationz;

public class Mail extends javax.mail.Authenticator {
public String user;
public String password;
private String to;
private String from;
private String port;
private String sport;
private String host;
private String subject;
private String body;
private boolean _auth;
private boolean _debuggable;
private Multipart multipart;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public Multipart getMultipart() {
return multipart;
}

public void setMultipart(Multipart multipart) {
this.multipart = multipart;
}
public Mail() {

host = "smtp.googlemail.com"; // default smtp server
port = "465"; // default smtp port
sport = "465"; // default socketfactory port
user = Configurationz.Emailz.ASDASD_OFFICIAL_ADDRESS;
password = Configurationz.Emailz.ASDASD_OFFICIAL_PASSWORD;
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on

multipart = new MimeMultipart();
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}

public boolean send() throws Exception {
Properties props = _setProperties();

if (!user.equals("") && !password.equals("") && to.length() > 0 && !from.equals("") && !subject.equals("") && !body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length()];
for (int i = 0; i < to.length(); i++) {
addressTo[i] = new InternetAddress(to);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setSentDate(new Date());
// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(multipart);
// send email
Transport.send(msg);
return true;
} else {
return false;
}
}

private Properties _setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", host);

if (_debuggable) {
props.put("mail.debug", "true");
}

if (_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
return props;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
// the getters and setters
public String getBody() {
return body;
}

public void setBody(String _body) {
this.body = _body;
}
}

这是错误消息:

 javax.mail.MessagingException: Could not connect to SMTP host: smtp.googlemail.com, port: 465;
nested exception is:
java.net.SocketTimeoutException: Connection timed out
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at net.asdasd.utilities.Mail.send(Mail.java:120)
at net.asdasd.utilities.EmailSender.doInBackground(EmailSender.java:38)
at net.asdasd.utilities.EmailSender.doInBackground(EmailSender.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.net.SocketTimeoutException: Connection timed out
at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
at java.net.Socket.connect(Socket.java:1002)
at java.net.Socket.connect(Socket.java:945)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 15 more

这是该类的先前版本(并且它有效)

public class EmailSender {
public boolean sendEmail(String to, String from, String subject, String message, String[] attachements) throws Exception {
Mail mail = new net.shiftinpower.utilities.Mail();
if (subject != null && subject.length() > 0) {
mail.setSubject(subject);
} else {
mail.setSubject("Subject");
}

if (message != null && message.length() > 0) {
mail.setBody(message);
} else {
mail.setBody("Message");
}
mail.setTo(to);
mail.setFrom(from);
return mail.send();

}
}

我也以不同的方式调用它:

在Signup.java中:

private class sendVerificationEmail extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
try {
emailSender.sendEmail(userEmail, Configurationz.Emailz.ASDASD_OFFICIAL_ADDRESS, Configurationz.Emailz.SUCCESSFUL_SIGNUP_SUBJECT, Configurationz.Emailz.SUCCESSFUL_SIGNUP_BODY(userName), null);
} catch (Exception e) {
}
return null;
}
}
class CreateUser extends AsyncTask<String, String, String> {
//blah blah
@Override
protected void onPostExecute(String result) {
ShowLoadingMessage.dismissDialog();



try {
new sendVerificationEmail().execute();
} catch (Exception e) {
}

super.onPostExecute(result);
}

最佳答案

我认为 smtp.googlemail.com 不存在;请尝试 smtp.gmail.com

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

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