UseSpringSending emails is mainly implemented by implementing the org. springframework. Mail. javamail. javamailsender interface. with IOC, we can achieve this through configuration.
1 <bean id = "mailsender"
2 class = "org. springframework. Mail. javamail. javamailsenderimpl">
3 <property name = "host">
4 <value> smtp.gmail.com
Package kafei. time;
Public class mailservice {
Private javamailsender mailsender;
Public feedbackservice (){}
Public void setmailsender (javamailsender mailsender) {Java heaven
This. mailsender = mailsender;
}
Public void send (){
// Create a multimedia email
Mimemessage me = mailsender. createmimemessage ();
Mimemessagehelper message = new mimemessagehelper (Me, true); Java heaven
Message. setto ("user@gmail.com ");
Message. setsubject ("test ");
Message. settext ("this is a test ");
Filesystemresource IMG = new filesystemresource (
New file ("E:/workspace/Founder/src/kafeitime.jpg "));
Message. addinline ("PNG", IMG );
// Message. addattachment ("PNG", IMG); // send the file as an attachment
Mailsender. Send (me );
System. Out. println ("email sent successfully ");
}
}
In this way, you can
The following code is written to the bean configuration file.
<Bean id = "mailservice"
Class = "kafei. Time. mailservice">
<Property name = "mailsender">
<Ref bean = "mailsender"/>
Public class mailtest {
Public static void main (string [] ARGs ){
Java heaven
Resource rs =
New filesystemresource ("beans-config.xml ");
Beanfactory factory =
New xmlbeanfactory (RS );
Kafei. Time. mailservice =
(Hellobean) Factory. getbean ("mailservice ");
Mailservice. Send ();
}
}
This example has been tested successfully, but may cause some errors during writing. Please leave a message if you have any questions.