频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

javax.mail使用例子

来源: 作者:佚名 出处:巧巧读书 2007-11-05 进入讨论组

package pg.system; /** * @author pdw * * TODO 发送邮件! * */ import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.security.spec.X509EncodedKeySpec; public class SendMail { public MimeMessage mimeMsg; //要发送的email信息 private Session session; private Properties props; private boolean needAuth=false; private String username=""; private String password=""; private Multipart mp; //存放邮件的title 内容和附件 public SendMail(String stmp){ setSmtpHost(stmp); createMimeMessage(); } /** * * @param hostName */ public void setSmtpHost(String hostName){ System.out.println("mail.stmp.host= "+hostName); if(props==null){ props=System.getProperties(); } props.put("mail.smtp.host",hostName); } public boolean createMimeMessage(){ try{ System.out.println("Session begin-----------"); session=Session.getInstance(props,null); }catch(Exception e){ System.out.println("Session.getInstance faild!"+e); return false; } System.out.println("MimeMEssage begin---------!"); try{ mimeMsg=new MimeMessage(session); mp=new MimeMultipart(); return true; }catch(Exception e){ System.out.println("MimeMessage fiald! "+e.toString()); return false; } } /** * * @param need */ public void setNeedAuth(boolean need){ System.out.println(":mail.smtp.auth="+need); if(props==null){ props=System.getProperties(); } if(need){ props.put("mail.smtp.auth","true"); } else{ props.put("mail.smtp.auth","false"); } } /** * * @param name * @param pass */ public void setNamePass(String name,String pass){ username=name; password=pass; } /** * * @param mailSubject * @return boolean */ public boolean setSubject(String mailSubject){ System.out.println("set title begin."); try{ if(!mailSubject.equals("")&&mailSubject!=null){ mimeMsg.setSubject(mailSubject); } return true; }catch(Exception e){ System.out.println("set Title faild!"); return false; } } /** * 添加附件.. * @param filename * @return */ public boolean addFileAffix(String filename){ System.out.println("增加附件.."); if(filename.equals("")||filename==null){ return false; } String file[]; file=filename.split(";"); System.out.println("你有 "+file.length+" 个附件!"); try{ for(int i=0;i<file.length;i++){ BodyPart bp=new MimeBodyPart(); FileDataSource fileds=new FileDataSource(file[i]); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); } return true; }catch(Exception e){ System.err.println("增加附件: "+filename+"--faild!"+e); return false; } } /** * * @param from * @return */ public boolean setFrom(String from){ System.out.println("Set From ."); try{ mimeMsg.setFrom(new InternetAddress(from)); return true; }catch(Exception e){ return false; } } /** * * @param to * @return */ public boolean setTo(String to){ System.out.println("Set to."); if(to==null||to.equals("")){ return false; } try{ mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to)); return true; }catch(Exception e){ return false; } } public boolean setCopyTo(String copyto){ if(copyto.equals("")||copyto==null){ return false; } try{ String copy[]; copy=copyto.split(";"); for(int i=0;i<copy.length;i++){ mimeMsg.setRecipients(Message.RecipientType.TO,(Address[])InternetAddress.parse(copy[i])); } return true; }catch(Exception e){ return false; } } /** * 设置信的内容! * @param mailBody * @return boolean */ public boolean setBody(String mailBody){ try{ BodyPart bp=new MimeBodyPart(); bp.setContent("<meta http-equiv=Context-Type context=text/html;charset=gb2312>"+mailBody,"text/html;charset=GB2312"); mp.addBodyPart(bp); return true; }catch(Exception e){ System.out.println("Set context Faild! "+e); return false; } } /** * * @param htmlpath * @return boolean */ public boolean setHtml(String htmlpath){ try{ if(!htmlpath.equals("")||htmlpath!=null){ BodyPart mbp=new MimeBodyPart(); DataSource ds=new FileDataSource(htmlpath); mbp.setDataHandler(new DataHandler(ds)); mbp.setHeader("Context-ID","meme"); mp.addBodyPart(mbp); } return true; }catch(Exception e){ System.err.println("Set Html Faild!"+e); return false; } } public boolean setOut(){ try{ mimeMsg.setContent(mp); mimeMsg.saveChanges(); System.out.println("正在SendMail."); Session mailSession=session.getInstance(props,null); Transport tp=mailSession.getTransport("smtp"); tp.connect((String)props.getProperty("mail.stmp.host"),username,password); tp.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO)); //tp.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC)); System.out.println("Send Mail 成功.."); tp.close(); return true; }catch(Exception e){ e.printStackTrace(); return false; } } }
测试类

package pg.system; public class SendMailDemo { public static void main(String[] args) { SendMail sm=new SendMail("mail.ldtec.com"); sm.setNamePass("peidewan@ldtec.com","123456"); sm.setSubject("测试,测试"); sm.setFrom("peidewan@ldtec.com"); sm.setTo("pdw2009@yahoo.com.cn"); sm.addFileAffix("f:/adsl.txt"); StringBuffer bs=new StringBuffer(); bs.append("裴德万:\n"); bs.append(" 测试度奇珍异宝埼地在檌!!!!!!!!!!!"); sm.setBody("DFSAAAAAAAAAAAAAAAAA"); sm.setNeedAuth(true); boolean b=sm.setOut(); if(b){ System.out.println("\n邮件发送成功!!!!!"); } else{ System.out.println("邮件发送失败!!!!"); } } }
文字:http://www.qqread.com/java/2007/11/c380005.html 更多文章 更多内容请看电子邮件使用基础篇专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章