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.