本文介绍了不能使用Java邮件API从Android应用程序发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java邮件API从我的Andr​​oid应用程序发送电子邮件,
当我在java项目运行code将其发送邮件成功,但是当我用这个code到Android应用程序,logcat的显示错误,如下所示: -

15 09-03:18:57.549:ERROR / AndroidRuntime(277):致命异常:主要

15 09-03:18:57.549:ERROR / AndroidRuntime(277):了java.lang.RuntimeException:无法启动活动ComponentInfo {} email3.app/email3.app.email3:了java.lang.RuntimeException:使用javax。 mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com端口:465;

15 09-03:18:57.549:ERROR / AndroidRuntime(277):java.net.SocketException异常:权限被拒绝。

在code是如下: -

文件1: - email3.java

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Toast; 公共类EMAIL3延伸活动{
 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    发送器S =新寄件人();    }
}文件2: - sender.java进口java.util.Properties;
导入javax.mail *。
进口javax.mail.internet *。公共类发件人{
 公众发送者(){ 字符串[email protected]; //随之改变  //获取会话对象
  属性道具=新特性();
  props.put(mail.smtp.host,smtp.gmail.com);
  props.put(mail.smtp.socketFactory.port,465);
  props.put(mail.smtp.socketFactory.javax.net.ssl​​.SSLSocketFactory);
  props.put(mail.smtp.auth,真);
  props.put(mail.smtp.port,465);会话的会话=作为Session.getDefaultInstance(道具,
新javax.mail.Authenticator(){
受保护的PasswordAut​​hentication的getPasswordAut​​hentication(){
返回新的PasswordAut​​hentication([email protected],YYY); //发生相应的变化
}
});//撰写消息
尝试{
的MimeMessage消息=新的MimeMessage(会话);
message.setFrom(新网际地址([email protected])); //发生相应的变化
message.addRecipient(Message.RecipientType.TO,新的网际地址(至));
message.setSubject(你好);
message.setText(测试......);//发信息
Transport.send(消息);
}赶上(MessagingException五){抛出新的RuntimeException(E);}}
}
文件: - 3 Menifest文件<应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
    <活动机器人:EMAIL3NAME =
              机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
<使用许可权的android:NAME =android.permission.INTERNET对/>
< /用途>


解决方案

您必须使用asyntask随着网络的

i am trying to send an email from my android app using java mail api,when i run the code in java project it sends mail successfully but when i use this code into android app, logcat shows error as follows :-

09-03 15:18:57.549: ERROR/AndroidRuntime(277): FATAL EXCEPTION: main

09-03 15:18:57.549: ERROR/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{email3.app/email3.app.email3}: java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;

09-03 15:18:57.549: ERROR/AndroidRuntime(277): java.net.SocketException: Permission denied

the code is as follows :-

file1 :- email3.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

 public class email3 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sender s=new sender();

    }
}





file 2 :- sender.java

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class sender {
 public sender() {

 String to="[email protected]";//change accordingly

  //Get the session object
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","yyy");//change accordingly
}
});

//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");

//send message
Transport.send(message);


} catch (MessagingException e) {throw new RuntimeException(e);}

}
}




file :- 3  Menifest file

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".email3"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<uses-permission android:name="android.permission.INTERNET" />
</application>
解决方案

you must use asyntask to work with network http://mobiledevtuts.com/android/android-sdk-smtp-email-tutorial/

这篇关于不能使用Java邮件API从Android应用程序发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:00