国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-10-21 21:46:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

需要添加activation.jar,additionnal.jar和mail.jar這三個(gè)包

首先是一個(gè)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); //端口號(hào) 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 來(lái)源 * @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()就行,不過有附件就顯示不出來(lái)內(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ā)送時(shí)間 this.message.setSentDate(new Date()); //發(fā)送的內(nèi)容,文本和附件 this.message.setContent(this.multipart); this.message.saveChanges(); //創(chuàng)建郵件發(fā)送對(duì)象,并指定其使用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();       //耗時(shí)操作要起線程...有幾個(gè)新手都是這個(gè)問題        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郵箱賬號(hào)", "EmailSender", "Java Mail !");        //設(shè)置收件人        sender.setReceiver(new String[]{"收件人郵箱"});        //添加附件        //這個(gè)附件的路徑是我手機(jī)里的啊,要發(fā)你得換成你手機(jī)里正確的路徑//        sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");        //發(fā)送郵件        sender.sendEmail("smtp.163.com", "你的163郵箱賬號(hào)", "你的郵箱密碼");//<span style="font-family: Arial, Helvetica, sans-serif;">sender.setMessage("你的163郵箱賬號(hào)", "EmailS//ender", "Java Mail !");這里面兩個(gè)郵箱賬號(hào)要一致</span>         } catch (AddressException e) {        // TODO Auto-generated catch block        e.printStackTrace();        } catch (MessagingException e) {        // TODO Auto-generated catch block        e.printStackTrace();        }       }       }).start();      }    });   } }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绿春县| 花莲县| 罗田县| 肇源县| 微博| 红安县| 长岭县| 中阳县| 博爱县| 合肥市| 玉溪市| 黄龙县| 都匀市| 武川县| 夏邑县| 五原县| 扶沟县| 荔浦县| 凌云县| 彭泽县| 丹阳市| 延吉市| 阿勒泰市| 扎囊县| 长沙市| 北安市| 黎川县| 襄垣县| 乌拉特后旗| 吉木萨尔县| 灵丘县| 武鸣县| 红桥区| 商都县| 汾阳市| 荥经县| 从化市| 泊头市| 阳朔县| 贵南县| 咸宁市|