Skip to content

Commit

Permalink
[nasuf]update sending email with mongo query result
Browse files Browse the repository at this point in the history
  • Loading branch information
nasuf committed May 14, 2016
1 parent 1e4bdda commit a0e4ad2
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.kindlepocket.cms;

import java.net.UnknownHostException;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;

@Configuration
@EnableMongoRepositories
public class MongoConfig {

private static Logger logger = Logger.getLogger(MongoConfig.class);

@Bean
public MongoClient client() {
MongoClient client = null;
try {
client = new MongoClient(new ServerAddress("120.25.224.11", 27017));
} catch (UnknownHostException e) {
if (logger.isErrorEnabled()) {
logger.error("unKnownHost!", e);
}
}
return client;
}

@Bean
public MongoDbFactory mongoDbFactory() {
String database = new MongoClientURI("mongodb://120.25.224.11/test").getDatabase();
return new SimpleMongoDbFactory(client(), database);
}

@Bean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
return new MongoTemplate(mongoDbFactory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kindlepocket.cms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kindlepocket.cms.pojo.Item;
import com.kindlepocket.cms.service.BookRepository;
import com.kindlepocket.cms.service.MailService;

@RestController
@RequestMapping("/bookManage")
public class BookManagementController {

@Autowired
private BookRepository bookRepository;

@Autowired
private MailService mailService;

@RequestMapping("/insert")
public void testInsert() {
System.out.println("inserting............");
List<Item> items = new ArrayList<Item>();
for (int i = 400; i < 450; i++) {
Item item = new Item();
item.setId((long) i);
item.setAuthor("nasuf_" + i);
item.setTitle("ephemeris_No." + i);
item.setUploadDate(new Date().getTime());
items.add(item);
}
this.bookRepository.insert(items);
}

@RequestMapping(value = "/findall", method = RequestMethod.GET)
public ResponseEntity<String> testFind(@RequestParam(value = "title") String title) {
title = title.toString();
System.out.println("title:" + title);
Item book = this.bookRepository.findByTitle(title);
System.out.println("book:" + book);
this.mailService.sendMail(book.getTitle());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Email Send Successfully!");
// System.out.println(book.getAuthor());
// List<Item> books = this.bookRepository.findAll();
// for (Item item : books) {
// System.out.println(item);
// }
}

@RequestMapping("/revome")
public void testRemove() {
System.out.println("removing...");
this.bookRepository.deleteAll();
System.out.println("removment finished!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kindlepocket.cms.service;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Component;

import com.kindlepocket.cms.pojo.Item;

@Component
public interface BookRepository extends MongoRepository<Item, String> {

Item findByAuthor(String author);

Item findByTitle(String title);

/*
* @Query("{'age':?0") List<Item> withQueryFindByAge(Integer age);
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.kindlepocket.cms.service;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class MailService {

private static Logger logger = Logger.getLogger(MailService.class);

public static void sendMail(String title) {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.163.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
// 使用JavaMail发送邮件的5个步骤
// 1、创建session
Session session = Session.getInstance(prop);
// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
try {
// 2、通过session得到transport对象
Transport ts = session.getTransport();
// 3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect("smtp.163.com", "binglingxueyou1031", "blxyst103166");
// 4、创建邮件
Message message = createSimpleMail(session, title + "'s book",
"please do not response this email!");
// 5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
logger.info("email send successfully");
ts.close();
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("email send failed!", e);
}
}
}

public static MimeMessage createSimpleMail(Session session, String subject, String content)
throws Exception {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人
message.setFrom(new InternetAddress("[email protected]"));
// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// 邮件的标题
message.setSubject(subject);
// 邮件的文本内容
message.setContent(content, "text/html;charset=UTF-8");
// 返回创建好的邮件对象
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ public static String initPicTextMessage(String toUserName, String fromUserName,
picText.setTitle(title);
picText.setDescription("kindle text books sharing platform");
picText.setPicUrl("http://33051bbe.nat123.net/Weixin/WEB-INF/imgs/welcome.jpg");
picText.setUrl("www.nasuf.cn");
picText.setUrl("http://44055713.nat123.net/bookManage/findall?title=" + title);
picText.setUrl(picText.getUrl().replace("\"", ""));
System.out.println("url:" + picText.getUrl());
picTextList.add(picText);
}
} else {
Expand Down

0 comments on commit a0e4ad2

Please sign in to comment.