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