Java uses the spring implementation method for sending mail _java

Source: Internet
Author: User

This article illustrates how Java uses the spring implementation to send mail. Share to everyone for your reference. Specifically as follows:

Here for reference to other people's advantages and some of their own processing, write the following code:

Package test;
Import java.util.Properties;
Import javax.mail.MessagingException;
Import Javax.mail.internet.MimeMessage;
Import Org.springframework.mail.SimpleMailMessage;
Import Org.springframework.mail.javamail.JavaMailSenderImpl;
Import Org.springframework.mail.javamail.MimeMessageHelper;  /** * This class test Simple mail sent directly by mail * * @author Administrator */public class Singlemailsend {public static void main (String
 Args[]) throws Messagingexception {Javamailsenderimpl Senderimpl = new Javamailsenderimpl ();
 Set up mail server//senderimpl.sethost ("smtp.163.com");
 Senderimpl.sethost ("smtp.qq.com");
 Establish mail message//simplemailmessage MailMessage = new Simplemailmessage ();
 MimeMessage MailMessage = Senderimpl.createmimemessage ();
 Mimemessagehelper helper = new Mimemessagehelper (mailmessage,true, "utf-8");
 Set recipient, mass mailing string[] array = new string[] {"18601463269@126.com", "1965571954@qq.com"};
 Helper.setto (array);
 Mailmessage.setto ("iamzken@163.com");
Helper.setfrom ("1965571954@qq.com"); Helper.setsubject ("This is my theme!")
 "); Helper.settext ("<p style= ' color:red;") > This is my content!
 </p> ", true);
Add Attachment classpathresource resource = new Classpathresource ("test.jpg");
  Helper.addattachment ("hello.jpg", Resource); 
  //according to their own situation, set up username//senderimpl.setusername ("iamzken@163.com"); 
 Senderimpl.setusername ("1965571954@qq.com"); Senderimpl.setpassword ("Your password");
 According to their own situation, set password/* Properties prop = new properties (); Prop.put ("Mail.smtp.auth", "true");
 Set this parameter to True to allow the server to authenticate and verify that the username and password are correct prop.put ("Mail.smtp.timeout", "25000");
 Senderimpl.setjavamailproperties (prop); *///Send mail Senderimpl.send (mailmessage);
 System.out.println ("Mail sent successfully ...");

 }
}

I hope this article will help you with your Java programming.

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.