android实现自动发送邮件

本文实例为大家分享了实现了一个android自动发送邮件的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);
 //是否验证
 this.properties.put("mail.smtp.auth",true);
 this.session=Session.getInstance(properties);
 this.message = new MimeMessage(session);
 this.multipart = new MimeMultipart("mixed");
 }
 /**
 * 设置收件人
 * @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);
 }
 /**
 * 设置邮件
 * @param from 来源
 * @param title 标题
 * @param content 内容
 * @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()就行,不过有附件就显示不出来内容了
 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);
 }
 /**
 * 发送邮件
 * @param host 地址
 * @param account 账户名
 * @param pwd 密码
 * @throws MessagingException
 */
 public void sendEmail(String host,String account,String pwd) throws MessagingException{
 //发送时间
 this.message.setSentDate(new Date());
 //发送的内容,文本和附件
 this.message.setContent(this.multipart);
 this.message.saveChanges();
 //创建邮件发送对象,并指定其使用SMTP协议发送邮件
 Transport transport=session.getTransport("smtp");
 //登录邮箱
 transport.connect(host,account,pwd);
 //发送邮件
 transport.sendMessage(message, message.getAllRecipients());
 //关闭连接
 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();
        //设置服务器地址和端口,网上搜的到
        sender.setProperties("smtp.163.com", "25");
        //分别设置发件人,邮件标题和文本内容
        sender.setMessage("你的163邮箱账号", "EmailSender", "Java Mail !");
        //设置收件人
        sender.setReceiver(new String[]{"收件人邮箱"});
        //添加附件
        //这个附件的路径是我手机里的啊,要发你得换成你手机里正确的路径
//        sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");
        //发送邮件
        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();
      }
    });

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android 后台发送邮件到指定邮箱

    这个主要是搜集app报错信息之后将信息上传到邮箱,请不要用做其他非法用途.代码请参考: MailUtils.Java public class MailUtils { /** * 创建一封只包含文本的简单邮件 * * @param session 和服务器交互的会话 * @param sendMail 发件人邮箱 * @param receiveMail 收件人邮箱 * @return * @throws Exception */ public static MimeMessage create

  • Android开发中怎样调用系统Email发送邮件(多种调用方式)

    我们都知道,在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外. 在Android中,调用Email有三种类型的Intent: Intent.ACTION_SENDTO 无附件的发送 Intent.ACTION_SEND 带附件的发送 Intent.ACTION_SEND_MULTIPLE 带有多附件的发送 当然,所谓的调用Email,只是说Email可以接收Intent并做这些事情,可能也有其他的应用程序实现了相关功能,所以在执行的时候,会出现选择

  • Android监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)

    本文实例讲述了Android监听手机电话状态与发送邮件通知来电号码的方法.分享给大家供大家参考,具体如下: 在android中可以用PhoneStateListener来聆听手机电话状态(比如待机.通话中.响铃等).本例是通过它来监听手机电话状态,当手机来电时,通过邮件将来电号码发送到用户邮箱的例子.具体程序如下: import android.app.Activity; import android.content.Intent; import android.os.Bundle; impor

  • Android 后台发送邮件示例 (收集应用异常信息+Demo代码)

    上一次说了如何收集我们已经发布的应用程序的错误信息,方便我们调试完善程序.上次说的收集方法主要是把收集的信息通过Http的post请求把相关的异常信息变成请求参数发送到服务器.这个对做过web开发的人来说,服务端处理是很简单.不过对很多没做个web的人来说却是麻烦事.今天介绍个更简单的方法,我们把异常信息收集后,通过后台发送邮件方法,把相关异常信息发送到我们指定的邮箱里面. 这篇文章是实用性文章,不会涉及太多理论分析.主要是让大家看了以后知道怎么在自己的应用里面添加这个功能. 1.第三方库这次发

  • Android发送邮件的方法实例详解

    本文实例讲述了Android发送邮件的方法.分享给大家供大家参考,具体如下: 在android手机中实现发送邮件的功能也是不可缺少的.如何实现它呢?下面以简单的例子进行说明. 程序如下: import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import

  • Android快速实现发送邮件实例

    前言 现在一般很少有用Android原生app发送邮件的需求,但是前段时间公司项目需要在Android app 内部发送邮件,于是就在网上收集资料并整理了一个Demo.虽然最后这个需求被减掉了,但是我这里还是把Demo的内容给大家分享一下. 第一步.导入第三方jar包 Android实现发送邮件,首先需要依赖additional.jar.mail.jar和activation.jar这3个jar包.Google提供了下载地址:https://code.google.com/archive/p/j

  • Android中使用Service实现后台发送邮件功能实例

    本文实例讲述了Android中使用Service实现后台发送邮件功能.分享给大家供大家参考,具体如下: 程序如下: import android.app.Activity; import android.content.Intent; import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.widget.TextView; public class A05Ac

  • android实现自动发送邮件

    本文实例为大家分享了实现了一个android自动发送邮件的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.FileD

  • PHP实现自动发送邮件功能代码(qq 邮箱)

    最近做一个邮箱验证的功能,研究了一会,搞定了邮件的自动发送.下面用qq邮箱作为演示,一步一步来解释: 代码下载地址 首先,就是做到邮件的发送,代码如下: <?PHP //邮件发送 require './mailer/class.phpmailer.php'; require './mailer/class.smtp.php'; date_default_timezone_set('PRC');//设置邮件发送的时间,如果不设置,则会显示其他区的时间 $mail = new PHPMailer()

  • sqlserver2008自动发送邮件

    这两天都在搞这个东西,从开始的一点不懂,到现在自己可以独立的完成这个功能!在这个过程中,CSDN的好多牛人都给了我很大的帮助,在此表示十二分的感谢!写这篇文章,一是为了巩固一下,二嘛我也很希望我写的这点小东西能帮助遇到同样问题的朋友们!当然这里有一部分是从网上的摘录的实现一个类似于注册平台的功能:比如注册了一个用户,就会向注册邮箱里发送一封邮件.首先是要搭建一个自动发送邮件的平台,这个用sql server 2008(sql server 2005也有)的database mail就能很方便的实

  • Android ListView自动显示隐藏布局的实现方法

    借助View的OnTouchListener接口来监听listView的滑动,通过比较与上次坐标的大小,判断滑动方向,并通过滑动方向来判断是否需显示或者隐藏对应的布局,并且带有动画效果. 1.自动显示隐藏Toolbar 首先给listView增加一个HeaderView,避免第一个Item被Toolbar遮挡. View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.

  • Android实现自动文本框提示功能

    本文实例为大家分享了Android实现自动文本框提示的具体代码,供大家参考,具体内容如下 activity_main.xml布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" a

  • PHP实现163邮箱自动发送邮件

    163邮箱大家都使用过吧,那么基于php如何实现163邮箱自动发送邮件功能呢,下面我们小编给大家分享具体实现代码: 想给大家展示下效果图: demo.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:/

  • 纯javascript实现自动发送邮件

    描述: 此JavaScript将帮助你的电子邮件的人.只要按一下电子邮件,有人!和JavaScript会要求的电子邮件地址,主题,等等然后你,新的邮件,是向你打开了. <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function mailsome1(){ who=prompt("Enter recipient's email address: ","antispammer@earthling.net

  • Android 实现自动打电话与发短信的实例

    Android 实现自动打电话与发短信的实例 一.自动可以拨打电话  bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EditText et = (EditText)findViewById(R.id.et); String number = et.getText().toString(); //激活可以打电话的组件 Intent intent = new

  • Android AutoCompleteTextView自动提示文本框实例代码

    自动提示文本框(AutoCompleteTextView)可以加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 先给大家展示下效果图,如果大家感觉还不错,请参考实现代码: 最后一张获取文本框里面的值(其实就跟TextView.EditText一样): 首先,在xml中定义AutoCompleteTextView控件: activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/r

  • Android程序自动更新功能模块的实现方法【附完整demo源码下载】

    本文实例讲述了Android程序自动更新功能模块的实现方法.分享给大家供大家参考,具体如下: 在程序启动的时候检测服务器上有没有对应版本更新,如果有更新,提示用户是否更新. 在程序启动的时候首先调用更新模块检测服务器上存放的版本号跟当前程序的版本号如果大于当前版本号,弹出更新对话框,如果用户选择更新,则显示当前更新状态,然后替换当前程序. 程序调用版本更新检测: private UpdateManager updateMan; private ProgressDialog updateProgr

随机推荐