在新开发的APP中集成邮件发送功能,用来做用户信息反馈功能,支持带附件发送;
使用的javaMail、 Apache Commons Email 来集成邮件发送功能,
这里所需要用到的jar包:http://download.csdn.net/detail/uu00soldier/9630366
我测试的时候使用到了163、qq/foxmail邮箱这两种;
qq邮箱使用的时候要做一下特殊的处理,他是使用授权串的方式作为密码的,QQ邮箱的具体设置方法如下:
参考资料:http://www.jb51.net/article/78405.htm
开启SMTP服务
在 QQ 邮箱里的 设置->账户里开启 SMTP 服务
注意开启完之后,QQ 邮箱会生成一个授权码,在代码里连接邮箱使用这个授权码而不是原始的邮箱密码,这样可以避免使用明文密码。
若使用163邮箱,无需设置,直接使用账号、密码即可
关于setHostName()、setSmtpPort()两个参数的设置:
使用163邮箱的情况,直接设置smtp.163.com:25,即setHostName("smtp.163.com")、setSmtpPort(25),端口也可以不设置,默认的就是25端口
使用QQ邮箱分两种:
1、设置端口号,smtp.qq.com:587,将这个属性设置进去,就可以实现邮件发送功能;
2、不设置端口号,设置email.setStartTLSEnabled(true);属性也可以达到效果(端口号也可以保留);
完整代码如下:
tvSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String receive = "";//接收邮箱 final String cc = "";//抄送邮箱 final String bcc = "";//密送邮箱 final String send = "";//发送邮箱 final String pwd = "";//邮箱密码 final String subject = "";//发送标题 final String message = "";//发送文本信息 // 建立发送邮件任务 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, cc, bcc, 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(); } /** * @throws EmailException * @Description TODO 发送带附件的email */ private void sendEmailByApacheCommonsEmail(String from, String fromPwd, String to, String cc, String bcc, String subject, String message) throws EmailException { if (!verifyEmailAddress(from) || !verifyEmailAddress(to)) { System.out.println("enter verifyEmailAddress"); return; } // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setDebug(true); // 这里使用163邮箱服务器,实际需要修改为对应邮箱服务器 //smtp.163.com:25 smtp.qq.com:587 email.setHostName("smtp.qq.com"); // email.setSmtpPort(465);//163邮箱25 email.setSocketTimeout(6 * 1000); email.setCharset("UTF-8"); // email.setTLS(true); email.setStartTLSEnabled(true); // email.setSSL(true); email.setAuthentication(from, fromPwd); email.addTo(to, to); email.addBcc(bcc); email.addCc(cc); email.setFrom(from, from); email.setSubject(subject); email.setMsg(message); // Create the attachment EmailAttachment attachment2 = new EmailAttachment(); attachment2.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "pdf02.png"); attachment2.setDisposition(EmailAttachment.ATTACHMENT); attachment2.setDescription("pdf02"); attachment2.setName("pdf02.png"); EmailAttachment attachment1 = new EmailAttachment(); attachment1.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "pdf01.png"); attachment1.setDisposition(EmailAttachment.ATTACHMENT); attachment1.setDescription("pdf01"); attachment1.setName("pdf01.png"); email.attach(attachment1); email.attach(attachment2); // send the email String sendStr = email.send(); System.out.println("sendStr=" + sendStr); }