ofbiz email发送配置及相关代码导读

ofbiz email发送配置及相关代码导读

ofibz登陆功能有通过电子邮件找会密码的功能,香港虚拟主机,网站空间,但找回密码功能需要配置一个发送email的邮箱账号和smtp服务器的配置,具体配置如下:

1:在ofbiz数据库的表product_store_email_settings中找到from_address字段,将该字段中值全部修改成配置的默认发送账号

2:在general.properties中配置smtp参数

# — The default domainname used in the notification emails links# as ‘baseUrl’ and ‘baseSecureUrl’ are set in the url.properties file.

# — mail notifications enabled (Y|N)mail.notifications.enabled=Y

# — redirect all mail notifications to this address for testing#mail.notifications.redirectTo=

# — the default mail server to usemail.smtp.relay.host=smtp.126.com

# — SMTP Auth settingsmail.smtp.auth.user=XXXXXXXX———————-(邮箱名称)mail.smtp.auth.password=XXXXXXXXXXXXXXX——(邮箱密码)

# — Additional Required Fields needed for Gmail and other non traditional smtp servers# — These added fields also work for Yahoo business mail for instance# — Gmail smtp port can be either 465 or 587mail.smtp.port=25# — Gmail requires StartTLSmail.smtp.starttls.enable=true

# — Gmail requires a JSSE socket factory, the following socketFactory settings will override JavaMail’s default socketFactory settings# — Port needs to be the same as mail.smtp.portmail.smtp.socketFactory.port=25#mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory#–Fallback [true|false] determines whether you will allow a non secure connection if you are unable to get a secure one#mail.smtp.socketFactory.fallback=false

3:配置完成后需要重启ofbiz,美国空间,配置才能生效,再次登录找回密码就能收到一封邮件

4:找回密码的相关代码导读

在controller.xml文件中可以找到找回密码对应的实现:

可以看到具体实现在org.ofbiz.securityext.login.LoginEvents类中的forgotPassword方法实现

打开在LoginEvents中看到具体调用到一下代码

