来自https://www.simplifiedcoding.net/android-email-app-using-javamail-api-in-android-studio/的代码:
public class MainActivity extends AppCompatActivity {
private String editTextEmail;
private String editTextSubject;
private String editTextMessage;
private Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void po(View view){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("my email", "passs");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("my eamail"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress("to email"));
//Adding subject
mm.setSubject("Heloo ");
//Adding message
mm.setText("jabour");
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
}
} 并为Genymotion运行运行应用程序的错误 enter image description here
我已经使用过Debug' app'这是一个错误
E / AndroidRuntime:FATAL EXCEPTION:main进程:first.zerammar.com.fha,PID:2090 java.lang.IllegalStateException:无法在父进程或祖先中找到方法op(View)for android:onClick属性已定义在视图类android.support.v7.widget.AppCompatButton上,带有id'按钮'在Android.sview.View.performClick的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) (View.java:4438)在android.view.Handler.dispatchMessage(Handler。)的android.view.View $ PerformClick.run(View.java:18422)android.os.Handler.handleCallback(Handler.java:733)。 java:95)在android.app.Looper.loop(Looper.java:136)的android.app.ActivityThread.main(ActivityThread.java:5001)java.lang.reflect.Method.invokeNative(Native Method)at java位于com.android.internal.os.ZygoteInit.main(ZygoteInit)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)中的.lang.reflect.Method.invoke(Method.java:515) .dava:601)at dalvik.system.NativeStart.main(Native Method)