您好,我正在尝试创建一个不需要用户交互即可发送电子邮件的Android应用程序(将通知用户发送的任何电子邮件)。我正在使用此javamail:http://code.google.com/p/javamail-android/downloads/list并遵循此示例:http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android
但我收到以下错误:
10-07 09:23:10.373: E/MailApp(9173): Could not send email
10-07 09:23:10.373: E/MailApp(9173): javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
10-07 09:23:10.373: E/MailApp(9173): nested exception is:
10-07 09:23:10.373: E/MailApp(9173): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 25): connect failed: ECONNREFUSED (Connection refused)
10-07 09:23:10.373: E/MailApp(9173): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
10-07 09:23:10.373: E/MailApp(9173): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
发送电子邮件的代码:
Thread emailSendThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
}
catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (sendEmail == true) {
try {
MailSender m = new MailSender("firstname.surname@gmail.com", "password");
String[] toArr = {"example@email.com"};
m.setTo(toArr);
m.setFrom("firstname.surname@gmail.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");
try {
m.send();
if(m.send()) {
Toast.makeText(MainActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
catch (Exception e) {
Log.e("email", "Error", e);
}
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendEmail = false;
}
}
}
});
我已将此电子邮件用户名和密码替换为此代码中的示例。
发送电子邮件我执行sendEmail = true
;
答案 0 :(得分:1)
您只能使用SMTP服务器发送邮件。如果您的SMTP服务器不在运行该应用程序的设备上,则需要提供可以发送电子邮件的SMTP地址。实际上,如果SMTP详细信息不正确,您的应用程序将无法成功发送电子邮件。