欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實(shí)現(xiàn)自動發(fā)送郵件

 更新時間:2018年07月18日 14:17:06   作者:zuyuanzhang  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)自動發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了實(shí)現(xiàn)了一個android自動發(fā)送郵件的demo。支持163,qq郵箱

需要添加activation.jar,additionnal.jar和mail.jar這三個包

首先是一個EmailSender類

import java.io.File;
 
import java.util.Date;
import java.util.Properties;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
public class EmailSender {
 private Properties properties;
 private Session session;
 private Message message;
 private MimeMultipart multipart;
 
 public EmailSender() {
 super();
 this.properties = new Properties();
 }
 public void setProperties(String host,String post){
 //地址
 this.properties.put("mail.smtp.host",host);
 //端口號
 this.properties.put("mail.smtp.post",post);
 //是否驗(yàn)證
 this.properties.put("mail.smtp.auth",true);
 this.session=Session.getInstance(properties);
 this.message = new MimeMessage(session);
 this.multipart = new MimeMultipart("mixed");
 }
 /**
 * 設(shè)置收件人
 * @param receiver
 * @throws MessagingException
 */
 public void setReceiver(String[] receiver) throws MessagingException{
 Address[] address = new InternetAddress[receiver.length];
 for(int i=0;i<receiver.length;i++){
  address[i] = new InternetAddress(receiver[i]);
 }
 this.message.setRecipients(Message.RecipientType.TO, address);
 }
 /**
 * 設(shè)置郵件
 * @param from 來源
 * @param title 標(biāo)題
 * @param content 內(nèi)容
 * @throws AddressException
 * @throws MessagingException
 */
 public void setMessage(String from,String title,String content) throws AddressException, MessagingException{
 this.message.setFrom(new InternetAddress(from));
 this.message.setSubject(title);
 //純文本的話用setText()就行,不過有附件就顯示不出來內(nèi)容了
 MimeBodyPart textBody = new MimeBodyPart();
 textBody.setContent(content,"text/html;charset=gbk");
 this.multipart.addBodyPart(textBody);
 }
 /**
 * 添加附件
 * @param filePath 文件路徑
 * @throws MessagingException
 */
 public void addAttachment(String filePath) throws MessagingException{
 FileDataSource fileDataSource = new FileDataSource(new File(filePath));
 DataHandler dataHandler = new DataHandler(fileDataSource);
 MimeBodyPart mimeBodyPart = new MimeBodyPart();
 mimeBodyPart.setDataHandler(dataHandler);
 mimeBodyPart.setFileName(fileDataSource.getName());
 this.multipart.addBodyPart(mimeBodyPart);
 }
 /**
 * 發(fā)送郵件
 * @param host 地址
 * @param account 賬戶名
 * @param pwd 密碼
 * @throws MessagingException
 */
 public void sendEmail(String host,String account,String pwd) throws MessagingException{
 //發(fā)送時間
 this.message.setSentDate(new Date());
 //發(fā)送的內(nèi)容,文本和附件
 this.message.setContent(this.multipart);
 this.message.saveChanges();
 //創(chuàng)建郵件發(fā)送對象,并指定其使用SMTP協(xié)議發(fā)送郵件 
 Transport transport=session.getTransport("smtp"); 
 //登錄郵箱 
 transport.connect(host,account,pwd); 
 //發(fā)送郵件
 transport.sendMessage(message, message.getAllRecipients());
 //關(guān)閉連接
 transport.close();
 }
}

下面是mainactivity代碼

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
 private Button btnOK; 
  @Override
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnOK = (Button) findViewById(R.id.button);
    btnOK.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View arg0) {
//        sendEmail();
       //耗時操作要起線程...有幾個新手都是這個問題
        new Thread(new Runnable() {
 
       @Override
       public void run() {
        try {
        EmailSender sender = new EmailSender();
        //設(shè)置服務(wù)器地址和端口,網(wǎng)上搜的到
        sender.setProperties("smtp.163.com", "25");
        //分別設(shè)置發(fā)件人,郵件標(biāo)題和文本內(nèi)容
        sender.setMessage("你的163郵箱賬號", "EmailSender", "Java Mail !");
        //設(shè)置收件人
        sender.setReceiver(new String[]{"收件人郵箱"});
        //添加附件
        //這個附件的路徑是我手機(jī)里的啊,要發(fā)你得換成你手機(jī)里正確的路徑
//        sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");
        //發(fā)送郵件
        sender.sendEmail("smtp.163.com", "你的163郵箱賬號", "你的郵箱密碼");//<span style="font-family: Arial, Helvetica, sans-serif;">sender.setMessage("你的163郵箱賬號", "EmailS//ender", "Java Mail !");這里面兩個郵箱賬號要一致</span>
 
        } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
       }
       }).start();
      }
    });
 
  }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)實(shí)現(xiàn)拍照功能的方法實(shí)例解析

    Android開發(fā)實(shí)現(xiàn)拍照功能的方法實(shí)例解析

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)拍照功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android拍照功能的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android?studio實(shí)現(xiàn)日期?、時間選擇器與進(jìn)度條

    Android?studio實(shí)現(xiàn)日期?、時間選擇器與進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)日期、時間選擇器與進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Android自定義View實(shí)現(xiàn)圓形進(jìn)度條

    Android自定義View實(shí)現(xiàn)圓形進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • 詳解Flutter 調(diào)用 Android Native 的方法

    詳解Flutter 調(diào)用 Android Native 的方法

    這篇文章主要介紹了詳解Flutter 調(diào)用 Android Native 的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Android應(yīng)用?;顚?shí)踐詳解

    Android應(yīng)用?;顚?shí)踐詳解

    這篇文章主要介紹了Android應(yīng)用?;顚?shí)踐詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 微信或手機(jī)瀏覽器在線顯示office文件(已測試ios、android)

    微信或手機(jī)瀏覽器在線顯示office文件(已測試ios、android)

    這篇文章主要介紹了微信或手機(jī)瀏覽器在線顯示office文件,已測試ios、android,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android之IphoneTreeView帶組指示器的ExpandableListView效果

    Android之IphoneTreeView帶組指示器的ExpandableListView效果

    在正在顯示的最上面的組的標(biāo)簽位置添加一個和組視圖完全一樣的視圖,作為組標(biāo)簽。這個標(biāo)簽的位置要隨著列表的滑動不斷變化,以保持總是顯示在最上方,并且該消失的時候就消失
    2013-06-06
  • Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例

    Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Android下拉刷新框架實(shí)現(xiàn)代碼實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • 利用Android畫圓弧canvas.drawArc()實(shí)例詳解

    利用Android畫圓弧canvas.drawArc()實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android實(shí)現(xiàn)動態(tài)高斯模糊效果

    Android實(shí)現(xiàn)動態(tài)高斯模糊效果

    在Android開發(fā)中常常會用到高斯模糊,但有的時候我們可能會需要一個圖片以不同的模糊程度展現(xiàn)出來,那如何實(shí)現(xiàn)呢,一起通過本文來學(xué)習(xí)學(xué)習(xí)吧。
    2016-08-08

最新評論