/** * 邮件发送 */ /** * @author */ public class EmailServiceImpl { private String sendHost = null;// 发送服务器地址 private String receiveHost = null;// 接受服务器地址 private String user = null;// 邮件服务器账号 private String password = null;// 邮件服务器账号密码 private String from = null;// 来源地址 /** * 获得配置文件内容 * @author */ public void Proper() { Properties prop = new Properties(); String file = this.getClass().getResource("/emailserver.properties").getFile(); FileInputStream in = null; try { in = new FileInputStream(file); prop.load(in); in.close(); } catch (Exception e) { e.printStackTrace(); } sendHost=prop.getProperty("emailserver.sendHost"); receiveHost = prop.getProperty("emailserver.receiveHost"); user=prop.getProperty("emailserver.user"); password=prop.getProperty("emailserver.password"); from=prop.getProperty("emailserver.from"); //System.out.println(password); //System.out.println("==sendHost="+sendHost+"==receiveHost=="+receiveHost+"===user==="+user+"===password=="+password); } /** * @return the sendHost */ public String getSendHost() { return sendHost; } /** * @param sendHost * the sendHost to set */ public void setSendHost(String sendHost) { this.sendHost = sendHost; } /** * @return the receiveHost */ public String getReceiveHost() { return receiveHost; } /** * @param receiveHost * the receiveHost to set */ public void setReceiveHost(String receiveHost) { this.receiveHost = receiveHost; } /** * @return the user */ public String getUser() { return user; } /** * @param user * the user to set */ public void setUser(String user) { this.user = user; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } /* 发送邮件时是否进行身份验证 */ private boolean authFlag = true; /* 发送邮件是否需要代理 */ private boolean proxyFlag = false; /* 其他设置 */ private boolean restFlag = false; /** * 设置邮件发信人、收信人、抄送、密送、邮件标题、邮件内容、附件的文件名称(包括路径) 同时生成Session、Message,并且调用发送邮件 */ public synchronized int sendEmailNow(String to,String cc,String subject, String body, String attchFile) { //System.out.println("hello"); Proper(); int successFlag = 0; int m_result; try { Properties props = System.getProperties(); // Properties用来将props传给MAIL Session session = null; // 创建Session对象 props.put("mail.smtp.host", sendHost); if (authFlag) { props.put("mail.smtp.auth", "true"); } else { props.put("mail.smtp.auth", ""); } session = Session.getInstance(props, null); if (restFlag) { session.setDebug(true); } /** 创建邮件MESSAGE */ Message mesg = new MimeMessage(session); InternetAddress[] addresses; /** from 地址列表 */ if (!from.equals("")) { mesg.setFrom(new InternetAddress(from)); } else { System.out.println("请填写发信人地址"); return -2; } /** TO地址列表 */ if (!to.equals("")) { addresses = InternetAddress.parse(to); mesg.setRecipients(javax.mail.Message.RecipientType.TO, addresses); // mesg.setr //mesg.setr } else { System.out.println("请填写收信人地址"); return -3; } /** CC地址列表 抄送*/ if (cc!=null&&!cc.equals("")) { //wlog.userLog(cc); addresses = InternetAddress.parse(cc); mesg.setRecipients(javax.mail.Message.RecipientType.CC, addresses); } /** 设置邮件主题topic */ if (!subject.equals("")) { mesg.setSubject(subject); } else { mesg.setSubject(subject); System.out.println("请填写邮件主题"); } /** 设置邮件内容 */ if (attchFile==null || attchFile.equals("")) { mesg.setContent(body, "text/html;charset=GBK"); //mesg.setText(body); } else { Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent(body, "text/html;charset=GBK"); mp.addBodyPart(mbp); /** 添加附件 */ ArrayList attachlist = tokenize(attchFile); for (int i = 0; i < attachlist.size(); i++) { MimeBodyPart attachMbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource( (String) (attachlist.get(i))); attachMbp.setDataHandler(new DataHandler(fds)); attachMbp.setFileName(MimeUtility.encodeText(fds.getName(), "GBK", "B")); attachMbp.setHeader("Content-Type", fds.getContentType()); mp.addBodyPart(attachMbp); } mesg.setContent(mp); } /** 设置邮件发送时间 */ mesg.setSentDate(new Date()); /** 发送邮件 */ if (authFlag) { Transport tr = session.getTransport("smtp"); tr.connect(sendHost, user, password); mesg.saveChanges(); tr.sendMessage(mesg, mesg.getAllRecipients()); tr.close(); } else { Transport.send(mesg); } }catch (MessagingException e) {/* send发送邮件的异常处理 */ System.out.println("邮件发送失败!"); successFlag=-9; e.printStackTrace(); } catch (Exception e) { successFlag=-9; e.printStackTrace(); } return successFlag; } private ArrayList tokenize(String s) { ArrayList arrayl = new ArrayList(); for (StringTokenizer stringtok = new StringTokenizer(s, ","); stringtok .hasMoreTokens(); arrayl.add(stringtok.nextToken().trim())) ; return arrayl; } String sendProtocol = "smtp"; String receiveProtocol = "pop3"; /** * 覆盖方法 */ public boolean sendEmail(String to,String cc, String subject, String body, String attchFile) { int successflag=sendEmailNow(to,cc,subject,body,attchFile); if(successflag==0){ return true; }else{ return false; } } } 配置文件: emailserver.properties: emailserver.sendHost=//邮箱服务器,比如163的:smtp.163.com emailserver.receiveHost=//不填 emailserver.user=//发送邮件的用户名,该邮箱必须是emailserver.sendHost的邮箱 emailserver.password=//发送邮件的密码 emailserver.from=//同emailserver.user 发邮件需要的jar包: activation.jar,mail.jar
可能出现的异常:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/sun/mail/util/LineInputStreams
原因是jar包版本不统一,解决方法如下:
删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.
具体方法如下:
在Myeclipse6.0.1中用rar打开X:/Program Files/MyEclipse 6.0/
myeclipse/eclipse/plugins/
com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/
data/libraryset/EE_5/
javaee.jar
在Myeclipse8.5中 D:/Program Files/MyEclipse 8.5/
Common/plugins/
com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033/
data/libraryset/EE_5/EE_5/javaee.jar ,然后删除mail,一切就ok了.
若删除的时候出错,则需要先在myeclipse里面删除掉引用了J2ee5.0的工程,
然后再关闭myeclipse再进行删除操作。
相关推荐
java发送电子邮件,程序和jar包都在里面,使用方法也有,程序可直接使用,直接改成自己的邮箱和密码就能发送了,解决了很多人java发送邮件不成功的烦恼
java 发邮件带excel附件,以流的形式发送附件,不生成excel文件
java发邮件
填写发件人邮箱地址和收件人邮箱地址即可发送邮件,方便快捷,供广大开发人员使用!
java发邮件.zip java发邮件.zip java发邮件.zip java发邮件.zip
java发邮件
用javaMail实现 发邮件 以前遇到好多的发件人不能确定问题 都解决了
java发邮件相关jar包,outlook or notes
通过实例,用java发邮件,保证秒懂, Xmail xmail = new Xmail(); xmail.setUsername("/////@163.com"); //设置发件人邮箱名字 xmail.setPassword("*******"); //邮箱密码 xmail.setHost("smtp.163.com"); //...
java发邮件程序java发邮件程序java发邮件程序java发邮件程序
java发邮件
当需要自动发邮件到制定的mail地址时,我们需要这4个jar包 注:如果jar包引用不当,则依然不能发邮件 使用步骤:1、先将jar包放入到ant%home%下的lib文件夹下,比如我的ant%home%是:D:\apache-ant-1.7.0\lib 2、...
java发邮件用到的jar包
java 发邮件工具类 多收件人 多抄送人 多附件 内有三个重载方法 使用方便
java发邮件所需架包 包含了activation和mail两个架包
java 发邮件项目,代码 测试运行正常。
java发邮件,可以带附件.使用了commons-email-1.0.jar包.
Java EMail Java发邮件的依赖库
java 发邮件jar包 jsp中也可以用,很多人都在网上找,
Java 发邮件教程