JavaMail和电子邮件帐户的连接

时间:2014-02-01 13:29:11

标签: java android email javamail

我尝试使用javamail连接到电子邮件 - 在非Android应用程序中,此代码工作正常:

public void perform(View v){
    Toast.makeText(getApplicationContext(), "Validating...",
               Toast.LENGTH_SHORT).show();



    new LoginIn().execute();


}
 private class LoginIn extends AsyncTask<Void, Void, Void> {


        @Override
        protected Void doInBackground(Void... arg0) {
            Properties props = new Properties();
             props.put("mail.smtp.host", "smtp.gmail.com");
             props.put("mail.stmp.user", "sample@gmail.com");          
             //If you want you use TLS 
             props.put("mail.smtp.auth", "true");
               props.put("mail.smtp.starttls.enable", "true");
             props.put("mail.smtp.password", "password of the sender");
             // If you want to use SSL
             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");

             props.put("mail.smtp.starttls.enable", "true");

             Session session = Session.getDefaultInstance(props);

             Transport trans;
             try {
                 trans = session.getTransport("smtp");
                 trans.connect("smtp.live.com", 25, "sample@gmail.com", "asdasd");
             } catch (NoSuchProviderException e) {
                 e.printStackTrace();
             }catch (MessagingException e) {
                 e.printStackTrace();
             }
             return null;
        }

 }

logcat的:

02-01 13:17:22.863: W/dalvikvm(3436): VFY: unable to resolve static method 4286: 

Ljavax/mail/Session;.getDefaultInstance (Ljava/util/Properties;)Ljavax/mail/Session;
02-01 13:17:22.863: D/dalvikvm(3436): VFY: replacing opcode 0x71 at 0x004b
02-01 13:17:22.893: W/dalvikvm(3436): threadid=13: thread exiting with uncaught exception (group=0x40a71930)
02-01 13:17:23.003: I/Choreographer(3436): Skipped 76 frames!  The application may be doing too much work on its main thread.
02-01 13:17:23.213: D/dalvikvm(3436): GC_CONCURRENT freed 150K, 11% free 2612K/2908K, paused 75ms+127ms, total 307ms
02-01 13:17:23.466: E/AndroidRuntime(3436): FATAL EXCEPTION: AsyncTask #3
02-01 13:17:23.466: E/AndroidRuntime(3436): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 13:17:23.466: E/AndroidRuntime(3436):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.lang.Thread.run(Thread.java:856)
02-01 13:17:23.466: E/AndroidRuntime(3436): Caused by: java.lang.NoClassDefFoundError: javax.mail.Session
02-01 13:17:23.466: E/AndroidRuntime(3436):     at com.example.droidmsg.MainActivity$LoginIn.doInBackground(MainActivity.java:64)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at com.example.droidmsg.MainActivity$LoginIn.doInBackground(MainActivity.java:1)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-01 13:17:23.466: E/AndroidRuntime(3436):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-01 13:17:23.466: E/AndroidRuntime(3436):     ... 4 more
02-01 13:17:24.123: I/Choreographer(3436): Skipped 34 frames!  The application may be doing too much work on its main thread.
02-01 13:17:26.243: I/Choreographer(3436): Skipped 160 frames!  The application may be doing too much work on its main thread.

我已经读过我应该在asynctask中这样做 - 所以我做了,但它没有帮助。有人能帮我吗 ?应用程序告诉我“他的应用程序可能在其主线程上做了太多工作”但我尝试在其他线程中执行此操作?

2 个答案:

答案 0 :(得分:1)

我在java项目中使用过mail.smtp。 以下是我的代码,可以帮助您。

    package com.agileinfotech.bsviewer.service;

    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    public class MailUtil {
        public final String username = "abcxyz@gmail.com";
        public final String password = "******";

        public void mailSuccAtt(String sch_Id, String jb_status, String filename,String documentName, String mail) {

            System.out.println("in the mail util tname of list is  " + mail);

            try {
                if (jb_status.equals("Completed")) {
                    Properties pros = new Properties();
                    pros.put("mail.smtp.auth", "true");
                    pros.put("mail.smtp.starttls.enable", "true");
                    pros.put("mail.smtp.host", "smtp.gmail.com");
                    pros.put("mail.smtp.port", "587");

                    Session session = Session.getInstance(pros,
                            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication(
                                            username, password);
                                }
                            });

                    MimeMessage msg = new MimeMessage(session);
                    msg.setFrom(new InternetAddress(username));
                    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
                            mail));
                    msg.setSubject("Attached Demo");

                    BodyPart messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setText("Hello " + "\n\n Status of " + sch_Id
                            + " is " + jb_status);

                    Multipart multipart = new MimeMultipart();
                    multipart.addBodyPart(messageBodyPart);

                    messageBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(filename);
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    messageBodyPart.setFileName(documentName);
                    multipart.addBodyPart(messageBodyPart);

                    msg.setContent(multipart);

                    Transport.send(msg);

                    //System.out.println("Mail Sent");

                }
            } catch (Exception e) {
    e.printStackTrace();
            }
        }

    }

答案 1 :(得分:0)

您的类路径是否已正确设置?

Caused by: java.lang.NoClassDefFoundError: javax.mail.Session