このコードには、2 つの単純なクラスがあります。メール送信者と、ボタンが 1 つしかないメインの Android アクティビティです。これはアクティビティ コードです。
package com.py.spycam;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.internet.AddressException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickMail(View v) {
String[] to = {"toaddress"};
SMTPMail mail = new SMTPMail("user", "pass");
try {
mail.send("sender", to, "prova", "prova bpdy");
Toast.makeText(this, "Mail sent", Toast.LENGTH_SHORT).show();
} catch (NoSuchProviderException e) {
Toast.makeText(this, "Unable to send email (NoSuchProviderException)", Toast.LENGTH_SHORT).show();
} catch (AddressException e) {
Toast.makeText(this, "Unable to send email (AddressException)", Toast.LENGTH_SHORT).show();
} catch (MessagingException e) {
Toast.makeText(this, "Unable to send email (MessagingException)", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Unable to send email (general Exception)", Toast.LENGTH_SHORT).show();
}
}
}
そして、これはメール送信者です:
package com.py.spycam;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SMTPMail {
private String host;
private String from;
private String pass;
private Properties props;
public SMTPMail(String _user, String _pass) {
host = "smtp.gmail.com";
from = _user;
pass = _pass;
props = System.getProperties();
/* Props settings */
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
}
public void send(String from, String[] to, String subject, String body)
throws AddressException, MessagingException, NoSuchProviderException {
//throws AddressException, MessagingException {
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for(int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
for(int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
どのステートメントにもキャッチされずに、一般的な例外が発生し続けcatch
ます(どうしてですか?どこからスローされますか?)。このコードの何が問題になっていますか? 注: このコードは、プレーンな Java で実行した場合に機能します (javac
コンパイルされ、シンプルなコードで実行されmain
ます)。
メガ編集: これは LogCat からの出力で、 System.err 警告に限定されています ( printStackTrace()
s.
01-17 01:41:41.550: W/System.err(23420): android.os.NetworkOnMainThreadException
01-17 01:41:41.550: W/System.err(23420): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-17 01:41:41.555: W/System.err(23420): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-17 01:41:41.555: W/System.err(23420): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-17 01:41:41.555: W/System.err(23420): at java.net.InetAddress.getByName(InetAddress.java:289)
01-17 01:41:41.555: W/System.err(23420): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
01-17 01:41:41.555: W/System.err(23420): at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
01-17 01:41:41.555: W/System.err(23420): at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
01-17 01:41:41.555: W/System.err(23420): at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
01-17 01:41:41.555: W/System.err(23420): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
01-17 01:41:41.555: W/System.err(23420): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
01-17 01:41:41.555: W/System.err(23420): at javax.mail.Service.connect(Service.java:288)
01-17 01:41:41.560: W/System.err(23420): at javax.mail.Service.connect(Service.java:169)
01-17 01:41:41.560: W/System.err(23420): at com.py.spycam.SMTPMail.send(SMTPMail.java:55)
01-17 01:41:41.560: W/System.err(23420): at com.py.spycam.MainActivity.onClickMail(MainActivity.java:29)
01-17 01:41:41.560: W/System.err(23420): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 01:41:41.560: W/System.err(23420): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 01:41:41.560: W/System.err(23420): at android.view.View$1.onClick(View.java:3586)
01-17 01:41:41.560: W/System.err(23420): at android.view.View.performClick(View.java:4084)
01-17 01:41:41.560: W/System.err(23420): at android.view.View$PerformClick.run(View.java:16966)
01-17 01:41:41.560: W/System.err(23420): at android.os.Handler.handleCallback(Handler.java:615)
01-17 01:41:41.560: W/System.err(23420): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 01:41:41.565: W/System.err(23420): at android.os.Looper.loop(Looper.java:137)
01-17 01:41:41.565: W/System.err(23420): at android.app.ActivityThread.main(ActivityThread.java:4931)
01-17 01:41:41.565: W/System.err(23420): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 01:41:41.565: W/System.err(23420): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 01:41:41.565: W/System.err(23420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-17 01:41:41.565: W/System.err(23420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-17 01:41:41.565: W/System.err(23420): at dalvik.system.NativeStart.main(Native Method)