Android下发送邮件功能总结
来源:互联网 发布:查看linux系统编码 编辑:程序博客网 时间:2024/06/03 20:35
最近项目用到邮件发送功能,自然联想到掉起手机自带的邮件APK,但太过于依赖于系统的邮件apk,如何你需要默默后台发送邮件,这种功能显然是无法满足的,经过查阅资料基本可以找到两种现有的解决方案。
1、第一种使用commons-net-3.3.jar
此jar可以实现简单的邮件发送,如果你只需要发送简单的文本邮件,这个包就可以满足需求。这个包封装的还是比较好的,监听接口等都很完善,使用也非常方便,唯一的缺点就是不支持附件、HTML等方式发送邮件,除非自己去扩展这个包,根据SMTP协议实现图片、附件、HTML邮件。
当然这个包还有其他功能,封装了很多协议,具体如下:
- FTP/FTPS
- FTP over HTTP (experimental)
- NNTP
- SMTP(S)
- POP3(S)
- IMAP(S)
- Telnet
- TFTP
- Finger
- Whois
- rexec/rcmd/rlogin
- Time (rdate) and Daytime
- Echo
- Discard
- NTP/SNTP
2、使用mail.jar核心邮件包实现邮件发送功能
可支持附件、图片、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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState == null) {getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();}}@Overridepublic 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;}@Overridepublic 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() {}@Overridepublic 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() {@Overridepublic 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>() {@Overrideprotected 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 attachmentEmailAttachment 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 messageMultiPartEmail 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 attachmentemail.attach(attachment);// send the emailString 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 errorsclient.setDefaultTimeout(10 * 6000);// you connect to the SMTP serverclient.connect(hostname, port);client.addProtocolCommandListener(new ProtocolCommandListener() {@Overridepublic void protocolCommandSent(ProtocolCommandEvent arg0) {System.out.println("protocolCommandSent =" + arg0.getCommand());}@Overridepublic void protocolReplyReceived(ProtocolCommandEvent arg0) {System.out.println("protocolReplyReceived =" + arg0.getMessage());}});// you say ehlo and you specify the host you are connecting// from, could be anythingclient.ehlo("163");// if your host accepts STARTTLS, we're good everything will be// encrypted, otherwise we're done hereif (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()) {// failurethrow 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 ");}}}}
3、DEMO下载
点此下载
- Android下发送邮件功能总结
- (Android 功能代码) android 邮件发送
- 在Android中集成邮件发送功能
- android:访问邮件软件发送邮件功能实现。
- Android下使用activation发送邮件
- 发送邮件功能
- 邮件发送功能
- java 发送邮件功能
- JavaMail发送邮件功能
- iOS发送邮件功能
- Java 发送邮件功能
- Spring邮件发送功能
- 邮件发送功能
- 邮件发送功能
- JavaMail邮件发送功能
- YII 邮件发送功能
- 发送邮件功能
- java发送邮件功能
- 我的YUV播放器MFC小笔记:设置picture控件背景为黑色、窗口缩放
- 每天一个Linux命令-16(nl)
- HDOJ 2037 今年暑假不AC 【贪心】
- 每天读一遍,不久你就会变
- 工作学习以及收获
- Android下发送邮件功能总结
- 开源的音频库
- AJAX的话题与简单实现
- Oracle_自适应游标共享的整体执行流程
- C#对Ajax页面内容采集
- 尾纤接口类型与作用
- 英语句子倒序
- igrimaceV8.0.0 IG 一键新机 陌陌 Uber优步打针 平安易贷 滴滴 一号专车 饿了么 ios8 V8 ZTN(插件安装方式)
- CSS中关于多个选择器中是否含有空格的区别