Android的JavaEmail出错

garfieldo 发布于 2014/05/12 15:36
阅读 155
收藏 0

在学javamail,看网上的代码,测试了一下,为什么会出错呢??代码看起来没错啊

Log

05-12 07:20:26.775: E/AndroidRuntime(281): FATAL EXCEPTION: main
05-12 07:20:26.775: E/AndroidRuntime(281): java.lang.VerifyError: com.example.testemail.SendEmail
05-12 07:20:26.775: E/AndroidRuntime(281): at com.example.testemail.MainActivity.onCreate(MainActivity.java:16)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
05-12 07:20:26.775: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 07:20:26.775: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 07:20:26.775: E/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
05-12 07:20:26.775: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 07:20:26.775: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 07:20:26.775: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)


代码:

一、MainAcvitity:

package com.example.testemail;


import java.io.UnsupportedEncodingException;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
SendEmail.sendEmail("这里就不发了哈", "hello", "this is from android");
//SendEmail.main(null);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


二、SendEmail:

package com.example.testemail;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {
static int port = 25;
static String serverString = "smtp.sina.com";
static String fromString = "Hacker";
static String userString = "这里就不发了哈";
static String passwordString = "这里就不发了哈";
public static void sendEmail(String email, String subject, String body)throws UnsupportedEncodingException{
try{
Properties props = new Properties();
props.put("mail.smtp.host", serverString);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
Transport transport = null;
Session session = Session.getDefaultInstance(props, null);
            transport = session.getTransport("smtp");
            transport.connect(serverString, userString, passwordString);
            MimeMessage msg = new MimeMessage(session);
            msg.setSentDate(new Date());
            InternetAddress fromAddress = new InternetAddress(userString,fromString,"UTF-8");
            msg.setFrom(fromAddress);
            InternetAddress[] toAddress = new InternetAddress[1];
            toAddress[0] = new InternetAddress(email);
            msg.setRecipients(Message.RecipientType.TO, toAddress);
            msg.setSubject(subject, "UTF-8");   
            msg.setText(body, "UTF-8");
            msg.saveChanges();
            transport.sendMessage(msg, msg.getAllRecipients());
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    /*public static void main(String args[]) throws UnsupportedEncodingException
    {


        sendEmail("这里就不发了哈","邮件测试","hello");//收件人
        System.out.println("ok");
    }*/
}

以下是问题补充:

@garfieldo:出错那里就是SendEmail,不过看网上的评论别人测试都成功了啊 (2014/05/12 15:37)
@garfieldo:用main测试(就是注释掉那里)也是一样的问题 (2014/05/12 15:38)
加载中
0
amonxu
amonxu

检查email配置。

g
garfieldo
不过写成普通的java程序就没问题啊……(当然这个里面用sina邮箱要验证就不行。。我写成普通java程序以后换了学校的邮箱就能用),可是android还是一样报错。
返回顶部
顶部