Spring邮件发送1


注意:邮件发送code中,邮件服务器的申请和配置是比较主要的一个环节,博主这里用的是QQ的邮件服务器。有需要的可以谷歌、百度查下如何开通。

今天看了下Spring的官方文档的邮件发送这一章节。在这里记录一下初次学习成果。详细使用方案如下:

1. 申请邮箱服务器,用于发送邮件。

  

2. 在项目中引入用于支持java邮件发送的jar包

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>

3. 配置java邮件发送器

邮件发送器配置: applicationContext-mail.xml

   <!-- Define mail sender util bean -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="protocol" value="#{config['mail.protocol']}" />
        <property name="host" value="#{config['mail.host']}" />
        <property name="port" value="#{config['mail.port']}" />
        <property name="username" value="#{config['mail.username']}" />
        <property name="password" value="#{config['mail.password']}" />
        <property name="defaultEncoding" value="#{config['mail.default.encoding']}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">#{config['mail.smtp.auth']}</prop> 
                <prop key="mail.smtp.starttls.enable">#{config['mail.smtp.starttls.enable']}</prop>
                <prop key="mail.smtp.timeout">#{config['mail.smtp.timeout']}</prop>  
            </props>
        </property>
    </bean>

邮件发送器属性文件

mail.protocol=smtp
mail.host=smtp.qq.com
mail.port=587

mail.username=发件人邮箱
mail.password=邮箱授权
mail.default.encoding=UTF-8

mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.timeout=25000

4. 编写邮件发送处理器

接口:

/**
  * send text email
  * 
  * @param email     recipients email address 
  * @param subject
  * @param content
  * @return          send mail result
  */
public boolean sendText(String email, String subject, String content);
    
/**
  * send email with attachment
  * 
  * @param email
  * @param subject
  * @param content
  * @param attachments
  * @return
  */
public boolean sendAttachment(String email, String subject, String content, File...attachments);

1). 发送文本邮件    

MimeMessage mimeMessage = mailSender.createMimeMessage();
            
// Indicate this is multipart message & indicate encoding
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
helper.setFrom(form);           // set sender
helper.setTo(to);               // set recipients
helper.setSubject(subject);
helper.setText(content, true);  // Indicate the text included is HTML
            
// Send mail
mailSender.send(mimeMessage);

2). 发送带附件邮件

MimeMessage mimeMessage = mailSender.createMimeMessage();
            
// Indicate this is multipart message & indicate encoding
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
helper.setFrom(form);           // set sender
helper.setTo(to);               // set recipients
helper.setSubject(subject);
helper.setText(content, true);  // Indicate the text included is HTML
            
// Indicate with attachment
for (File attachment : attachments) {
  helper.addAttachment(attachment.getName(), attachment);
}
            
// Send mail
mailSender.send(mimeMessage);

优质内容筛选与推荐>>
1、第四次SCRUM冲刺
2、Python数据类型(二)
3、hive 限制本地内存使用量
4、手动方式sql注入脚本命令之精华版
5、ArrayList_HashSet的比较,以及HashSet分析


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn