Use the Mail API provided by spring to send messages

Source: Internet
Author: User

There is mail technology in Java. Use spring's mail to simplify your code


The demo code on the Spring Mail API, the annotation on the code is enough to explain everything, actually this is a very simple technology ....


Package org.mail.test;
Import Java.io.File;

Import java.util.Properties;

Import Javax.mail.internet.MimeMessage;
Import Org.springframework.core.io.FileSystemResource;
Import Org.springframework.mail.javamail.JavaMailSenderImpl;

Import Org.springframework.mail.javamail.MimeMessageHelper;

	public class Springmail {private static String host = "smtp.qq.com";

	The destination mailbox writes the mailbox address that you want to send private static String to = "";

	Send a mailbox write your own email address to send ... private static String from = "";

	User name private static String username = "";

	Password private static String password = "";
	
	
	Message subject private static String subject = "Test message subject ...";

	private static Properties Prop = new properties (); 
		static {Prop.put ("Mail.smtp.auth", "false");
		Prop.put ("Mail.smtp.timeout", "25000");
		Prop.put ("Mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	Prop.put ("Mail.smtp.port", 465); public static void Main (string[] args) throws Exception {Javamailsenderimpl JavamailsenderiMPL = new Javamailsenderimpl ();
		
		MimeMessage mimemessage = Javamailsenderimpl.createmimemessage ();
		Javamailsenderimpl.sethost (host);
		Javamailsenderimpl.setusername (username);

		
		Javamailsenderimpl.setpassword (password);
		
		Mimemessagehelper mimemessagehelper = new Mimemessagehelper (mimemessage,true, "utf-8");
		Mimemessagehelper.setfrom (from);
		Mimemessagehelper.setsubject (subject);
		Mimemessagehelper.settext ("", true);
		Mimemessagehelper.setto (to); Mimemessagehelper.addattachment ("Hello.jpg", New Filesystemresource ("The New File" ("C:/users/administrator/desktop/icon .
		
		JPG "));
		
		Javamailsenderimpl.setjavamailproperties (prop);
		Javamailsenderimpl.send (MimeMessage);
	
	SYSTEM.OUT.PRINTLN ("Send message OK ...");
 }
}


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.