이 기사는 Spring+Maven의 특정 코드를 공유하여 참조를 위해 이메일 전송을 구현합니다. 특정 내용은 다음과 같습니다
오늘 나는 그것을 보내기 위해 이메일을 받고 싶었고, Baidu에 가서 이메일을 사용하여 그것을 보내는 많은 친구들이 나를 포함하여 다양한 문제에 직면한다는 것을 알았습니다. 몇 가지 문제가 발생했습니다. 그것을 보내고 단계를 따르고 직접 실행하겠습니다.
추신 : 다음 소스 코드는 Baidu에서 제공되며 개인이 작성하지 않습니다. 더 이상 고민하지 않고 위 그림의 코드
프로젝트:
코드 블록
package com.baidu.action; import org.springframework.mail.mailsender; import org.springframework.mail.simplemailmessage;/*** -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Parameter 이름 : @param 제목 이메일 주제* 매개 변수 이름 : @param 컨텐츠 이메일 주제 내용* 매개 변수 이름 : 수신자 이메일 주소로 @param으로 이메일 주소 : 이메일 보내기*/ public void sendmail (문자열 주제, 문자열 내용, 문자열 to) {simplemailmessage.setSubject (주제); // 이메일 주제를 SMIPLEMAILMESAGE.SETTO (to)로 설정합니다. // 수신자 SimpleEmailMessage.settext (content)를 설정합니다. // 이메일 주제 내용 MailSender.Send (SimpleMailMessage)를 설정합니다. // 이메일 보내기} // 스프링 종속성 주입 공개 void setSimplemailMessage (simplemailMessage simplemailMessage) {this.SimpleMailMessage = simplemailMessage; } // 스프링 종속성 주입 공개 void setmailsender (mailsender mailsender) {this.mailsender = mailsender; }}Spring-SMTP-Mail.xml
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id = "mailsender "> <! <smtp. <!-포트 번호-> <속성 이름 = "port"value = "25" /> <!-사용자 이름-> <속성 이름 = "username"value = "[email protected]" /> <!-password "="property name = "valess"= "비밀번호는 우편함에서 설정해야합니다. <props> <!-신원 확인-> <prop key = "mail.smtp.auth"> true </prop> </props> </property> </bean> </bean> </bean> </bean> </bean> </bean> </bean> </bean key = "mail.smtp.auth"> true </prop> </props> </bean> <! 다음 목록은 SMTP 서버 이름 및 NetEase의 포트 번호입니다. Netease Mailbox SMTP SMTP SMTP SMTP POR3 Server POP3 POR POP3 [email protected] SMTP.126.com 25 POP3.126.com 110 @163.com SMTP.163.com 25 POP3.163.com 110 @YEAY.NET SMTP.YEAH.NET 25 POP3.YEAH.NET 110-> <!-발신자 이메일-> <속성 이름 = "from"value = "fengemail04@ bean> < /bean> <bean id ="simplemail "> <property name ="mailsender "ref ="mailsender " /<property name ="simplemailmessage "ref ="simplemailmessage " /> < /beans>
emailTest.java
package com.baidu.test; import junit.framework.testcase; import org.springframework.context.applicationcontext; import org.sprameframework.context.support.classpathxmlapplicationContext; import com.baidu.action.email;/*** *** *** ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ { public void testSendMail () {ApplicationContext context = new ClassPathXmlApplicationContext ( "Spring-Smtp-mail.xml"); 이메일 mail = (이메일) context.getBean ( "simplemail"); mail.sendmail ( "제목", "내용", "[email protected]"); //mail.sendmail("title ","content ","수신자 이메일 "); }}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.apache.org/pom/0.0.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.baidu.maven </groupid> <artifactid> mailtest02 </artifactid> <parging> war </panking0 <est0> 0.0.1-shot> </version 2 Maven WebApp </name> <Url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </properties> <pecienties> <ponceptency> <groupid> org.springframwork> <Artifactid> Spring-Core </artifactid> <bersion> 3.2.4. Release </version> </dependency> <pectomency> <groupid> org.springframework </groupid> <artifactid> spring-context </artifactid> </artifactid> </version </versionency> <group-groupid> javax.mail.mail.mail. <artifactid> mail </artifactid> <bersion> 1.4.4 </version> </dependency> <pectionement> <groupid> </groupid> <artifactid> junit </artifactid> <bersion> 3.8.1 </version> <cope> test </scope> </dectinement> <groupd> groupframwork </groupframwork <아티 팩트> Spring-Context-Support </artifactid> <bersion> 3.2.13. Release </version> </fectionency> </fectencies> <build> <FinalName> MailTest02 </finalName> </build> </project>
테스트 : emailtest.java start junit
전송할 수없는 경우 그림과 같이 SMTP 설정을 수행하려면 이메일 주소를 입력하십시오.
위의 내용으로 간단한 이메일을 보내 주시면 도움이되기를 바랍니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.