Spring MVC 5.xx Framework – org.springframework.mail kullanarak E-posta Göndermenin basit yolu. javamail.JavaMailSenderImpl
Yayınlanan: 2018-10-23Spring MVC Framework'e Giriş, Hello World Spring MVC, Spring MVC kullanarak Birden Çok Dosya Yükle gibi Spring MVC'de Crunchify'da okumuş olabileceğiniz birkaç makale var.
Bu eğitimde, Spring MVC 5.1.3.RELEASE kullanarak bir e-posta göndermek için org.springframework.mail.javamail.JavaMailSenderImpl
kitaplığından nasıl yararlanılacağını inceleyeceğiz.
Başlayalım:
İşte nihai bir proje yapısı. Dosyayı buna göre oluşturduğunuzdan emin olun.
Aşama 1
Basit Maven Projesi oluşturun CrunchifySpringMVC4SendEmailTutorial
.
- “
Create a simple project (skip archetype selection)
” seçeneği için onay kutusunu seçin - Sonraki adımlarda kullanacağımız Grup Kimliği, Eser Kimliği, Ad ve Açıklama gibi aşağıdaki şemaya benzer tüm bilgileri sağlayın.
Adım 2
pom.xml
dosyasını açın ve 3 bağımlılık ekleyin.
- bahar bağlamı
- yay-bağlam-destek
- javax.mail
İşte tam pom.xml
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > CrunchifySpringMVC4SendEmailTutorial < / groupId > < artifactId > CrunchifySpringMVC4SendEmailTutorial < / artifactId > < version > 0.0.1 - SNAPSHOT < / version > < name > CrunchifySpringMVC4SendEmailTutorial < / name > < description > Best way to send an email using Spring MVC 4.1.6 - Crunchify Tips < / description > < dependencies > < dependency > < groupId > org . springframework < / groupId > < artifactId > spring - context < / artifactId > < version > 5.1.3.RELEASE < / version > < / dependency > < dependency > < groupId > org . springframework < / groupId > < artifactId > spring - context - support < / artifactId > < version > 5.1.3.RELEASE < / version > < / dependency > < dependency > < groupId > javax . mail < / groupId > < artifactId > mail < / artifactId > < version > 1.4.7 < / version > < / dependency > < / dependencies > < / project > |
Aşama 3
Spring Bean dosyasını crunchify-bean.xml
dosyasını src/main/resources
klasörü altında oluşturun.
Bahar fasulyesi, geleneksel XML yaklaşımı kullanılarak yapılandırılır. Spring'de MVC framework bean
, bir Spring IoC (Kontrolün Tersine Çevirilmesi) konteyneri tarafından instantiated
, assembled
ve başka bir şekilde managed
bir nesnedir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns : util = "http://www.springframework.org/schema/util" xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns : context = "http://www.springframework.org/schema/context" xsi : schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" > < ! -- this is your package structure where you create file CrunchifyEmailAPI . java and CrunchifyEmailTest . java -- > < context : component - scan base - package = "crunchify.com.tutorials" / > < ! -- Production implementation of the JavaMailSender interface , supporting both JavaMail MimeMessages and Spring SimpleMailMessages -- > < bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl" > < property name = "host" value = "smtp.gmail.com" / > < property name = "port" value = "587" / > < property name = "username" value = "<!-- Provide your Gmail ID -->" / > < property name = "password" value = "<!-- Provide your Gmail Password -->" / > < ! -- The name of the property , following JavaBean naming conventions -- > < property name = "javaMailProperties" > < props > < prop key = "mail.transport.protocol" > smtp < / prop > < prop key = "mail.smtp.auth" > true < / prop > < prop key = "mail.smtp.starttls.enable" > true < / prop > < prop key = "mail.debug" > true < / prop > < / props > < / property > < / bean > < / beans > |
Lütfen username
ve password
alanı değerlerini gerçek/gerçek değerlerinizle güncellediğinizden emin olun.
4. Adım
src/main/java
klasörü altında @Service
(org.springframework.stereotype.Service) ile API sınıfı CrunchifyEmailAPI.java
ek açıklaması oluşturun.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package crunchify . com . tutorials ; import org . springframework . beans . factory . annotation . Autowired ; import org . springframework . mail . MailSender ; import org . springframework . mail . SimpleMailMessage ; import org . springframework . stereotype . Service ; /** * @author Crunchify.com * */ @Service ( "crunchifyEmail" ) public class CrunchifyEmailAPI { @Autowired private MailSender crunchifymail ; // MailSender interface defines a strategy // for sending simple mails public void crunchifyReadyToSendEmail ( String toAddress , String fromAddress , String subject , String msgBody ) { SimpleMailMessage crunchifyMsg = new SimpleMailMessage ( ) ; crunchifyMsg . setFrom ( fromAddress ) ; crunchifyMsg . setTo ( toAddress ) ; crunchifyMsg . setSubject ( subject ) ; crunchifyMsg . setText ( msgBody ) ; crunchifymail . send ( crunchifyMsg ) ; } } |
Adım 5
src/main/java
klasörü altında Test sınıfı CrunchifyEmailTest.java
oluşturun.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package crunchify . com . tutorials ; import org . springframework . context . ConfigurableApplicationContext ; import org . springframework . context . support . ClassPathXmlApplicationContext ; /** * @author Crunchify.com * */ public class CrunchifyEmailTest { @SuppressWarnings ( "resource" ) public static void main ( String args [ ] ) { // Spring Bean file you specified in /src/main/resources folder String crunchifyConfFile = "crunchify-bean.xml" ; ConfigurableApplicationContext context = new ClassPathXmlApplicationContext ( crunchifyConfFile ) ; // @Service("crunchifyEmail") <-- same annotation you specified in CrunchifyEmailAPI.java CrunchifyEmailAPI crunchifyEmailAPI = ( CrunchifyEmailAPI ) context . getBean ( "crunchifyEmail" ) ; // email subject String subject = "Hey.. This email sent by Crunchify's Spring MVC Tutorial" ; // email body String body = "There you go.. You got an email.. Let's understand details on how Spring MVC works -- By Crunchify Admin" ; crunchifyEmailAPI . crunchifyReadyToSendEmail ( toAddr , fromAddr , subject , body ) ; } } |
Lütfen yukarıdakiAddr ve toAddr
güncelleme yaptığınızdan emin fromAddr
.

Adım-6
Şimdi CrunchifyEmailTest.java'nızı let's run
ve console
sonucunu kontrol edelim. Ayrıca checkout your Gmail
.
Gmail hesabımdan:
Konsol Çıkışı:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
Jun 22, 2015 8:38:05 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5010be6: startup date [Mon Jun 22 20:38:05 CDT 2015]; root of context hierarchy Jun 22, 2015 8:38:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [crunchify-bean.xml] DEBUG: JavaMail version 1.4.7 DEBUG: successfully loaded resource: /META-INF/javamail.default.providers DEBUG: Tables of loaded providers DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false 220 mx.google.com ESMTP gc7sm11710775obb.26 - gsmtp DEBUG SMTP: connected to host "smtp.gmail.com", port: 587 EHLO 192.168.0.3 250-mx.google.com at your service, [25.15.21.112] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "STARTTLS", arg "" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" STARTTLS 220 2.0.0 Ready to start TLS EHLO 192.161.0.22 250-mx.google.com at your service, [25.15.21.112] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN succeeded DEBUG SMTP: use8bit false MAIL FROM:<test@crunchify.com> 250 2.1.0 OK gc7sm11710775obb.26 - gsmtp RCPT TO:<test@crunchify.com> 250 2.1.5 OK gc7sm11710775obb.26 - gsmtp DEBUG SMTP: Verified Addresses DEBUG SMTP: test@crunchify.com DATA 354 Go ahead gc7sm11710775obb.26 - gsmtp Date: Mon, 22 Jun 2015 20:38:08 -0500 (CDT) From: test@crunchify.com To: test@crunchify.com Message-ID: <428566321.0.1435023488148.JavaMail.app@LM-AUN-11000370> Subject: Hey.. This email sent by Crunchify's Spring MVC Tutorial MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit There you go.. You got an email.. Let's understand details on how Spring MVC works -- By Crunchify Admin . 250 2.0.0 OK 1435023491 gc7sm11710775obb.26 - gsmtp QUIT 221 2.0.0 closing connection gc7sm11710775obb.26 - gsmtp |