JavaMail を使用して電子メールを送信する Android アプリを開発しようとしています。以下のコードをコンソール アプリケーションとして試してみましたが、動作しますが、エミュレータから Android アプリとして使用すると、メッセージなしで例外がスローされます。manifest.xml を変更して put を入れましたが、それでも機能しません。例外は message.setText("Welcome to JavaMail"); でスローされます。だから私を助けてください!
Sun の mail.jar と activation.jar を使用しています。
以下は、ClickHandler の完全なコードです。
public void btnSendClickHandler(View view)
{
try{
String host = "smtp.gmail.com";
String from = "username@gmail.com";
String pass = "password";
Properties props = System.getProperties();
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");
String[] to = {"toEmailAddress@gmail.com"}; // added this line
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++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i=0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");//The exception is thrown here
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch(Exception e){Toast.makeText(this, e.toString(),
Toast.LENGTH_LONG).show();}
}