java制作简易邮箱代码 java制作简易邮箱代码怎么写( 五 )


.getMsg("reg.authorid.exist"));
errors.add("org.apache.struts.action.GLOBAL_MESSAGE", error);
saveErrors(request, errors);
return mapping.getInputForward();
}
String firstname = authorActionForm.getString("firstname");
String lastname = authorActionForm.getString("lastname");
String title = authorActionForm.getString("title");
String email = authorActionForm.getString("email");
String phone = authorActionForm.getString("phone");
String fax = authorActionForm.getString("fax");
String company = authorActionForm.getString("company");
String department = authorActionForm.getString("department");
String address = authorActionForm.getString("address");
String city = authorActionForm.getString("city");
String country = authorActionForm.getString("country");
String postalcode = authorActionForm.getString("postalcode");
// 密码自动生成;
StringBuffer password = new StringBuffer();
Random a = new Random();
for (int i = 0; i8; i++) {
password.append(a.nextInt(9));
}
String isValid = "f";
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat fat = new SimpleDateFormat("yyyy-MM-dd");
String str = fat.format(date);
AuthorsBean author = new AuthorsBean();
author.setAuthorid(authorid);
author.setPassword(password.toString());
author.setFirstname(firstname);
author.setLastname(lastname);
author.setTitle(title);
author.setEmail(email);
author.setPhone(phone);
author.setFax(fax);
author.setCompany(company);
author.setDepartment(department);
author.setAddress(address);
author.setCity(city);
author.setCountry(country);
author.setPostalcode(postalcode);
author.setRegTime(Date.valueOf(str));
author.setIsValid(isValid);
int retVal = dao.insertAuthor(author);
log.debug("--------------------retVal=" + retVal);
if (retVal == 1) {
// 将获得的Password发送动用户邮箱中
Properties props = new Properties();
props.put("mail.smtp.host", CISConfig.getMsg("email.smtpserver"));
props.put("mail.smtp.auth", CISConfig
.getMsg("email.smtp.isAuthenticate"));
props.put("mail.smtp.user", CISConfig.getMsg("email.user"));
props.put("mail.smtp.from", CISConfig.getMsg("email.from"));
props.put("mail.debug", "true");
MyAuthenticator auth = new MyAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
Message msg = new MimeMessage(session);
try {
// msg.setFrom(new
// InternetAddress(CISConfig.getMsg("email.from")));
InternetAddress[] addresses = { new InternetAddress(email) };
msg.setRecipients(Message.RecipientType.TO, addresses);
msg.setSentDate(Date.valueOf(str));
// 设置邮件的标题
String subject =CISConfig.getMsg("reg.mail.subject");
msg.setSubject(subject);
// 设置邮件的内容
String msgtext = "Congratulate to you,\n you are successfully registration in CIS System.Your Reviewer ID is:"
+ authorid
+ "and password is:"
+ password
+ ".\n We will auditing your infomation in 24 hours and send a message to you for telling you the resultset!! \nThen you can access URL:\n\nThanks.";// CISConfig.getMsg("reg.mail.text");
msg.setText(msgtext);
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
return mapping.findForward("success");
} else {
//此处不能这样返回!要修改!
return mapping.findForward("success");
}
}
}
其中上面的一些参数是在配置文件里写的!你可以直接写,不用配置文件!
reg.mail.subject=This is CIS System send to your infomation!
email.smtpserver=smtp.126.com

推荐阅读