Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android中利用commons-email.jar和 mail.jar 發送和接受郵件

Android中利用commons-email.jar和 mail.jar 發送和接受郵件

編輯:Android開發實例

commons-mail.jar 發送郵件

mail.jar additionnal.jar  收郵件(雖然可以實現收發,需要配合activation.jar)

 

 

發郵件

 注意:在Android中很容易出現 

 Multipart multipart = null;

Object obj = message.getContent();

  if (obj instanceof Multipart) {

                multipart = (Multipart) obj;

   } else {

              this.sendJavascript("javascript:alert('有封郵件接收出錯')");

                continue;

 }類型匹配錯誤


import org.apache.commons.net.smtp.SMTP;

import org.apache.commons.net.smtp.SMTPClient;

import org.apache.commons.net.smtp.SMTPReply;

SMTPClient client = new SMTPClient( );

client.connect("www.discursive.com");

int response = client.getReplyCode( );

if( SMTPReply.isPositiveCompletion( response ) ) {

// Set the sender and the recipients

client.setSender( "[email protected]" );

client.addRecipient( "[email protected]" );

client.addRecipient( "[email protected]" );

// Supply the message via a Writer

Writer message = client.sendMessageData( );

message.write( "Spend more money on energy research.  Thanks." );

message.close( );

// Send the message and print a confirmation

boolean success = client.completePendingCommand( );

if( success ) {

System.out.println( "Message sent" );

}

} else {

System.out.println( "Error communicating with SMTP server" );

}

client.disconnect( );

 

 

 

 

commons.net  接收郵件

import org.apache.commons.io.CopyUtils;
import org.apache.commons.io.IOUtils;
import org.apache. commons.net.pop3.POP3Client;
import org.apache.commons.net.pop3.POP3MessageInfo;
 
 
POP3Client client = new POP3Client( );
client.connect("www.discursive.com");
client.login("[email protected]", "secretpassword");
POP3MessageInfo[] messages = client.listMessages( );
for (int i = 0; i < messages.length; i++) {
int messageNum = messages[i].number;
System.out.println( "************* Message number: " + messageNum );
Reader reader = client.retrieveMessage( messageNum );
System.out.println( "Message:\n" + IOUtils.toString( reader ) );
IOUtils.closeQuietly( reader );
}
client.logout( );
client.disconnect( );
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved