我想在eclipse中自动发送电子邮件作为用户按下按钮我尝试了在互联网上找到的所有可能的建议添加了jar文件,如javax.mail.jar,activation.jar和additional.jar,并尝试了所有可能的添加它们但总是产生错误的方法。 我做了很多努力,但没有结果,我真的很累。 我尝试了java工程上的代码并成功发送电子邮件,但是在android项目中它没有,这里是代码片段:
`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 Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
test.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("dev.montaser@gmail.com"));
message.setRecipients(MimeMessage.RecipientType.TO,
InternetAddress.parse("eng.montaser99@yahoo.com"));
message.setSubject("Test JCG Example");
message.setContent("Hello how are you","text/html; charset=utf-8");
Transport.send(message);
System.out.println("Mail sent succesfully!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
});
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
这是错误日志。 03-29 15:16:00.240:E / AndroidRuntime(1249):致命异常:主要 03-29 15:16:00.240:E / AndroidRuntime(1249):java.lang.NoClassDefFoundError:com.example.finalp.MainActivity $ 1 03-29 15:16:00.240:E / AndroidRuntime(1249):at com.example.finalp.MainActivity.onCreate(MainActivity.java:40) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.Activity.performCreate(Activity.java:5133) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.os.Handler.dispatchMessage(Handler.java:99) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.os.Looper.loop(Looper.java:137) 03-29 15:16:00.240:E / AndroidRuntime(1249):在android.app.ActivityThread.main(ActivityThread.java:5103) 03-29 15:16:00.240:E / AndroidRuntime(1249):at java.lang.reflect.Method.invokeNative(Native Method) 03-29 15:16:00.240:E / AndroidRuntime(1249):at java.lang.reflect.Method.invoke(Method.java:525) 03-29 15:16:00.240:E / AndroidRuntime(1249):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
`