android实现后台自动发邮件功能

公司项目需实现一个后台自动发邮件的功能,网上文档多为用Intent跳到GMail邮件编辑页面。

找了好久,终于在一国外网站找到解决办法,下面是代码。



主要用到图中两个类

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GMailSender extends Authenticator {
	private String mailhost = "smtp.gmail.com";
	private String user;
	private String password;
	private Session session;

	static {
		Security.addProvider(new JSSEProvider());
	}

	public GMailSender(String user, String password) {
		this.user = user;
		this.password = password;

		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", mailhost);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");

		session = Session.getDefaultInstance(props, this);
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(user, password);
	}

	public synchronized void sendMail(String subject, String body,
			String sender, String recipients) throws Exception {
		try {
			MimeMessage message = new MimeMessage(session);
			DataHandler handler = new DataHandler(new ByteArrayDataSource(
					body.getBytes(), "text/plain"));
			message.setSender(new InternetAddress(sender));
			message.setSubject(subject);
			message.setDataHandler(handler);
			if (recipients.indexOf(',') > 0)
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(recipients));
			else
				message.setRecipient(Message.RecipientType.TO,
						new InternetAddress(recipients));
			Transport.send(message);
		} catch (Exception e) {

		}
	}

	public class ByteArrayDataSource implements DataSource {
		private byte[] data;
		private String type;

		public ByteArrayDataSource(byte[] data, String type) {
			super();
			this.data = data;
			this.type = type;
		}

		public ByteArrayDataSource(byte[] data) {
			super();
			this.data = data;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getContentType() {
			if (type == null)
				return "application/octet-stream";
			else
				return type;
		}

		public InputStream getInputStream() throws IOException {
			return new ByteArrayInputStream(data);
		}

		public String getName() {
			return "ByteArrayDataSource";
		}

		public OutputStream getOutputStream() throws IOException {
			throw new IOException("Not Supported");
		}
	}

}


import java.security.AccessController;
import java.security.Provider;


public class JSSEProvider extends Provider {
public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}



程序中调用:

GMailSender sender = new GMailSender("....@gmail.com", "password");
            try {
				sender.sendMail("This is Subject",   
				        "This is Body",   
				        "sender",   
				        ".....@qq.com");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.e("other", e.getMessage(), e);   
			}   



测试发现new  GMailSender的参数只能是gmali,其他邮箱无效,gmail无需于手机绑定。


需要3个jar包,链接:http://download.csdn.net/detail/qiaoming_scorpio/5732699


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Android系统提供了多种方法来清理手机的后台运行应用,以提高系统性能和节省电池消耗。 首先,我们可以使用系统自带的任务管理器来查看并关闭后台运行的应用程序。通常可以通过长按手机的“Home”键或者是使用最近任务键来打开任务管理器。在任务管理器中,我们可以看到当前正在运行的应用程序,并且可以通过滑动或滑动手势将其关闭。 其次,一些手机品牌也会在系统设置中提供“清理手机”或“清理后台应用程序”的功能。这样的功能可以帮助用户一键清理后台运行的应用程序,以释放内存和关闭不必要的进程。 此外,一些第三方的清理应用也可以帮助用户清理手机后台。这些应用程序提供了更多的功能,比如自动清理后台运行的应用程序、加速手机性能、提醒用户清理垃圾文件等。用户可以根据自己的需要从应用商店下载并安装这些应用程序。 但需要注意的是,清理手机后台应用程序并不意味着所有后台运行的应用程序都是无用的或者会消耗过多的系统资源。某些应用程序可能需要在后台持续运行以提供某些功能,如社交媒体通知、邮件推送等。因此,用户在清理后台应用程序时应谨慎操作,确保不会影响到正常的手机使用。 综上所述,Android手机清理后台的方法包括使用系统自带的任务管理器、在系统设置中寻找清理手机的选项或者使用第三方的清理应用程序。使用这些方法可以有效地清理后台运行的应用程序,提高系统性能和节省电池消耗。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值