Spring郵件發送1

注意:郵件發送code中,郵件服務器的申請和配置是比較主要的一個環節,博主這裏用的是QQ的郵件服務器。有須要的能夠谷歌、百度查下如何開通。java

 

今天看了下Spring的官方文檔的郵件發送這一章節。在這裏記錄一下初次學習成果。詳細使用方案以下:spring

 

1.  申請郵箱服務器,用於發送郵件。服務器

  

2.  在項目中引入用於支持java郵件發送的jar包app

<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.xmlthis

   <!-- 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>

郵件發送器屬性文件spa

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. 編寫郵件發送處理器code

接口: orm

/**
  * 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). 發送文本郵件    xml

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);
相關文章
相關標籤/搜索