问题描述
数据库连接,取出服务器,端口,发送邮箱,有户名,和密码importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importcom.mail.model.Sender;importcom.mail.tool.DB;publicclassSenderDao{publicstaticArrayListgetAllSender(){ArrayList<Sender>list=newArrayList();Connectionconn=null;PreparedStatementpst=null;ResultSetrs=null;conn=DB.getConn();Stringsql="selectsServerAdd,sPort,sUserName,sPassword,sAddressfromSender";try{pst=conn.prepareStatement(sql);rs=pst.executeQuery();while(rs.next()){Senders=newSender();s.setsServerAdd(rs.getString("sServerAdd"));s.setsPort(rs.getInt("sPort"));s.setsUserName(rs.getString("sUserName"));s.setsPassWord(rs.getString("sPassword"));s.setsAddress(rs.getString("sAddress"));list.add(s);}}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{DB.close(rs);DB.close(pst);DB.close(conn);}returnlist;}}发送方法publicclassSendMethod{privateStringhost="";privateStringuser="";privateStringpassword="";privateStringaddress="";privateintport=0;publicvoidsetHost(Stringhost){this.host=host;}publicvoidsetPort(intport){this.port=port;}publicvoidsetAccount(Stringuser,Stringpassword){this.user=user;this.password=password;}publicvoidsend(Stringfrom,Stringto,Stringsubject,Stringcontent){Propertiesprops=newProperties();props.put("mail.smtp.host","host");props.put("mail.smtp.auth","true");Sessionsession=Session.getInstance(props,null);session.setDebug(true);MimeMessagemessage=newMimeMessage(session);try{message.setFrom(newInternetAddress(from));message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));message.setSubject(subject);message.setText(content);message.saveChanges();Transporttransport=session.getTransport("smtp");transport.connect(host,user,password);transport.sendMessage(message,message.getAllRecipients());transport.close();}catch(AddressExceptione){e.printStackTrace();}catch(MessagingExceptione){e.printStackTrace();}}}主方法publicclassJavaMain{publicstaticvoidmain(Stringargs[]){Msgmultisengms=newMsgmultiseng();ms.setHost("smtp.163.com");//指定使用的邮箱服务器,从数据库提取。ms.setAccount("rencheng850908","aa123456");//指定使用邮箱的用户名和密码//从数据库里,提取用户名和密码ms.send("rencheng850908@163.com","rencheng850908@sina.com,517018071@qq.com,rencheng850908@163.com,rencheng850908@sohu.com","java测试","成功了");//发送地址,是从文本文件中读取来。}}
解决方案
解决方案二:
貌似有开源框架吧