Artikel ini membagikan kode spesifik untuk Spring+Maven untuk mengimplementasikan pengiriman email untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Hari ini saya ingin mendapatkan email untuk mengirimkannya, dan kemudian saya pergi ke Baidu dan menemukan bahwa banyak teman yang menggunakan email untuk mengirimnya akan menghadapi berbagai masalah, termasuk saya. Saya mengalami beberapa masalah. Izinkan saya mengirimkannya dan mengikuti langkah -langkah dan menjalankannya secara langsung.
PS: Kode sumber berikut berasal dari Baidu dan tidak ditulis oleh seorang individu. Tanpa basa -basi lagi, kode pada gambar di atas
proyek:
Blok kode
Paket com.baidu.action; impor org.springframework.mail.mailsender; impor org.springframework.mail.simplemailmessage;/*** ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Nama: @param Subjek Email Subjek* Nama Parameter: @param Konten Email Subjek Konten* Nama Parameter: @param ke alamat email penerima* Deskripsi: Kirim email*/ public void sendMail (subjek string, konten string, string ke) {SimpleMailMessage.setsubject (subjek); // atur subjek email SimpleMailMessage.setto (to); // atur penerima SimpleMailMessage.setText (konten); // atur Konten Subjek Email MailSender.Send (SimpleMailMessage); // Kirim Email} // Spring Dependency Injeksi Public Void SetSimplemailMessage (SimpleMailMessage SimpleMailMessage) {this.SimplemailMessage = SimpleMailMessage; } // Spring Dependency Injection void public setMailSender (MailSender MailSender) {this.mailsender = mailsender; }}Spring-smtp-mail.xml
<? XML Versi = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmls XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring--3 —1.xsd"> <bean id = "mailsender"> <! <!-Nomor port-> <nama properti = "port" value = "25" /> <!-nama pengguna-> <nama properti = "nama pengguna" value = "[email protected]" /> <!-kata sandi-> <nama properti = "kata sandi" nilai = "Kata sandi perlu disetel di SMailbox, atau Izin Kotak Mailbox adalah enabled" /> <! name="javaMailProperties"> <props> <!-- Verification of identity--> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- Verification of identity--> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- The EMAIL accounts I have used are all from Netease. Daftar berikut adalah nama server SMTP dan nomor port NetEase: Netease Mailbox SMTP Server SMTP Port Pop3 Server Pop3 [email protected] SMTP.126.com 25 POP3.126.com 110 @163.com smtp.163.com Pop3.163.com 110 @yeah.net smtp.yeAh.net 25.163.com 110 @yeah.net smtp.yeAh.net 25.163.com. ID = "SimpleMailMessage"> <!-email pengirim-> <nama properti = "from" value = "[email protected]" /> </ bean> <bean id = "simplemail"> <nama properti = "mailsender" ref = "mailsender" /> <nama properti = "simplemailmessage" simpender = "mailsender" "
EmailTest.java
Paket com.baidu.test; import junit.framework.testcase; impor org.springframework.context.applicationContext; impor org.springframework.context.support.classpathxmLappplicationContext; com.baidu.action.mail;/** --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- testSendMail () {ApplicationContext Context = new ClassPathXMLapPlicationContext ("Spring-Smtp-Mail.xml"); Email mail = (email) context.getBean ("SimpleMail"); mail.sendmail ("judul", "konten", "[email protected]"); //mail.sendmail("title "," konten "," email penerima "); }}pom.xml
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/maven.romp.romp.romp. http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.baidu.maven</groupId> <artifactId>mailtest02</artifactId> <packaging>war</packaging>> <version>0.0.1-SNAPSHOT</version> <name> mailTest02 Maven Webapp </name> <rurl> http://maven.apache.org </rerl> <proy Properties> <poject.build.sourceencoding> </project.build.sourceencoding> </Properties> </pendendency> </project. <ArtifactId> Spring-core </RiTtifacTID> <Version> 3.2.4.release </version> </dependency> <dependency> <groupid> org.springframework </groupid> <ArtifactId> Spring-Context </ArtifactId> <version> 3.2.4.release </Versi> </Dependency </Dependency> <group> <version> </Versi </Versi> </Dependency> <group> <group> <ArTifactId> Mail </RiTtifacTID> <Version> 1.4.4 </version> </dependency> <dependency> <Groupid> Junit </groupid> <ArTifactId> Junit </artifactid> <version> 3.8.1 </version> <scope> tes </seupope> </dependency> <version> </grupingD> </scopeffr. <ArTifactId> Spring-Context-Support </RaintifactId> <Version> 3.2.13.release </version> </dependency> </dependencies> <build> <finalname> mailTest02 </finalname> </bangun> </project>
Tes: emailTest.java mulai junit
Jika tidak dapat dikirim, masukkan alamat email untuk melakukan pengaturan SMTP, seperti yang ditunjukkan pada gambar:
Kirim email sederhana untuk menjadi konten di atas, saya harap ini dapat membantu Anda.
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.