| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -104,7 +104,7 @@ public Result register(@PathVariable("email") String email, @PathVariable("passw | |||
| 104 | 104 | return new Result(Err.USER_REPEAT.getCode(), Err.USER_REPEAT.getMessage()); | |
| 105 | 105 | } | |
| 106 | 106 | String redirectValidateUrl = MailUtil.redirectValidateUrl(email, password); | |
| 107 | - MailUtil.send(email, redirectValidateUrl); | ||
| 107 | + MailUtil.send(email, "帐号激活邮件", redirectValidateUrl); | ||
| 108 | 108 | return new Result(redirectValidateUrl); | |
| 109 | 109 | } | |
| 110 | 110 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,8 @@ | |||
| 3 | 3 | import javax.mail.*; | |
| 4 | 4 | import javax.mail.internet.InternetAddress; | |
| 5 | 5 | import javax.mail.internet.MimeMessage; | |
| 6 | + import java.io.FileInputStream; | ||
| 7 | + import java.io.IOException; | ||
| 6 | 8 | import java.util.Date; | |
| 7 | 9 | import java.util.Properties; | |
| 8 | 10 | ||
@@ -21,46 +23,41 @@ | |||
| 21 | 23 | * @Copyright(©) 2015 by xiaomo. | |
| 22 | 24 | **/ | |
| 23 | 25 | public class MailUtil { | |
| 24 | - private static final String HOST = "smtp.xiaomo.info"; | ||
| 25 | - private static final String PROTOCOL = "smtp"; | ||
| 26 | - private static final int PORT = 25; | ||
| 27 | - private static final String FROM = "website@xiaomo.info";//发件人的email | ||
| 28 | - private static final String PWD = "Xiaomo123";//发件人密码 | ||
| 26 | + private static String USERNAME; | ||
| 27 | + private static String PASSWORD; | ||
| 29 | 28 | ||
| 30 | 29 | /** | |
| 31 | 30 | * 获取Session | |
| 32 | 31 | */ | |
| 33 | - private static Session getSession() { | ||
| 32 | + private static Session getSession() throws IOException { | ||
| 34 | 33 | Properties props = new Properties(); | |
| 35 | - props.put("mail.smtp.host", HOST);//设置服务器地址 | ||
| 36 | - props.put("mail.store.protocol", PROTOCOL);//设置协议 | ||
| 37 | - props.put("mail.smtp.port", PORT);//设置端口 | ||
| 38 | - props.put("mail.user", FROM); | ||
| 39 | - props.put("mail.password", PWD); | ||
| 40 | - props.put("mail.smtp.auth", true); | ||
| 41 | - | ||
| 34 | + String dir = System.getProperty("user.dir"); | ||
| 35 | + FileInputStream is = new FileInputStream(dir + "/website/src/main/resources/config/application.properties"); | ||
| 36 | + props.load(is); | ||
| 37 | + USERNAME = String.valueOf(props.get("mail.username")); | ||
| 38 | + PASSWORD = String.valueOf(props.get("mail.password")); | ||
| 42 | 39 | Authenticator authenticator = new Authenticator() { | |
| 43 | 40 | @Override | |
| 44 | 41 | protected PasswordAuthentication getPasswordAuthentication() { | |
| 45 | - return new PasswordAuthentication(FROM, PWD); | ||
| 42 | + return new PasswordAuthentication(USERNAME, PASSWORD); | ||
| 46 | 43 | } | |
| 47 | - | ||
| 48 | 44 | }; | |
| 49 | 45 | return Session.getDefaultInstance(props, authenticator); | |
| 50 | 46 | } | |
| 51 | 47 | ||
| 52 | - public static void send(String toEmail, String content) { | ||
| 53 | - Session session = getSession(); | ||
| 48 | + public static void send(String toEmail, String subject, String content) { | ||
| 49 | + Session session; | ||
| 54 | 50 | try { | |
| 51 | + session = getSession(); | ||
| 55 | 52 | Message msg = new MimeMessage(session); | |
| 56 | - msg.setFrom(new InternetAddress(FROM)); | ||
| 53 | + msg.setFrom(new InternetAddress(USERNAME)); | ||
| 57 | 54 | InternetAddress[] address = {new InternetAddress(toEmail)}; | |
| 58 | 55 | msg.setRecipients(Message.RecipientType.TO, address); | |
| 59 | - msg.setSubject("账号激活邮件"); | ||
| 56 | + msg.setSubject(subject); | ||
| 60 | 57 | msg.setSentDate(new Date()); | |
| 61 | 58 | msg.setContent(content, "text/html;charset=utf-8"); | |
| 62 | 59 | Transport.send(msg); | |
| 63 | - } catch (MessagingException mex) { | ||
| 60 | + } catch (Exception mex) { | ||
| 64 | 61 | mex.printStackTrace(); | |
| 65 | 62 | } | |
| 66 | 63 | } | |
@@ -69,10 +66,9 @@ public static void send(String toEmail, String content) { | |||
| 69 | 66 | * 返回激活链接 | |
| 70 | 67 | * | |
| 71 | 68 | * @param email email | |
| 72 | - * @return | ||
| 73 | - * 有4个参数 email password validateCode time | ||
| 69 | + * @return 有4个参数 email password validateCode time | ||
| 74 | 70 | */ | |
| 75 | - public static String redirectValidateUrl(String email,String password) { | ||
| 71 | + public static String redirectValidateUrl(String email, String password) { | ||
| 76 | 72 | Long now = DateUtil.getNowOfMills(); | |
| 77 | 73 | StringBuilder sb = new StringBuilder("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!</br>"); | |
| 78 | 74 | sb.append("<a href=\"http://xiaomo.info/validate.html?email="); | |
@@ -101,6 +97,6 @@ public static String redirectValidateUrl(String email,String password) { | |||
| 101 | 97 | } | |
| 102 | 98 | ||
| 103 | 99 | public static void main(String[] args) { | |
| 104 | - System.out.println(redirectValidateUrl("83387856@qq.com","123456")); | ||
| 100 | + System.out.println(redirectValidateUrl("83387856@qq.com", "123456")); | ||
| 105 | 101 | } | |
| 106 | 102 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,9 @@ | |||
| 1 | 1 | package info.xiaomo.website.controller; | |
| 2 | 2 | ||
| 3 | - import info.xiaomo.core.controller.Result; | ||
| 4 | - import org.springframework.beans.factory.annotation.Autowired; | ||
| 5 | - import org.springframework.mail.MailSender; | ||
| 6 | - import org.springframework.mail.SimpleMailMessage; | ||
| 7 | - import org.springframework.stereotype.Controller; | ||
| 3 | + import info.xiaomo.core.untils.MailUtil; | ||
| 8 | 4 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 9 | 5 | import org.springframework.web.bind.annotation.RequestMethod; | |
| 6 | + import org.springframework.web.bind.annotation.RestController; | ||
| 10 | 7 | ||
| 11 | 8 | /** | |
| 12 | 9 | * 把今天最好的表现当作明天最新的起点..~ | |
@@ -23,26 +20,13 @@ | |||
| 23 | 20 | * @Copyright(©) 2015 by xiaomo. | |
| 24 | 21 | **/ | |
| 25 | 22 | ||
| 26 | - @Controller | ||
| 23 | + @RestController | ||
| 27 | 24 | @RequestMapping("/mail") | |
| 28 | 25 | public class MailController { | |
| 29 | 26 | ||
| 30 | - private final MailSender mailSender; | ||
| 31 | - | ||
| 32 | - @Autowired | ||
| 33 | - public MailController(MailSender mailSender) { | ||
| 34 | - this.mailSender = mailSender; | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | 27 | @RequestMapping(value = "/send", method = RequestMethod.GET) | |
| 38 | - public Result sendMail() { | ||
| 39 | - SimpleMailMessage message = new SimpleMailMessage(); | ||
| 40 | - message.setFrom("83387856@qq.com"); | ||
| 41 | - message.setTo("83387856@qq.com"); | ||
| 42 | - message.setSubject("主题:简单邮件"); | ||
| 43 | - message.setText("测试邮件内容"); | ||
| 44 | - mailSender.send(message); | ||
| 45 | - return new Result(message); | ||
| 28 | + public void sendMail() { | ||
| 29 | + MailUtil.send("hupengbest@163.com", "测试邮件是否正常", "测试"); | ||
| 46 | 30 | } | |
| 47 | 31 | ||
| 48 | 32 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,13 +39,12 @@ spring.redis.pool.min-idle=0 | |||
| 39 | 39 | spring.redis.timeout=0 | |
| 40 | 40 | ||
| 41 | 41 | #\u90AE\u4EF6 | |
| 42 | - spring.mail.host=smtp.qq.com | ||
| 43 | - spring.mail.username=83387856@qq.com | ||
| 44 | - spring.mail.password=Lm199301161127 | ||
| 45 | - spring.mail.properties.mail.smtp.auth=true | ||
| 46 | - spring.mail.properties.mail.smtp.starttls.enable=true | ||
| 47 | - spring.mail.properties.mail.smtp.starttls.required=true | ||
| 48 | - | ||
| 42 | + mail.host=smtp.163.com | ||
| 43 | + mail.username=xiaomotest@163.com | ||
| 44 | + mail.password=Xiaomo123456 | ||
| 45 | + mail.smtp.auth=true | ||
| 46 | + mail.port=25 | ||
| 47 | + mail.protocol=smtp | ||
| 49 | 48 | ||
| 50 | 49 | # DEVTOOLS (DevToolsProperties) | |
| 51 | 50 | spring.devtools.restart.additional-exclude=/** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments