테이블에서 내용 message 지웠다
CREATE TABLE TBL_EMAIL
(
IDX NUMBER,
SENDERNAME VARCHAR2 (100),
SENDERMAIL VARCHAR2 (100),
RECEIVEMAIL VARCHAR2 (100),
SUBJECT VARCHAR2 (100),
REGDATE DATE DEFAULT SYSDATE
)
NOCACHE
LOGGING;
VO
@Data
public class AdminEmailVO {
private Integer idx;
private String senderName;
private String senderMail;
private String receiveMail;
private String subject;
private String message;
private Date regdate;
private MultipartFile files;
}
핵심 부분 서비스 영역
@Service
public class AdminEmailServiceImpl implements AdminEmailService{
private final static Logger logger =LoggerFactory.getLogger(AdminEmailServiceImpl.class);
@Inject
private AdminEmailDAO adminEmailDAO;
@Inject
private JavaMailSender javaMailSender;
@Override
public void senderEmail(AdminEmailVO vo , HttpServletRequest request) {
MimeMessage message =javaMailSender.createMimeMessage();
try{
//true 이면 multipart 여부
MimeMessageHelper messageHelper=new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setSubject(vo.getSubject());
messageHelper.setText(vo.getMessage(), true); //true 면 html 형식
messageHelper.setFrom(vo.getSenderMail(), "운영자");
messageHelper.setTo(new InternetAddress(vo.getReceiveMail(), "utf-8"));
String filePath =UploadPath.path(request);
//첨부파일이 있는 경우
if(vo.getFiles().getOriginalFilename()!=null ){
logger.info("첨부파일이 있는 경우"+ vo.getFiles().getOriginalFilename());
//서버에 임시 저장 을 한다.
File target =new File(filePath, vo.getFiles().getOriginalFilename());
FileCopyUtils.copy(vo.getFiles().getBytes(), target);
//서버에 저장 된 파일을 보낸다.
DataSource dataSource =new FileDataSource(filePath+vo.getFiles().getOriginalFilename());
messageHelper.addAttachment(MimeUtility.encodeText(
vo.getFiles().getOriginalFilename(), "utf-8", "B"
), dataSource);
//메일 보내기
javaMailSender.send(message);
//서버에 저장된 파일을 삭제 한다.
File file =new File(filePath+vo.getFiles().getOriginalFilename());
if(file.exists()){
file.delete();
logger.info("첨부 파일 삭제 완료");
}
}else{
//첨부파일이 없는 경우
//메일 보내기
javaMailSender.send(message);
}
//DB에 보낸 메일 저장
adminEmailDAO.senderEmail(vo);
}catch(Exception e){
e.printStackTrace();
}
}
}
@Controller
@RequestMapping("/admin/mail")
public class AdminMailController {
private static final Logger logger =LoggerFactory.getLogger(AdminMailController.class);
private final String JSP_PAGE ="/admin/mail/";
@Inject
private AdminEmailService adminEmailService;
private final String PATH="WEB-INF/uploads/mailbox/";
@RequestMapping(value="/composeForm", method=RequestMethod.GET)
public String mailComposeForm(){
return JSP_PAGE+"compose";
}
@RequestMapping(value="/send", method=RequestMethod.POST)
public String mailComposeAction(AdminEmailVO vo, HttpServletRequest request){
adminEmailService.senderEmail(vo, request);
return "redirect:composeForm";
}
@RequestMapping(value="/ckeditorupload")
public void imageUpload(HttpServletRequest request,
HttpServletResponse response,
@RequestParam MultipartFile upload) throws Exception{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
OutputStream out=null;
PrintWriter printWriter =null;
logger.info("ckediotr 업로드");
try{
String fileName =upload.getOriginalFilename();
byte[] bytes=upload.getBytes();
UploadPath.attach_path=PATH;
String uploadPath =UploadPath.path(request)+fileName;
logger.info("Ckeditor 업로드 경로 :" + uploadPath);
out=new FileOutputStream(new File(uploadPath));
out.write(bytes);
String callback=request.getParameter("CKEditorFuncNum");
printWriter=response.getWriter();
String fileUrl ="/mailbox/"+fileName;
String script="<script>";
script +="window.parent.CKEDITOR.tools.callFunction(";
script +=callback;
script +=", '";
script +=fileUrl;
script +="', '이미지를 업로드 했습니다.' )";
script +="</script>";
printWriter.println(script);
printWriter.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
if(out!=null){
out.close();
}
if(printWriter!=null){
printWriter.close();
}
}
}
}
<div class="box box-primary">
<form action="/admin/mail/send" method="post" enctype="multipart/form-data" name="form1">
<div class="box-header with-border">
<h3 class="box-title">새 메시지를 작성 </h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="form-group">
<label>발신자 이름 </label>
<input class="form-control" name="senderName">
</div>
<div class="form-group">
<label>발신자 이메일 주소 </label>
<input class="form-control" name="senderMail" placeholder="from :">
</div>
<div class="form-group">
<label>수신자 이메일 주소 </label>
<input class="form-control" placeholder="To:" name="receiveMail" >
</div>
<div class="form-group">
<label>제목 </label>
<input class="form-control" placeholder="Subject:" name="subject">
</div>
<div class="form-group">
<label>내용 </label>
<textarea id="message" class="form-control" style="height: 300px" name="message"></textarea>
</div>
<div class="form-group">
<div class="form-group">
<i class="fa fa-paperclip"></i> 파일첨부
<input type="file" name="files" class="form-control" >
</div>
<!-- <p class="help-block">Max. 32MB</p> -->
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<div class="pull-right">
<button type="reset" class="btn btn-default" id="reset"><i class="fa fa-pencil"></i> 리셋</button>
<button type="submit" class="btn btn-primary"><i class="fa fa-envelope-o"></i>보내기</button>
</div>
<!-- <button type="reset" class="btn btn-default"><i class="fa fa-times"></i> Discard</button> -->
</div>
<!-- /.box-footer -->
</form>
</div>
<script src="/resources/admin/plugins/ckeditor/ckeditor.js"></script>
<script>
$(document).ready(function(){
$("#reset").click(function(){
document.form1.senderName.value("");
document.form1.senderMail.value("");
document.form1.receiveMail.value("");
document.form1.subject.value("");
});
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'message', {//해당 이름으로 된 textarea에 에디터를 적용 <-- 이거 이름 부분입니다.
width:'100%',
height:'600px',
// extraPlugins : 'youtube',
//여기 경로로 파일을 전달하여 업로드 시킨다.
// JSP, PHP 공통입니다. 경로를 적당히 적어줍니다.
filebrowserImageUploadUrl: '/admin/mail/ckeditorupload'
});
});
</script>
댓글 ( 4)
댓글 남기기