成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

android實現(xiàn)自動發(fā)送郵件的方法

這篇文章主要介紹了android實現(xiàn)自動發(fā)送郵件的方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)站頁面設(shè)計、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)絡(luò)公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設(shè)計最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅持講誠信,負責(zé)任的原則,為您進行細心、貼心、認真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。

需要添加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);
 //是否驗證
 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[]{"收件人郵箱"});
        //添加附件
        //這個附件的路徑是我手機里的啊,要發(fā)你得換成你手機里正確的路徑
//        sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");
        //發(fā)送郵件
        sender.sendEmail("smtp.163.com", "你的163郵箱賬號", "你的郵箱密碼");//<span >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();
      }
    });
 
  }
 
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“android實現(xiàn)自動發(fā)送郵件的方法”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

分享文章:android實現(xiàn)自動發(fā)送郵件的方法
網(wǎng)頁URL:http://jinyejixie.com/article46/pggoeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序云服務(wù)器、網(wǎng)站建設(shè)、網(wǎng)站策劃網(wǎng)站收錄、App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
阿拉善左旗| 杂多县| 长春市| 闽清县| 凤城市| 曲阜市| 荆州市| 梅州市| 郁南县| 宜章县| 霍邱县| 浙江省| 玉田县| 甘洛县| 吉安市| 西青区| 宣汉县| 丽水市| 德钦县| 都昌县| 易门县| 东乌珠穆沁旗| 商都县| 迁西县| 松阳县| 亚东县| 台北县| 承德市| 呼和浩特市| 屯昌县| 永顺县| 琼结县| 马关县| 皮山县| 静海县| 镇江市| 醴陵市| 南阳市| 河源市| 江阴市| 桐城市|