public static String emailPassword(HttpServletRequest request, HttpServletResponse response) {String defaultScreenLocation = “component://securityext/widget/EmailSecurityScreens.xml#PasswordEmail”;Delegator delegator = (Delegator) request.getAttribute(“delegator”);LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute(“dispatcher”);String productStoreId = ProductStoreWorker.getProductStoreId(request);String errMsg = null;Map<String, String> subjectData = FastMap.newInstance();subjectData.put(“productStoreId”, productStoreId);boolean useEncryption = “true”.equals(UtilProperties.getPropertyValue(“security.properties”, “password.encrypt”));String userLoginId = request.getParameter(“USERNAME”);subjectData.put(“userLoginId”, userLoginId);if ((userLoginId != null) && (“true”.equals(UtilProperties.getPropertyValue(“security.properties”, “username.lowercase”)))) {userLoginId = userLoginId.toLowerCase();}if (!UtilValidate.isNotEmpty(userLoginId)) {// the password was incompleteerrMsg = UtilProperties.getMessage(resource, “loginevents.username_was_empty_reenter”, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}GenericValue supposedUserLogin = null;String passwordToSend = null;try {supposedUserLogin = delegator.findOne(“UserLogin”, false, “userLoginId”, userLoginId);if (supposedUserLogin == null) {// the Username was not founderrMsg = UtilProperties.getMessage(resource, “loginevents.username_not_found_reenter”, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}if (useEncryption) {// password encrypted, can’t send, generate new password and email to userpasswordToSend = RandomStringUtils.randomAlphanumeric(Integer.parseInt(UtilProperties.getPropertyValue(“security”, “password.length.min”, “5”)));supposedUserLogin.set(“currentPassword”, HashCrypt.getDigestHash(passwordToSend, LoginServices.getHashType()));supposedUserLogin.set(“passwordHint”, “Auto-Generated Password”);if (“true”.equals(UtilProperties.getPropertyValue(“security.properties”, “password.email_password.require_password_change”))){supposedUserLogin.set(“requirePasswordChange”, “Y”);}} else {passwordToSend = supposedUserLogin.getString(“currentPassword”);}} catch (GenericEntityException e) {Debug.logWarning(e, “”, module);Map<String, String> messageMap = UtilMisc.toMap(“errorMessage”, e.toString());errMsg = UtilProperties.getMessage(resource, “loginevents.error_accessing_password”, messageMap, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}if (supposedUserLogin == null) {// the Username was not foundMap<String, String> messageMap = UtilMisc.toMap(“userLoginId”, userLoginId);errMsg = UtilProperties.getMessage(resource, “loginevents.user_with_the_username_not_found”, messageMap, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}StringBuilder emails = new StringBuilder();GenericValue party = null;try {party = supposedUserLogin.getRelatedOne(“Party”);} catch (GenericEntityException e) {Debug.logWarning(e, “”, module);party = null;}if (party != null) {Iterator<GenericValue> emailIter = UtilMisc.toIterator(ContactHelper.getContactMechByPurpose(party, “PRIMARY_EMAIL”, false));while (emailIter != null && emailIter.hasNext()) {GenericValue email = emailIter.next();emails.append(emails.length() > 0 ? “,” : “”).append(email.getString(“infoString”));}}if (!UtilValidate.isNotEmpty(emails.toString())) {// the Username was not founderrMsg = UtilProperties.getMessage(resource, “loginevents.no_primary_email_address_set_contact_customer_service”, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}//前面主要是从request中获取一些配置参数// get the ProductStore email settingsGenericValue productStoreEmail = null;try {productStoreEmail = delegator.findOne(“ProductStoreEmailSetting”, false, “productStoreId”, productStoreId, “emailType”, “PRDS_PWD_RETRIEVE”);//从Productstoreemaisetting中获取发送email的账号} catch (GenericEntityException e) {Debug.logError(e, “Problem getting ProductStoreEmailSetting”, module);}if (productStoreEmail == null) {errMsg = UtilProperties.getMessage(resource, “loginevents.problems_with_configuration_contact_customer_service”, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}String bodyScreenLocation = productStoreEmail.getString(“bodyScreenLocation”);if (UtilValidate.isEmpty(bodyScreenLocation)) {bodyScreenLocation = defaultScreenLocation;}// set the needed variables in new contextMap<String, Object> bodyParameters = FastMap.newInstance();bodyParameters.put(“useEncryption”, Boolean.valueOf(useEncryption));bodyParameters.put(“password”, UtilFormatOut.checkNull(passwordToSend));bodyParameters.put(“locale”, UtilHttp.getLocale(request));bodyParameters.put(“userLogin”, supposedUserLogin);bodyParameters.put(“productStoreId”, productStoreId);Map<String, Object> serviceContext = FastMap.newInstance();serviceContext.put(“bodyScreenUri”, bodyScreenLocation);serviceContext.put(“bodyParameters”, bodyParameters);serviceContext.put(“subject”, productStoreEmail.getString(“subject”));serviceContext.put(“sendFrom”, productStoreEmail.get(“fromAddress”));serviceContext.put(“sendCc”, productStoreEmail.get(“ccAddress”));serviceContext.put(“sendBcc”, productStoreEmail.get(“bccAddress”));serviceContext.put(“contentType”, productStoreEmail.get(“contentType”));serviceContext.put(“sendTo”, emails.toString());serviceContext.put(“partyId”, party.getString(“partyId”));try {Map<String, Object> result = dispatcher.runSync(“sendMailFromScreen”, serviceContext);if (ModelService.RESPOND_ERROR.equals(result.get(ModelService.RESPONSE_MESSAGE))) {Map<String, Object> messageMap = UtilMisc.toMap(“errorMessage”, result.get(ModelService.ERROR_MESSAGE));errMsg = UtilProperties.getMessage(resource, “loginevents.error_unable_email_password_contact_customer_service_errorwas”, messageMap, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}} catch (GenericServiceException e) {Debug.logWarning(e, “”, module);errMsg = UtilProperties.getMessage(resource, “loginevents.error_unable_email_password_contact_customer_service”, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;} (useEncryption) {try {supposedUserLogin.store();} catch (GenericEntityException e) {Debug.logWarning(e, “”, module);Map<String, String> messageMap = UtilMisc.toMap(“errorMessage”, e.toString());errMsg = UtilProperties.getMessage(resource, “loginevents.error_saving_new_password_email_not_correct_password”, messageMap, UtilHttp.getLocale(request));request.setAttribute(“_ERROR_MESSAGE_”, errMsg);return “error”;}}if (useEncryption) {errMsg = UtilProperties.getMessage(resource, “loginevents.new_password_createdandsent_check_email”, UtilHttp.getLocale(request));request.setAttribute(“_EVENT_MESSAGE_”, errMsg);} else {errMsg = UtilProperties.getMessage(resource, “loginevents.new_password_sent_check_email”, UtilHttp.getLocale(request));request.setAttribute(“_EVENT_MESSAGE_”, errMsg);}return “success”;}

email的发送过程在framework\common\src\org\ofbiz\common\email\EmailServices.java中实现

当你成功得意的时候,最重要的是瞧得起别人。

ofbiz email发送配置及相关代码导读

相关文章:

你感兴趣的文章:

标签云: