由于公司有库存,但是老板不舍得买软件,每次仓库同事出库都是手动统计设备sn号,很是麻烦。想着写个程序通过PDA实现条码识别,然后通过邮箱发送到PC端。今天简单分享下自己踩得坑
本文以163邮箱为例
1、首先需要在163邮箱中开通POP3/SMTP服务并获取授权码,
保留好授权码、保留好授权码、保留好授权码
下面直接上代码
2、新建一个EmailSender类
import android.app.blob.BlobStoreManager;
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();
}
}
3、下面是放在MainActivity中代码
case R.id.send:
new Thread(new Runnable() {
@Override
public void run() {
try {
EmailSender sender = new EmailSender();
//设置服务器地址和端口
sender.setProperties("smtp.163.com", "25");
//分别设置发件人,邮件标题和文本内容
sender.setMessage("******@163.com", Util.getCurDateTime()+"出库统计", "设备SN !");
//设置收件人
sender.setReceiver(new String[]{"******@126.com"});
//添加附件
sender.addAttachment( Environment.getExternalStorageDirectory()
.getAbsolutePath()+ "/sendemail" + "/" +"kumai.txt");
//下面是重点 否则程序会报
//javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart
//以下代码 在执行sendEmail前加上如下代码就行!
// add handlers for main MIME types
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
//以下需要注意,下面又是一个大坑,很多说是填写你发件人邮箱的密码,其实不是 这里需要填的是邮箱授权码
// 否则程序会报 javax.mail.AuthenticationFailedException
//发送邮件 "smtp.163.com", "你的163邮箱账号", "邮箱授权码";
sender.sendEmail("smtp.163.com", "******@163.com", "*********AHN");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
效果展示图