Spring uses Gmail as the mail sending server

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.