velocity +mybatis+ springMvc构建邮件服务器知识总结
1.在controller中传值到页面
(1)List<String> 类型
List<String> servers = null ; //…………………… mv.addObject("servers", servers);
<select id="serverSel" name="server" onchange="changeServer()"> #foreach ($server in $servers) <option value="$server" >$server</option> #end </select>
(2)List<Bean>类型
List<User> qqList = null ; //…………………… mv.addObject("qqEmailList", qqList);
#foreach ($qe in $qqEmailList) <input type="checkbox" name="userEmail" value="$qe.userId">$qe.userEmail<br /> #end
2.在controller中不跳转页面直接跳转下一个方法
mView.setViewName("redirect:sendEmail.do");
return mView;
或者如下方法:
return new RedirectView("tasks.do");
3.用spring自带的JavaMailSenderImpl类和MimeMessageHelper类发送可以包含html内容的邮件
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); //设置邮件服务器主机和端口 mailSender.setHost(serverBean.getHost()); mailSender.setPort(Integer.valueOf(serverBean.getPort())); MimeMessage mailMessage = mailSender.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8"); //设置收件人 messageHelper.setTo(toEmail); //设置发件人以及发件人名字 messageHelper.setFrom(serverBean.getFromEmail(), "EquipOne"); //设置邮件标题 messageHelper.setSubject(subject); //设置邮件内容,以及是否允许为html代码 messageHelper.setText(content, true); //构建properties类,存放SMTP服务器参数 Properties prop = new Properties(); prop.put("mail.smtp.auth", "true"); prop.put("mail.smtp.timeout", "250000"); final String from = serverBean.getFromEmail(); final String password = serverBean.getPassword(); //创建账号密码验证器,验证邮件服务器登陆账号 Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }; //Session.getInstance(prop, auth)根据邮件会话属性和密码验证器构造一个发//送邮件的session,并放入JavaMailSenderImpl类 mailSender.setSession(Session.getInstance(prop, auth)); mailSender.send(mailMessage);
4.mybatis插入语句返回所插入的主键的值,加上(useGeneratedKeys="true" keyProperty="对应表字段的类属性")
<resultMap type="EmailDetail" id="emailDetail"> <result property="emailId" column="email_id" /> <result property="emailContent" column="email_content" /> <result property="emailSubject" column="email_subject" /> <result property="emailStatus" column="email_status" /> </resultMap> <insert id="addData" parameterType="com.netcloud.mail.entity.EmailDetail" useGeneratedKeys="true" keyProperty="emailId"> insert into t_email_detail(email_content,email_subject,email_status) values(#{emailContent},#{emailSubject},#{emailStatus}) </insert>