我正在尝试开发一个使用JavaMail发送电子邮件的Android应用程序。我已经尝试了下面的代码作为控制台应用程序,它的工作原理,但当我从模拟器中使用作为Android应用程序时,它抛出异常,没有消息。我修改了manifest.xml并将其放置,但它仍然无效。在message.setText(“欢迎使用JavaMail”)中抛出异常;所以请帮帮我!
我正在使用Sun的mail.jar和activation.jar。
Bellow是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();}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
我不熟悉JavaMail jar,但是如果它引用的是不属于Android的类,它将无法工作。我发现一些网站正在为Android创建JavaMail端口。
http://code.google.com/p/javamail-android/
http://groups.google.com/group/android-developers/browse_thread/thread/9c7bca0a1b6957a9
请注意,第二个链接的库不可免费使用,您应该在开始实施前联系开发人员。