浏览 4560 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-21
最后修改:2011-02-15
public class SendMailAgent { private MimeMessage mimeMsg; //MIME邮件对象 private Session session; //邮件会话对象 private Properties props; //系统属性 private boolean needAuth = true; //smtp是否需要认证 private String hostName = constantVar.EMAIL_SERVER; private String username = constantVar.EMAIL_ACCOUNT; //smtp认证用户名和密码 private String password = constantVar.EMAIL_PASSWORD; private String from = constantVar.EMAIL_MANAGER; private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 /* public SendMailAgent() { setSmtpHost(ConsConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取 createMimeMessage(); }**/ public SendMailAgent(){ //System.out.println(hostName); setSmtpHost(); createMimeMessage(); } /** * @param hostName String */ public void setSmtpHost( ) { //System.out.println("设置系统属性:mail.smtp.host = "+hostName); if(props == null)props = System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host",hostName); //设置SMTP主机 } /** * @return boolean */ public boolean createMimeMessage() { try{ //System.out.println("准备获取邮件会话对象!"); session = Session.getDefaultInstance(props,null); //获得邮件会话对象 } catch(Exception e){ //System.err.println("获取邮件会话对象时发生错误!"+e); return false; } //System.out.println("准备创建MIME邮件对象!"); try{ mimeMsg = new MimeMessage(session); //创建MIME邮件对象 mp = new MimeMultipart(); return true; } catch(Exception e){ //System.err.println("创建MIME邮件对象失败!"+e); return false; } } /** * @param need boolean */ public void setNeedAuth(boolean need) { // new javax.mail.PasswordAuthentication("",""); //SmtpAuthenticator sa=new SmtpAuthenticator(); //System.out.println("设置smtp身份认证: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 String * @param pass String */ // public void setNamePass(String name,String pass) { // username = name; // password = pass; // } /** * @param mailSubject String * @return boolean */ public boolean setSubject(String mailSubject) { //System.out.println("设置邮件主题!"); try{ mimeMsg.setSubject(mailSubject); return true; } catch(Exception e) { //System.err.println("设置邮件主题发生错误!"); return false; } } /** * @param mailBody String */ public boolean setBody(String mailBody) { try{ BodyPart bp = new MimeBodyPart(); bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312"); mp.addBodyPart(bp); return true; } catch(Exception e){ //System.err.println("设置邮件正文时发生错误!"+e); return false; } } /** * @param name String * @param pass String */ public boolean addFileAffix(String filename) { //System.out.println("增加邮件附件:"+filename); try{ BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); return true; } catch(Exception e){ //System.err.println("增加邮件附件:"+filename+"发生错误!"+e); return false; } } /** * @param name String * @param pass String */ public boolean setFrom( ) { //System.out.println("设置发信人!"); //String from= mailServer.getEMAIL_MANAGER(); try{ mimeMsg.setFrom(new InternetAddress(from)); //设置发信人 return true; } catch(Exception e) { return false; } } /** * @param name String * @param pass String */ public boolean setTo(String to){ if(to == null)return false; try{ mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to)); return true; } catch(Exception e) { return false; } } /** * @param name String * @param pass String */ public boolean setCopyTo(String copyto) { if(copyto == null)return false; try{ mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto)); return true; } catch(Exception e) { return false; } } /** * @param name String * @param pass String */ public boolean sendout() { try{ mimeMsg.setContent(mp); mimeMsg.saveChanges(); //System.out.println("正在发送邮件...."); Session mailSession = Session.getInstance(props,null); Transport transport = mailSession.getTransport("smtp"); transport.connect((String)props.get("mail.smtp.host"),username,password); transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO)); //transport.send(mimeMsg); //System.out.println("发送邮件成功!"); transport.close(); return true; } catch(Exception e) { //System.err.println("邮件发送失败!"+e); return false; } } public boolean sendEmail(String title,String mailbody,String to_email ) { setNeedAuth(true); if( setSubject(title) == false) return false; if( setBody(mailbody) == false) return false; if( setTo(to_email)== false) return false; if( setFrom() == false) return false; if( sendout() == false) return false; return true; } /** * Just do it as this */ public static void main(String[] args) { String mailbody = "测试邮件"; SendMailAgent SendMailServer = new SendMailAgent(); try { SendMailServer.sendEmail("测试", mailbody, "c.j.w_521@163.com"); } catch (Exception e) { System.out.println("发送邮件失败!"+e); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-07-24
建议用apache组件
很方便!就10几行代码就搞定, |
|
返回顶楼 | |
发表时间:2008-08-05
这个函数
setNeedAuth(boolean need) 虽然设置了true,但是你还是没有进行验证啊,应该在获取会话之前加上以下代码 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password);//你的邮箱用户名和密码 } }; Session s = Session.getInstance(p,auth); 还有你215行这里,临时变量mailSession只是为了发送邮件,为什么不用全局变量session啊? 你试试吧。我在邮件发送过程中也是问题很多,汗啊........ |
|
返回顶楼 | |