Android中利用commons-email.jar和 mail.jar 发送和接受邮件

来源:转载

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;

importorg.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 onenergy 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 communicatingwith 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( );

 

分享给朋友:
您可能感兴趣的文章:
随机阅读: