天天看點

安卓收發郵件怎樣實作

作者:藍色記錄wei088xin

安卓收發郵件怎樣實作?經過一番折騰,終于實作了,記錄一下,以免忘記。

要實作收發郵件,要在libs檔案夾中加入三個jar包。

activation.jar

additionnal.jar

mail.jar

自己可以去網上下載下傳。

安卓收發郵件怎樣實作

複制以下5個檔案就可以測試了。

第一個 MailSenderInfo.java

package com.aj.mail;

import java.util.Properties;

public class MailSenderInfo {

private String mailServerHost;

private String mailServerPort = "25";

private String fromAddress;

private String toAddress;

private String userName;

private String password;

private boolean validate = true;

private String subject;

private String content;

private String[] attachFileNames;

public Properties getProperties() {

Properties p = new Properties();

p.put("mail.smtp.host", this.mailServerHost);

p.put("mail.smtp.port", this.mailServerPort);

p.put("mail.smtp.auth", validate ? "true" : "false");

return p;

}

public String getMailServerHost() {

return mailServerHost;

}

public void setMailServerHost(String mailServerHost) {

this.mailServerHost = mailServerHost;

}

public String getMailServerPort() {

return mailServerPort;

}

public void setMailServerPort(String mailServerPort) {

this.mailServerPort = mailServerPort;

}

public boolean isValidate() {

return validate;

}

public void setValidate(boolean validate) {

this.validate = validate;

}

public String[] getAttachFileNames() {

return attachFileNames;

}

public void setAttachFileNames(String[] fileNames) {

this.attachFileNames = fileNames;

}

public String getFromAddress() {

return fromAddress;

}

public void setFromAddress(String fromAddress) {

this.fromAddress = fromAddress;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getToAddress() {

return toAddress;

}

public void setToAddress(String toAddress) {

this.toAddress = toAddress;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public String getContent() {

return content;

}

public void setContent(String textContent) {

this.content = textContent;

}

}

第二個 MyAuthenticator.java

package com.aj.mail;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator

{

String userName=null;

String password=null;

public MyAuthenticator()

{

}

public MyAuthenticator(String username, String password)

{

this.userName = username;

this.password = password;

}

protected PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(userName, password);

}

}

第三個 ReceiveMail

package com.aj.mail;

import com.tool.App;

import com.tool.Constant;

import java.util.Date;

import java.util.Properties;

import java.util.logging.Handler;

import javax.mail.Address;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Store;

import org.json.JSONObject;

public class ReceiveMail {

//收件人位址

public static String recipientAddress = Constant.MailAddress;

//收件人賬戶名

public static String recipientAccount = Constant.MailUser;

//收件人賬戶密碼

public static String recipientPassword = Constant.MailPassWord;

public static void receive(Handler handlerMail) throws Exception {

//1、連接配接郵件伺服器的參數配置

Properties props = new Properties();

//設定傳輸協定

props.setProperty("mail.store.protocol", "pop3");

//設定收件人的POP3伺服器

props.setProperty("mail.pop3.host", "pop3.139.com");

//2、建立定義整個應用程式所需的環境資訊的 Session 對象

Session session = Session.getInstance(props);

//設定調試資訊在控制台列印出來

//session.setDebug(true);

Store store = session.getStore("pop3");

//連接配接收件人POP3伺服器

store.connect("pop3.139.com", recipientAccount, recipientPassword);

//獲得使用者的郵件賬戶,注意通過pop3協定擷取某個郵件夾的名稱隻能為inbox

Folder folder = store.getFolder("inbox");

//設定對郵件賬戶的通路權限

folder.open(Folder.READ_WRITE);

//得到郵件賬戶的所有郵件資訊

Message [] messages = folder.getMessages();

for(int i = 0 ; i < messages.length ; i++){

//獲得郵件主題

String subject = messages[i].getSubject();

//獲得郵件發件人

Address[] from = messages[i].getFrom();

//擷取郵件内容(包含郵件内容的html代碼)

String content = (String) messages[i].getContent();

App.wt(content);

Date d=messages[i].getReceivedDate();

long time=d.getTime();

long now = System.currentTimeMillis();

long cha=now-time;

if(cha<60*60*1000){

if(content.contains("内容")&&subject.equals("測試")){

JSONObject js=new JSONObject();

js.put("time",0);

android.os.Message msg=new android.os.Message();

msg.obj = js;

msg.what = 0;

//這裡用handler傳過去處理

}

}

}

//關閉郵件夾對象

folder.close(true);

//關閉連接配接對象

store.close();

}

}

下面這個是活動,測試發送郵件用

package com.aj.mail;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import com.aj.nongmao.R;

import com.tool.App;

import com.tool.Constant;

import com.tool.Err;

public class SendEmailActivity extends Activity {

private Button send;

private EditText userid;

private EditText password;

private EditText from;

private EditText to;

private EditText subject;

private EditText body;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.mail2);

send = findViewById(R.id.send);

userid = findViewById(R.id.userid);

password = findViewById(R.id.password);

from = findViewById(R.id.from);

to = findViewById(R.id.to);

subject = findViewById(R.id.subject);

body = findViewById(R.id.body);

send.setText("内容");

userid.setText(Constant.MailUser);

password.setText(Constant.MailPassWord);

from.setText(Constant.MailAddress);

to.setText("[email protected]");

subject.setText("标題");

body.setText("内容");

send.setOnClickListener(new View.OnClickListener()

{

@Override

public void onClick(View v)

{

new Thread(

new Runnable(){

@Override

public void run() {

try

{

MailSenderInfo mailInfo = new MailSenderInfo();

mailInfo.setMailServerHost("smtp.139.com");

mailInfo.setMailServerPort("25");

mailInfo.setValidate(true);

mailInfo.setUserName(userid.getText().toString());

mailInfo.setPassword(password.getText().toString());

mailInfo.setFromAddress(from.getText().toString());

mailInfo.setToAddress(to.getText().toString());

mailInfo.setSubject(subject.getText().toString());

mailInfo.setContent(body.getText().toString());

SimpleMailSender sms = new SimpleMailSender();

sms.sendTextMail(mailInfo);

}

catch (Exception e) {

Err.getErrStr(e);

}

}

}).start();

// TODO Auto-generated method stub

}

});

}

}

最後一個

package com.aj.mail;

import java.util.Date;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

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;

import com.tool.Err;

public class SimpleMailSender

{

public void sendTextMail(final MailSenderInfo mailInfo)

{

new Thread(

new Runnable(){

@Override

public void run() {

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties();

if (mailInfo.isValidate())

{

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

}

Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

try

{

Message mailMessage = new MimeMessage(sendMailSession);

Address from = new InternetAddress(mailInfo.getFromAddress());

mailMessage.setFrom(from);

Address to = new InternetAddress(mailInfo.getToAddress());

mailMessage.setRecipient(Message.RecipientType.TO,to);

mailMessage.setSubject(mailInfo.getSubject());

mailMessage.setSentDate(new Date());

String mailContent = mailInfo.getContent();

mailMessage.setText(mailContent);

Transport.send(mailMessage);

//return true;

}

catch (MessagingException ex)

{

Err.getErrStr(ex);

}

}

}).start();

//return false;

}

public static boolean sendHtmlMail(MailSenderInfo mailInfo)

{

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties();

if (mailInfo.isValidate())

{

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

}

Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

try {

Message mailMessage = new MimeMessage(sendMailSession);

Address from = new InternetAddress(mailInfo.getFromAddress());

mailMessage.setFrom(from);

Address to = new InternetAddress(mailInfo.getToAddress());

// Message.RecipientType.TO

mailMessage.setRecipient(Message.RecipientType.TO,to);

mailMessage.setSubject(mailInfo.getSubject());

mailMessage.setSentDate(new Date());

Multipart mainPart = new MimeMultipart();

BodyPart html = new MimeBodyPart();

html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");

mainPart.addBodyPart(html);

mailMessage.setContent(mainPart);

Transport.send(mailMessage);

return true;

} catch (MessagingException ex) {

ex.printStackTrace();

}

return false;

}

}

注意:password不是登入郵箱的密碼,到郵箱去擷取密鑰。

繼續閱讀