赞
踩
最近项目用到邮件发送功能,自然联想到掉起手机自带的邮件APK,但太过于依赖于系统的邮件apk,如何你需要默默后台发送邮件,这种功能显然是无法满足的,经过查阅资料基本可以找到两种现有的解决方案。
此jar可以实现简单的邮件发送,如果你只需要发送简单的文本邮件,这个包就可以满足需求。这个包封装的还是比较好的,监听接口等都很完善,使用也非常方便,唯一的缺点就是不支持附件、HTML等方式发送邮件,除非自己去扩展这个包,根据SMTP协议实现图片、附件、HTML邮件。
当然这个包还有其他功能,封装了很多协议,具体如下:
可支持附件、图片、HTML等邮件形式,使用这个包需要引入activation.jar、additionnal.jar,这样就可以实现带有附件的邮件发送了,这才是我们真正需要的,一般来说不能发送附件还是比较鸡肋的,当然这个mail.jar使用起来不是那么顺畅,所以apache有一个再次封装的包commons-email-1.4.jar,使用这个包发送邮件就非常简单了。
我已经写了一个DEMO,正式项目已经将本代码重构,这是一个测试DEMO,稍作修改就可以用于项目了。已经调试通过可正常发送邮件。
下面是界面,抄送和密送没有写入,需要你自己加入,具体可以参照Apache官网的对应包的API http://commons.apache.org/
当然第一步你需要向sd卡push 一张png图片(pdf02.png),如果你也是使用genymotion 模拟器可以使用如下方式将图片添加到sd 卡
然后点击界面发送后,logcat 会显示如下信息表示发送成功。
好了,到发件邮箱看看是不是有相应的邮件发出了!
好了,一切Ok!具体核心代码如下:
- package com.example.email;
-
- import java.io.File;
- import java.io.Writer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import org.apache.commons.mail.EmailAttachment;
- import org.apache.commons.mail.EmailException;
- import org.apache.commons.mail.MultiPartEmail;
- import org.apache.commons.net.ProtocolCommandEvent;
- import org.apache.commons.net.ProtocolCommandListener;
- import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
- import org.apache.commons.net.smtp.SMTPClient;
- import org.apache.commons.net.smtp.SMTPReply;
- import org.apache.commons.net.smtp.SimpleSMTPHeader;
- import org.apache.james.mime4j.codec.EncoderUtil;
- import org.apache.james.mime4j.codec.EncoderUtil.Usage;
-
- import android.app.Activity;
- import android.app.Fragment;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- if (savedInstanceState == null) {
- getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- private EditText et_receive, et_cc, et_bcc, et_send, et_pwd, et_subject, et_message;
- private Button btn_send;
-
- public PlaceholderFragment() {
-
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container, false);
- btn_send = (Button) rootView.findViewById(R.id.btn_send);
- et_receive = (EditText) rootView.findViewById(R.id.et_receive);
- et_cc = (EditText) rootView.findViewById(R.id.et_cc);
- et_bcc = (EditText) rootView.findViewById(R.id.et_bcc);
- et_send = (EditText) rootView.findViewById(R.id.et_send);
- et_pwd = (EditText) rootView.findViewById(R.id.et_password);
- et_subject = (EditText) rootView.findViewById(R.id.et_subject);
- et_message = (EditText) rootView.findViewById(R.id.et_message);
- setListener();
- return rootView;
- }
-
- private void setListener() {
-
- btn_send.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- final String receive = et_receive.getText().toString().trim();
- final String cc = et_cc.getText().toString().trim();
- final String bcc = et_bcc.getText().toString().trim();
- final String send = et_send.getText().toString().trim();
- final String pwd = et_pwd.getText().toString().trim();
- final String subject = et_subject.getText().toString().trim();
- final String message = et_message.getText().toString().trim();
- // 建立发送邮件任务
- new AsyncTask<String, Integer, Boolean>() {
-
- @Override
- protected Boolean doInBackground(String... params) {
- try {
- // sendEmailByApacheCommonsNet(send, pwd,
- // receive, subject, message);
- System.out.println("send=" + send + ",pwd=" + pwd + ",receive=" + receive + ",subject=" + subject + ",message="
- + message);
- sendEmailByApacheCommonsEmail(send, pwd, receive, subject, message);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- }.execute();
- }
- });
- }
-
- /**
- * 检测邮箱地址是否合法
- *
- * @param address
- * @return true合法 false不合法
- */
- private boolean verifyEmailAddress(String address) {
- if (null == address || "".equals(address))
- return false;
- Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");// 复杂匹配
- Matcher m = p.matcher(address);
- return m.matches();
- }
-
- /**
- * @Description TODO 发送带附件的email
- * @throws EmailException
- */
- private void sendEmailByApacheCommonsEmail(String from, String fromPwd, String to, String subject, String message) throws EmailException {
-
- if (!verifyEmailAddress(from) || !verifyEmailAddress(to)) {
- System.out.println("enter verifyEmailAddress");
- return;
- }
- // Create the attachment
- EmailAttachment attachment = new EmailAttachment();
- attachment.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "pdf02.png");
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("pdf02");
- attachment.setName("pdf02.png");
-
- // Create the email message
- MultiPartEmail email = new MultiPartEmail();
- email.setDebug(true);
- // 这里使用163邮箱服务器,实际需要修改为对应邮箱服务器
- email.setHostName("smtp.163.com");
- email.setSmtpPort(25);
- email.setSocketTimeout(6 * 1000);
- email.setCharset("UTF-8");
- email.setAuthentication(from, fromPwd);
- email.addTo(to, to);
- // email.addBcc(arg0);
- // email.addCc(arg0);
- email.setFrom(from, from);
- email.setSubject(subject);
- email.setMsg(message);
-
- // add the attachment
- email.attach(attachment);
-
- // send the email
- String sendStr = email.send();
- System.out.println("sendStr=" + sendStr);
- }
-
- /**
- * @Description TODO 只能发送简单的邮件文本,如需要发送附件需要自己去实现
- * @throws Exception
- */
- @SuppressWarnings("unused")
- private void sendEmailByApacheCommonsNet(String from, String fromPwd, String to, String subject, String message) throws Exception {
- if (!verifyEmailAddress(from) || !verifyEmailAddress(to)) {
- System.out.println("enter verifyEmailAddress");
- return;
- }
- String hostname = "smtp.163.com";
- int port = 25;
-
- String password = fromPwd;
- String login = from;
-
- String text = message;
-
- AuthenticatingSMTPClient client = new AuthenticatingSMTPClient();
- try {
- // optionally set a timeout to have a faster feedback on errors
- client.setDefaultTimeout(10 * 6000);
- // you connect to the SMTP server
- client.connect(hostname, port);
- client.addProtocolCommandListener(new ProtocolCommandListener() {
-
- @Override
- public void protocolCommandSent(ProtocolCommandEvent arg0) {
- System.out.println("protocolCommandSent =" + arg0.getCommand());
- }
-
- @Override
- public void protocolReplyReceived(ProtocolCommandEvent arg0) {
- System.out.println("protocolReplyReceived =" + arg0.getMessage());
- }
-
- });
- // you say ehlo and you specify the host you are connecting
- // from, could be anything
- client.ehlo("163");
- // if your host accepts STARTTLS, we're good everything will be
- // encrypted, otherwise we're done here
- if (client.execTLS()) {
- System.out.println("enter to login");
- client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, login, password);
- checkReply(client);
- client.setSender(from);
- checkReply(client);
-
- client.addRecipient(to);
- checkReply(client);
-
- Writer writer = client.sendMessageData();
-
- if (writer != null) {
- // 使用apache mime4j中的EncoderUtil来编码邮件主题
- String encodedSubject = EncoderUtil.encodeIfNecessary(subject, Usage.TEXT_TOKEN, 0);
- SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, encodedSubject);
- header.addHeaderField("Content-Type", "text/plain; charset=UTF-8");
- header.addHeaderField("Content-Transfer-Encoding", "base64");
- // 使用apache mime4j中的EncoderUtil来编码邮件正文
- String encodedBody = EncoderUtil.encodeB(text.getBytes());
- writer.write(header.toString());
- writer.write(encodedBody);
- writer.close();
- if (!client.completePendingCommand()) {// failure
- throw new Exception("Failure to send the email ");
- }
- } else {
- throw new Exception("Failure to send the email ");
- }
- } else {
- throw new Exception("STARTTLS was not accepted ");
- }
- } catch (Exception e) {
- throw e;
- } finally {
- client.logout();
- client.disconnect();
- }
- }
-
- private static void checkReply(SMTPClient sc) throws Exception {
- if (SMTPReply.isNegativeTransient(sc.getReplyCode())) {
- throw new Exception("Transient SMTP error ");
- } else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) {
- throw new Exception("Permanent SMTP error ");
- }
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。