加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring 发送邮件代码示例(带附件和不带附件的)

发布时间:2020-12-15 01:51:54 所属栏目:大数据 来源:网络整理
导读: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.Fi

<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;"&gt;private</span><span style="color: #000000;"&gt; JavaMailSender mailSender;

</span><span style="color: #0000ff;"&gt;private</span> SimpleMailMessage simpleMailMessage = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; SimpleMailMessage();

@RequestMapping(value </span>= "/sendmsg",method =<span style="color: #000000;"&gt; RequestMethod.GET)
@ResponseBody
?public String sendMessage(@RequestParam String mail){</span><span style="color: #000000;"&gt;

    simpleMailMessage.setSubject(</span>"~-Test-~"<span style="color: #000000;"&gt;);
    simpleMailMessage.setText(</span>"test"<span style="color: #000000;"&gt;);
    simpleMailMessage.setFrom(</span>"xxxxy@xxxxy.com"<span style="color: #000000;"&gt;);</span></pre>

? ? ? ? ? if(mail.indexOf(",")>0){? ? ? ? ? String[] tos=mail.split(",");? ? ? ? ? simpleMailMessage.setTo(tos);? ? ? ? ? }else{? ? ? ? ? simpleMailMessage.setTo(mail);? ? ? ? ? }

    </span><span style="color: #0000ff;"&gt;return</span> "Mail Sent"<span style="color: #000000;"&gt;;

}

@RequestMapping(value </span>= "/sendMessageWithAttachment",method =<span style="color: #000000;"&gt; RequestMethod.GET)
@ResponseBody
public String sendMessageWithAttachment(@RequestParam String mail){</span><span style="color: #000000;"&gt;

    simpleMailMessage</span>=<span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; SimpleMailMessage();
    simpleMailMessage.setFrom(</span>"xxxxy@xxxxy.com"<span style="color: #000000;"&gt;);</span></pre>

      if(mail.indexOf(",")>0){       String[] tos=mail.split(",");       simpleMailMessage.setTo(tos);       }else{       simpleMailMessage.setTo(mail);       }

"xxxxy""dear davidwangwei456"+"rn xxxxy" +",见附件所示"    sendMailWithAttachment(</span>"xxxxy.xls"<span style="color: #000000;"&gt;,simpleMailMessage);

    </span><span style="color: #0000ff;"&gt;return</span> "Mail Sent"<span style="color: #000000;"&gt;;

}

</span><span style="color: #0000ff;"&gt;private</span> <span style="color: #0000ff;"&gt;void</span><span style="color: #000000;"&gt; sendMailWithAttachment(String fileName,SimpleMailMessage simpleMailMessage) {
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 发送邮件</span>
    MimeMessage message  =<span style="color: #000000;"&gt; mailSender.createMimeMessage();          
    </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; {
        MimeMessageHelper helper </span>= <span style="color: #0000ff;"&gt;new</span> MimeMessageHelper(message,<span style="color: #0000ff;"&gt;true</span>,"UTF-8"<span style="color: #000000;"&gt;);
        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(simpleMailMessage.getTo());
        helper.setSubject(simpleMailMessage.getSubject());
        helper.setText(simpleMailMessage.getText());

        FileSystemResource file </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; FileSystemResource(fileName);
        helper.addAttachment(file.getFilename(),file);

    } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt; (MessagingException e) {
        </span><span style="color: #0000ff;"&gt;throw</span> <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; MailParseException(e);
    }
    mailSender.send(message);
}

@RequestMapping(value </span>= "/hello",method =<span style="color: #000000;"&gt; RequestMethod.GET)
@ResponseBody
</span><span style="color: #0000ff;"&gt;public</span><span style="color: #000000;"&gt; String getHello(){
    </span><span style="color: #0000ff;"&gt;return</span> "Hello"<span style="color: #000000;"&gt;;
}</span></pre>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读