Spring 专题
专题目录
您的位置:java > Spring专题 > Spring发送带附件邮件
Spring发送带附件邮件
作者:--    发布时间:2019-11-20
下面是一个例子使用spring通过gmail smtp服务器来发送电子邮件附件。为了包含附件的电子邮件,你必须使用 spring的javamailsender及mimemessage 来代替 mailsender&simplemailmessage。

2.spring的邮件发件人

必须使用 javamailsender 代替 mailsender 发送附件,并用 mimemessagehelper 附加的资源。在这个例子中,它会得到 “c:\\log.txt” 从文件系统(filesystemresource)作为电子邮件附件的文本文件。

除了文件系统,您还可以从url路径(urlresource对象),类路径(使用classpathresource),inputstream(inputstreamresource)的任何资源......请参考 spring 的 abstractresource 类的实现。

file : mailmail.java

package com.h3.common;

import javax.mail.messagingexception;
import javax.mail.internet.mimemessage;

import org.springframework.core.io.filesystemresource;
import org.springframework.mail.mailparseexception;
import org.springframework.mail.simplemailmessage;
import org.springframework.mail.javamail.javamailsender;
import org.springframework.mail.javamail.mimemessagehelper;

public class mailmail
{
	private javamailsender mailsender;
	private simplemailmessage simplemailmessage;
	
	public void setsimplemailmessage(simplemailmessage simplemailmessage) {
		this.simplemailmessage = simplemailmessage;
	}

	public void setmailsender(javamailsender mailsender) {
		this.mailsender = mailsender;
	}
	
	public void sendmail(string dear, string content) {
	
	   mimemessage message = mailsender.createmimemessage();
		
	   try{
		mimemessagehelper helper = new mimemessagehelper(message, true);
			
		helper.setfrom(simplemailmessage.getfrom());
		helper.setto(simplemailmessage.getto());
		helper.setsubject(simplemailmessage.getsubject());
		helper.settext(string.format(
			simplemailmessage.gettext(), dear, content));
			
		filesystemresource file = new filesystemresource("c:\\log.txt");
		helper.addattachment(file.getfilename(), file);

	     }catch (messagingexception e) {
		throw new mailparseexception(e);
	     }
	     mailsender.send(message);
         }
}

3. bean配置文件

配置 mailsender bean,电子邮件模板,并指定gmail的smtp服务器电子邮件的详细信息。

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="h3.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.h3.common.mailmail">
	<property name="mailsender" ref="mailsender" />
	<property name="simplemailmessage" ref="customemailmessage" />
</bean>
	
<bean id="customemailmessage"
	class="org.springframework.mail.simplemailmessage">

	<property name="from" value="from@no-spam.com" />
	<property name="to" value="to@no-spam.com" />
	<property name="subject" value="testing subject" />
	<property name="text">
	<value>
		<![cdata[
			dear %s,
			mail content : %s
		]]>
	</value>
    </property>
</bean>

</beans>

4. 运行它

package com.h3.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("applicationcontext.xml");
    	 
    	mailmail mm = (mailmail) context.getbean("mailmail");
        mm.sendmail("h3", "this is text content");
        
    }
}

输出结果

dear h3,
 mail content : this is text content
 attachment : log.txt

下载代码 –  http://pan.baidu.com/s/1jhn9vlw

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册