Reference address A
Reference Address II
The primary package for the
Spring message abstraction layer is org.springframework.mail . It includes the primary interface for sending e-mail mailsender , and value objects simplemailmessage , which encapsulates the properties of a simple message such as from , to , cc , subject , text . The package also contains a checked exception inheritance tree with mailexception as the root, which provides a high-level abstraction of the underlying message system exceptions. For richer information about the level of message anomalies, please refer to Javadocs.
In order to use some of the features in JavaMail , such as MIME-type letters, Spring provides , org.springframework.mail.javamail.javamailsender . Spring also provides a callback interface org.springframework.mail.javamail.mimemessagepreparator , MIME letter used to prepare the javamail.
1. Send a simple text message
Package net.xftzr.mail;
Import java.util.Properties;
Import Org.springframework.mail.SimpleMailMessage;
Import Org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* This class tests Simple mail
* Send directly by mail
* @author Administrator
*
*/
public class Singlemailsend {
public static void Main (String args[]) {
Javamailsenderimpl Senderimpl = new Javamailsenderimpl ();
Setting Up mail server
Senderimpl.sethost ("smtp.163.com");
Create a mail message
Simplemailmessage MailMessage = new Simplemailmessage ();
Set recipient, Sender sends multiple messages in an array
string[] array = new string[] {"[email protected]", "[Email protected]"};
Mailmessage.setto (array);
Mailmessage.setto ("[email protected]");
Mailmessage.setfrom ("[email protected]");
Mailmessage.setsubject ("Test simple text mail send! " );
Mailmessage.settext ("Test my Simple mail delivery mechanism!! " );
Senderimpl.setusername ("UserName"); Depending on your situation, set the username
Senderimpl.setpassword ("password"); Depending on your situation, set the password
Properties prop = new properties ();
Prop.put ("Mail.smtp.auth", "true"); Set this parameter to TRUE to have the server authenticate and authenticate the user name and password correctly
Prop.put ("Mail.smtp.timeout", "25000");
Senderimpl.setjavamailproperties (prop);
Send mail
Senderimpl.send (MailMessage);
System.out.println ("Mail sent successfully.");
}
}
2. Send a simple HTML message
Org.springframework.mail.javamail.MimeMessageHelper is one of the most common handy components for handling JavaMail messages. It allows you to get rid of the complex javax.mail.internetAPI class
Package net.xftzr.mail;
Import java.util.Properties;
Import Javax.mail.internet.MimeMessage;
Import Org.springframework.mail.javamail.JavaMailSenderImpl;
Import Org.springframework.mail.javamail.MimeMessageHelper;
/**
* This class tests HTML mail
* @author Sunny
*
*/
public class Htmlmaildemo {
/**
* @param args
*/
public static void Main (string[] args) throws exception{
Javamailsenderimpl Senderimpl = new Javamailsenderimpl ();
Setting Up mail server
Senderimpl.sethost ("smtp.163.com");
Create a mail message to send the difference between a simple message and an HTML message
MimeMessage MailMessage = Senderimpl.createmimemessage ();
Mimemessagehelper messagehelper = new Mimemessagehelper (mailmessage);
Set recipient, Sender
Messagehelper.setto ("[email protected]");
Messagehelper.setfrom ("[email protected]");
Messagehelper.setsubject ("Test HTML mail! ");
True to start HTML-formatted messages
Messagehelper.settext ("
Senderimpl.setusername ("username"); Depending on your situation, set the username
Senderimpl.setpassword ("password"); Depending on your situation, set the password
Properties prop = new properties ();
Prop.put ("Mail.smtp.auth", "true"); Set this parameter to TRUE to have the server authenticate and authenticate the user name and password correctly
Prop.put ("Mail.smtp.timeout", "25000");
Senderimpl.setjavamailproperties (prop);
Send mail
Senderimpl.send (MailMessage);
System.out.println ("Mail sent successfully.");
}
}
3. Send a message with a nested picture
Email allows you to add attachments, and also allows you to embed resources in multipart letters. Embedded resources may be the images you want to use in your letters, or stylesheets, but you don't want to add them as attachments.
Package net.xftzr.mail;
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;
/**
* Nested pictures in this type of test message
* @author Sunny
*
*/
public class Attachedimagemail {
public static void Main (string[] args) throws exception{
Javamailsenderimpl Senderimpl = new Javamailsenderimpl ();
Setting Up mail server
Senderimpl.sethost ("smtp.163.com");
Create a mail message to send the difference between a simple message and an HTML message
MimeMessage MailMessage = Senderimpl.createmimemessage ();
Notice here that the Boolean is equal to the real time to nest the picture, when constructing mimemessagehelper, the given value is true to enable,
Multipart mode
Mimemessagehelper messagehelper = new Mimemessagehelper (mailmessage,true);
Set recipient, Sender
Messagehelper.setto ("[email protected]");
Messagehelper.setfrom ("[email protected]");
Messagehelper.setsubject ("Nested picture in test mail!! ");
True to start HTML-formatted messages
Messagehelper.settext (""</body>
Filesystemresource img = new Filesystemresource (New File ("g:/123.jpg"));
Messagehelper.addinline ("AAA", IMG);
Senderimpl.setusername ("username"); Depending on your situation, set the username
Senderimpl.setpassword ("password"); Depending on your situation, set the password
Properties prop = new properties ();
Prop.put ("Mail.smtp.auth", "true"); Set this parameter to TRUE to have the server authenticate and authenticate the user name and password correctly
Prop.put ("Mail.smtp.timeout", "25000");
Senderimpl.setjavamailproperties (prop);
Send mail
Senderimpl.send (MailMessage);
System.out.println ("Mail sent successfully.");
}
}
4. Send a message with an attachment
Package net.xftzr.mail;
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 Attachedfilemail{
/**
* This type of test is about an example of an attachment in a message
* @param args
*/
public static void Main (string[] args) throws Exception{
Javamailsenderimpl Senderimpl = new Javamailsenderimpl ();
Setting Up mail server
Senderimpl.sethost ("smtp.163.com");
Create a mail message to send the difference between a simple message and an HTML message
MimeMessage MailMessage = Senderimpl.createmimemessage ();
Notice here that the Boolean is equal to the real time to nest the picture, when constructing mimemessagehelper, the given value is true to enable,
Multipart mode is true when sending attachments can be set in HTML format
Mimemessagehelper messagehelper = new Mimemessagehelper (mailmessage,true, "utf-8");
Set recipient, Sender
Messagehelper.setto ("[email protected]");
Messagehelper.setfrom ("[email protected]");
Messagehelper.setsubject ("Upload attachments in test mail!! ");
True to start HTML-formatted messages
Messagehelper.settext ("
Filesystemresource file = new Filesystemresource (new file ("G:/test.rar"));
The method call here and the Insert picture are different.
Messagehelper.addattachment ("Test.rar", file);
Senderimpl.setusername ("username"); Depending on your situation, set the username
Senderimpl.setpassword ("password"); Depending on your situation, set the password
Properties prop = new properties ();
Prop.put ("Mail.smtp.auth", "true"); Set this parameter to TRUE to have the server authenticate and authenticate the user name and password correctly
Prop.put ("Mail.smtp.timeout", "25000");
Senderimpl.setjavamailproperties (prop);
Send mail
Senderimpl.send (MailMessage);
System.out.println ("Mail sent successfully.");
}
}
Spring various mail send