<div class="cnblogs_code">
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.beans.factory.annotation.Autowired;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.core.io.FileSystemResource;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.mail.MailParseException;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.mail.SimpleMailMessage;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.mail.javamail.JavaMailSender;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.mail.javamail.MimeMessageHelper;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.stereotype.Controller;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.RequestMapping;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.RequestMethod;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.springframework.web.bind.annotation.ResponseBody;@Controller
@RequestMapping("/api"<span style="color: #000000;">)
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> SendMailController {@Autowired </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> JavaMailSender mailSender; </span><span style="color: #0000ff;">private</span> SimpleMailMessage simpleMailMessage = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SimpleMailMessage(); @RequestMapping(value </span>= "/sendmsg",method =<span style="color: #000000;"> RequestMethod.GET) @ResponseBody public String sendMessage(@RequestParam String mail){</span><span style="color: #000000;"> simpleMailMessage.setSubject(</span>"~-Test-~"<span style="color: #000000;">); simpleMailMessage.setText(</span>"test"<span style="color: #000000;">); simpleMailMessage.setFrom(</span>"xxxxy@xxxxy.com"<span style="color: #000000;">);</span></pre>
if(mail.indexOf(",")>0){ String[] tos=mail.split(","); simpleMailMessage.setTo(tos); }else{ simpleMailMessage.setTo(mail); }
</span><span style="color: #0000ff;">return</span> "Mail Sent"<span style="color: #000000;">; } @RequestMapping(value </span>= "/sendMessageWithAttachment",method =<span style="color: #000000;"> RequestMethod.GET) @ResponseBody public String sendMessageWithAttachment(@RequestParam String mail){</span><span style="color: #000000;"> simpleMailMessage</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> SimpleMailMessage(); simpleMailMessage.setFrom(</span>"xxxxy@xxxxy.com"<span style="color: #000000;">);</span></pre>
if(mail.indexOf(",")>0){ String[] tos=mail.split(","); simpleMailMessage.setTo(tos); }else{ simpleMailMessage.setTo(mail); }
"xxxxy""dear davidwangwei456"+"\r\n xxxxy" +",见附件所示"
sendMailWithAttachment(</span>"xxxxy.xls"<span style="color: #000000;">,simpleMailMessage); </span><span style="color: #0000ff;">return</span> "Mail Sent"<span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> sendMailWithAttachment(String fileName,SimpleMailMessage simpleMailMessage) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="/tag/fasongyoujian/" target="_blank" class="keywords">发送邮件</a></span> MimeMessage message =<span style="color: #000000;"> mailSender.createMimeMessage(); </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { MimeMessageHelper helper </span>= <span style="color: #0000ff;">new</span> MimeMessageHelper(message,<span style="color: #0000ff;">true</span>,"UTF-8"<span style="color: #000000;">); helper.setFrom(simpleMailMessage.getFrom()); helper.setTo(simpleMailMessage.getTo()); helper.setSubject(simpleMailMessage.getSubject()); helper.setText(simpleMailMessage.getText()); <a href="/tag/FileSystemResource/" target="_blank" class="keywords">FileSystemResource</a> file </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> <a href="/tag/FileSystemResource/" target="_blank" class="keywords">FileSystemResource</a>(fileName); helper.addAttachment(file.getFilename(),file); } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (MessagingException e) { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> MailParseException(e); } mailSender.send(message); } @RequestMapping(value </span>= "/hello",method =<span style="color: #000000;"> RequestMethod.GET) @ResponseBody </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getHello(){ </span><span style="color: #0000ff;">return</span> "Hello"<span style="color: #000000;">; }</span></pre>