この記事では、Spring+Mavenの特定のコードを共有して、リファレンスの電子メール送信を実装しています。特定のコンテンツは次のとおりです
今日、私はそれを送るために電子メールを受け取りたかったので、バイドゥに行き、電子メールを使用して送信する多くの友人が私を含むさまざまな問題に遭遇することを発見しました。いくつかの問題に遭遇しました。それを送り出して、手順に従って直接実行させてください。
PS:次のソースコードはBaiduからのものであり、個人によって書かれていません。これ以上苦労することなく、上の写真のコード
プロジェクト:
コードブロック
パッケージcom.baidu.action; import org.springframework.mail.mailsender; Import org.springframework.mail.simplemailmessage;/*** -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Parameter名前:@param件名の件名*パラメーター名:@paramコンテンツ電子メール主題コンテンツ*パラメーター名:@param to semay emailアドレス*説明:電子メールを送信*/ public void sendmail(string subject、string content、string to){simplemailmessage.setsubject(subject); //電子メールの件名を設定しましたsimplemailmessage.setto(to); //受信者をSimpleMailmessage.settext(content); //電子メールの主題コンテンツMailSender.send(SimpleMailmessage)を設定します。 //電子メールを送信} //スプリング依存関係インジェクションpublic void setsimplemailmessage(simplemailmessage simplemailmessage){this.simplemailmessage = simplemailmessage; } //スプリング依存関係注射public void setMailsender(Mailsender MailSender){this.mailsender = mailsender; }}spring-smtp-mail.xml
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.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 " <! - ポート番号 - > <プロパティ名= "port" value = "25" /> <! - ユーザー名 - > <プロパティ名= "username" value = "[email protected]" /> <! - パスワード - > <プロパティ名= "パスワード"値= "パスワードはメールボックスに設定する必要があります。 <Props> <! - IDの検証 - > <prop key = "mail.smtp.auth"> true </prop> </props> </property> </bean> <! - 識別の検証 - > <prop key = "mail.smtp.auth"> true </prop> </props> </property> </bean> </bean> <次のリストはSMTPサーバー名とポート番号のNetease番号:Netease Mailbox Smtp Server SMTP POP3 SERVER POP3 POP3 PORT@ PORT@ PORT@ PORT@ PORT@ PORTP.126.com 25 Pop3.126.com 110 @163.com SMTP.163.com id = "Simplemailmessage"> <! - 送信者メール - > <プロパティ名= "from" value = "[email protected]" /> < /bean> <bean id = "simplemail"> <property name = "mailsender" ref = "mailsender" /> <プロパティ名= "
emailTest.java
パッケージcom.baidu.test; Import junit.framework.testcase; import org.springframework.context.applicationcontext; Import org.springframework.context.support.classpathxmlapplicationContext; import com.baidu.email;/*** *** ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ { public void testsendmail(){applicationContext Context = new ClassPathXMLApplicationContext( "Spring-SMTP-Mail.xml");メールメール=(電子メール)Context.getBean( "SimpleMail"); mail.sendmail( "title"、 "content"、 "[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.apach/4.0. http://maven.apache.org/maven-v4_0_0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.baidu.maven </groupid> <artifactid> mailtest02 </artifactid> <パッケージ> war <name> mailtest02 maven webapp </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <artifactid> spring-core </artifactid> <version> 3.2.4.Release </version> </dependency> <seplagency> <shipid> org.springframework </groupid> <artifactid> spring-context </artifactid> <バージョン> 3.2.4.release </version <artifactid> mail </artifactid> <バージョン> 1.4.4 </version> </dependency> <dependency> groupid> junit </groupid> <artifactid> junit </artifactid> <バージョン> 3.8.1 </version> <scope> test </scope> </despence> <artifactid> spring-context-support </artifactid> <version> 3.2.13.release </version> </dependency> </dependencies> <burt> <binalName> mailTest02 </finalName> </build> </project>
テスト:emailTest.java Junitを開始します
送信できない場合は、図に示すように、SMTP設定を実行するためにメールアドレスを入力してください。
上記のコンテンツになるように簡単なメールを送信してください。それがあなたを助けることができることを願っています。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。