JavaMail sending mail through SMTP in spring

Source: Internet
Author: User

Public final class Emails {

/** the Java Mail sender. */
private static Javamailsender Javamailsender;

/**
* Send.
*
* @param emaildata
* The email data
*/
public static void Send (Emailconfig.emaildata emaildata) {
Emailorsmslogentity emailorsmslogentity = new emailorsmslogentity ();
Emailorsmslogentity.setmessagetype (Messagetype.email);
Emailorsmslogentity.revodeip ();
try {
Javamailsender Javamailsender = Maildefaultsender ();
MimeMessage message = Javamailsender.createmimemessage ();
Mimemessagehelper messagehelper = new Mimemessagehelper (message,
True, constants.default_encoding);
Messagehelper.setfrom (Emaildata.getfrom ());
Emailorsmslogentity.setfrom (Emaildata.getfrom ());
Messagehelper.setto (Emaildata.to ());
Emailorsmslogentity.setto (Stringutils.join (Emaildata.to (), ","));
Messagehelper.setsubject (Emaildata.gettitle ());
Emailorsmslogentity.setsubject (Emaildata.gettitle ());
Messagehelper.settext (Emaildata.getcontent (), true);
Emailorsmslogentity.settext (Emaildata.getcontent ());
collection<file> files = emaildata.getattachment ();
if (Files! = null) {
list<string> fileNames = Lists.newarraylist ();
for (File file:files) {
Messagehelper.addattachment (File.getname (), file);
Filenames.add (File.getname ());
}
Emailorsmslogentity.addfeatures (Emailorsmslogentity.filetag,
Stringutils.join (FileNames, ","));
}
Javamailsender.send (message);
Emailorsmslogentity.setsuccess (TRUE);
} catch (Exception e) {
E.printstacktrace ();
Emailorsmslogentity.setsuccess (FALSE);
Emailorsmslogentity.setfailure (Throwables.getstacktraceasstring (e));
}
Emailorsmslogentity.createorupdate ();
}

/**
* AYSN send.
*
* @param emaildata
* The email data
*/
public static void Aysnsend (final emailconfig.emaildata emaildata) {
Swingutilities.invokelater (New Runnable () {

@Override
public void Run () {
Send (Emaildata);
}
});
}

/**
* Mail default sender.
*
* @return the Java Mail Sender
*/
public static Javamailsender Maildefaultsender () {
Return MailSender (Sysconfigentity.config ());
}

/**
* Mail sender.
*
* @param config
* the config
* @return the Java Mail sender
*/
Private Static Javamailsender MailSender (emailconfig config) {
Javamailsender Javamailsender = MailSender ();
if (javamailsender instanceof javamailsenderimpl && config! = null) {
Javamailsenderimpl Impl = (javamailsende Rimpl) Javamailsender;
if (config.getmailhost () = null)
Impl.sethost (Config.getmailhost ());
if (config.getmailpost () = null)
Impl.setport (Config.getmailpost ());
if (config.getmailusername () = null)
Impl.setusername (Config.getmailusername ());
if (Config.getmailpassword () = null)
Impl.setpassword (Cryptoutils.aesdecryptfromhex (
Config.getmailpassword (), Keyconstants.mailpassword));
}
return javamailsender;
}

/**
* Mail sender.
*
* @return the Java Mail Sender
*/
private static Javamailsender MailSender () {
if (Javamailsender! = null)
return javamailsender;
Synchronized (Emails.class) {
Javamailsender = Utils.Container.getBean (Javamailsender.class);
}
return javamailsender;
}

}

JavaMail sending mail through SMTP in spring

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.