簡體   English   中英

使用javamail發送郵件會引發錯誤

[英]Sending mail using javamail throws error

我有兩個活動,一個活動是使用Imap接收電子郵件,第二個活動是使用SMTP發送電子郵件。 發送電子郵件活動的按鈕位於“接收電子郵件”活動中,因此必須在“發送電子郵件活動”之前運行“接收電子郵件”活動。

我的問題是我不斷收到異常消息,提示Could not connect to SMTP host: localhost, port: 25 Transport.send(new_message) Could not connect to SMTP host: localhost, port: 25

但是,如果我運行相同的活動而沒有先調用接收電子郵件活動,則它可以正常工作。 有人可以幫我了解為什么會這樣嗎?

這是RecieveEmail Async任務的代碼(完整的Activity代碼太長)

 try{


               Properties props = new Properties();
               props.setProperty( "mail.imaps.socketFactory.class", "com.X509TrustAll.DummySSLSocketFactory" );
               Log.v("EmailList", "Stting properties");

              // Get the default Session object.
              session = Session.getDefaultInstance(props, null);
              Log.v("EmailList", "Geting Default Instance");

              // Get a Store object that implements the specified protocol.
              store = session.getStore("imaps");
              Log.v("EmailList", "Getting Sesion");

              //Connect to the current host using the specified username and password.
              Log.v("EmailList", "Connecting...");
              store.connect(host,port, user, password);
              Log.v("EmailList", "Connected");

              //Create a Folder object corresponding to the given name.
              Folder[] folders = store.getDefaultFolder().list("*");
              Log.v("EmailList", "Got Folder List");
              folder =  folders[5];



              // Open the Folder.
              Log.v("EmailList", "Opening Folder");
              folder.open(Folder.READ_ONLY);

              Log.v("EmailList", "Getting Messages");
              messages = folder.getMessages();

              Log.v("EmailList", "Got Messages");


          } catch (Exception e){

              Log.v("EmailList", "Exception");

          }
        }

              for (int i = lastMessageNumber -1;  i >= interateUpto; i--) {

                  Log.v("EmailRecieve", "Email no. " + Integer.toString(i));

                  javax.mail.Address[] froms = messages[i].getFrom();
                  String emailAdress = froms == null ? null : ((InternetAddress) froms[0]).getAddress();


                  FromAsync.add(emailAdress);

                  SubjectAsync.add(messages[i].getSubject());

                  SentDateAsync.add(messages[i].getSentDate().toString());


                  Part part = messages[i];

                  checkAttachments(part);



                  if (messages[i].isSet(Flag.SEEN)){

                      SeenAsync.add("true");

                  }else{

                      SeenAsync.add("false");

                  }


              }



              lastMessageNumber = lastMessageNumber - 19;   //new last number is stored

              folder.close(false);
              store.close();

        } catch (Exception e){

                  Log.v("EmailList", "Exception");

        }



      Log.v("EmailList", "Retrieve Email Finished");



        return null;

    }

這是發送電子郵件的代碼(同樣,完整代碼太長,因此我只發布了相關內容,即未發布UI等)。

@Override
    protected Void doInBackground(Void... arg0) {

        //Recipient's email ID needs to be mentioned.
          String to = To.getText().toString();

          // Sender's email ID needs to be mentioned
          String from = "foobar@foobar.hehe";

          // The Host
          String smtphost = "www.isleworthsyon.hounslow.sch.uk";

          // Get system properties
          Properties smtpproperties = System.getProperties();

          // Setup mail server
          smtpproperties.setProperty("mail.smtp.host", smtphost);

          // Get the default Session object.
          Session smtpsession = Session.getDefaultInstance(smtpproperties);

          try{
             MimeMessage new_message = new MimeMessage(smtpsession);

             // Set From:
             new_message.setFrom(new InternetAddress(from));

             // Set To:
             new_message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));

             // Set Subject: 
             new_message.setSubject(Subject.getText().toString());

             if (hasAttachment == true){

                 // Create the message part 
                 BodyPart messageBodyPart = new MimeBodyPart();

                 // Fill the message
                 messageBodyPart.setText(Body.getText().toString());

                 // Create a multipar message
                 Multipart multipart = new MimeMultipart();

                 // Set text message part
                 multipart.addBodyPart(messageBodyPart);

                 // Iterating over all Attachments    
                 for (int i=0; i < attachmentFiles.size(); i++){

                    if (attachmentFiles.get(i).equals("null") != true)    {

                         messageBodyPart = new MimeBodyPart();
                         String filename = attachmentFiles.get(i);
                         FileDataSource source = new FileDataSource(filename);
                         messageBodyPart.setDataHandler(new DataHandler(source));
                         messageBodyPart.setFileName(source.getName());
                         multipart.addBodyPart(messageBodyPart);

                    }
                 }


                 new_message.setContent(multipart);

             }else{

                 // Set Body if not attaching anything
                 new_message.setText(Body.getText().toString());

             }

             // Send message
             Transport.send(new_message);
             sendingSuccesfull = true;
          }catch (MessagingException mex) {
             sendingSuccesfull = false;
             Log.v("Email Compose", "Message Sending Failed, Details: " + mex.getMessage());

          }

        return null;
    }

嘗試清除這些常見錯誤 ,看看是否可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM