Simple email sending using spring

Source: Internet
Author: User

Spring provides an abstract layer for sending mail, which makes sending mail very simple.

BelowCodeThe mail. jar package is required. If the server requires authentication, add the following bold code:

Source File: Sendmail. Java:

Package mail;

Import org. springframework. Mail. javamail. javamailsenderimpl;
Import org. springframework. Mail. javamail. mimemessagehelper;
Import javax. Mail. Internet. mimemessage;
Import java. util. properties;
Import java. util. date;
/**
* @ Author chrischen
*/
Public class Sendmail {
// Email sender
Public static string sender (string subject, string MSG, string sendto, string frommail, string user, string PW, string fromname, string protocol, string host, string port ){
Try {
Final string username = user;
Final string pass = pw;
// Authentication required
Properties props = new properties ();
Props. Put ("mail. SMTP. Host", host );
Props. Put ("mail. SMTP. Auth", "true ");
Props. Put ("mail. Transport. Protocol", Protocol );
Props. Put ("mail. From", frommail );
// Create a sender
Javamailsenderimpl sender = new javamailsenderimpl ();
Sender. sethost (host );
Sender. setusername (username );
Sender. setpassword (PASS );
// Create a message
Mimemessage message = sender. createmimemessage ();
Message. addheader ("X-mailer", "Java Mailer ");
Mimemessagehelper helper = new mimemessagehelper (Message );
Helper. setto (sendto );
Helper. setfrom (frommail, fromname );
Helper. setsubject (subject );
Helper. settext (MSG );
Helper. setsentdate (new date ());
// Start sending
Sender. setjavamailproperties (props );
Sender. Send (Message );
} Catch (exception e ){
System. Out. println ("error:" + E );
Return" Failure ";
}
Return"Success ";
}

// Test
Public static void main (string ARGs []) throws exception
{

String subject = "test email"; // Title
String sendto = "test@my.com"; // recipient email
String frommail = "send@my.com"; // sender mail
String user = "send@my.com"; // sender user
String PW = "password"; // the sender's email password.
String fromname = "Chen"; // the sender's name.
String protocol = "SMTP"; // Protocol
String host = "smtp.my.com"; // sending host
String Port = "25"; // Port
String MSG = "send a simple email. "; // Send content
String ret = sender (subject, MSG, sendto, frommail, user, PW, fromname, protocol, host, Port );

System. Out. println ("email sending Result:" + RET );

}

}

 

UseMimemessagehelper can implement multipart email for adding attachments and embedded resources.

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.