日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring通过Gmail SMTP服务器MailSender发送电子邮件

發布時間:2024/9/20 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring通过Gmail SMTP服务器MailSender发送电子邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring提供了一個有用的“org.springframework.mail.javamail.JavaMailSenderImpl”類,通過JavaMail?API?簡化郵件發送過程。這里有一個項目中使用Spring “JavaMailSenderImpl”通過Gmail SMTP服務器發送電子郵件。 1.?Spring郵件發件人 Java 類使用 Spring 的 MailSender 接口發送電子郵件。

File : MailMail.java

package com.yiibai.common;import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage;public class MailMail {private MailSender mailSender;public void setMailSender(MailSender mailSender) {this.mailSender = mailSender;}public void sendMail(String from, String to, String subject, String msg) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(msg);mailSender.send(message); } } 2.?bean配置文件 配置 mailSender?bean 并指定Gmail的SMTP服務器電子郵件的詳細信息。 注
Gmail的配置細節(這里是墻,該翻的翻)?–?http://mail.google.com/support/bin/answer.py?hl=en&answer=13287

File : Spring-Mail.xml

<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-2.5.xsd"><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="yiibai.com@gmail.com" /><property name="password" value="password" /><property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop><prop key="mail.smtp.starttls.enable">true</prop></props></property> </bean><bean id="mailMail" class="com.yiibai.common.MailMail"><property name="mailSender" ref="mailSender" /> </bean></beans>

運行它

package com.yiibai.common;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");MailMail mm = (MailMail) context.getBean("mailMail");mm.sendMail("from@no-spam.com","to@no-spam.com","Testing123", "Testing only \n\n Hello Spring Email Sender");} } 下載源代碼 –??http://pan.baidu.com/s/1gepbWEf 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Spring通过Gmail SMTP服务器MailSender发送电子邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。