Baidu looked for a half-day find the problem is to use a fixed format to populate the From attribute, as follows:
The code is as follows |
Copy Code |
Set recipient, Sender String Nick = Javax.mail.internet.MimeUtility.encodeText ("your nickname"); Messagehelper.setfrom (New internetaddress (Nick + "<service@caomeishuo.com>")); Messagehelper.setto (Tomail); Messagehelper.setsubject (subject); |
All Instance Code
Spring's support for mail is defined by the Org.springframework.mail.MailSender interface, where the Org.springframework.mail.javamail.JavamailSenderImpl This implementation class provides support for JavaMail, the following is a brief introduction to its usage, first look at the method without injection, and then look at the method of injection.
1, send Simple Mail example
First of all, we need to springframwork 1.2.8 in the Lib directory Activation.jar and Mail.jar files, put them in the project's Lib directory down.
Examples of sending a simple message are as follows
The code is as follows |
Copy Code |
Import Org.springframework.mail.javamail.JavaMailSenderImpl; Import Org.springframework.mail.SimpleMailMessage; public class Simplemaildemo { public static void Main (string[] args) throws Exception { Javamailsenderimpl Senderimpl = new Javamailsenderimpl (); Setting Mail Server Senderimpl.sethost ("mail.xxxxx"); User name and password required for SMTP authentication Senderimpl.setusername ("abc"); Senderimpl.setpassword ("Defdf");
Create mail message Simplemailmessage MailMessage = new Simplemailmessage ();
Set recipient, sender, subject and text Mailmessage.setto (hello@abc.com); Mailmessage.setfrom (der@def.com); Mailmessage.setsubject ("Test"); Mailmessage.settext ("This is a test!!!");
Send mail Senderimpl.send (MailMessage);
System.out.println ("Mail transfer ok ..."); } } |
2, send the HTML format of the message
To send HTML format, use the
The code is as follows |
Copy Code |
Org.springframework.mail.javamail.MimeMessageHelper to create an HTML message Import Org.springframework.mail.javamail.JavaMailSenderImpl; Import Javax.mail.internet.MimeMessage; Import Org.springframework.mail.javamail.MimeMessageHelper; public class Htmlmaildemo { public static void Main (string[] args) throws Exception { Javamailsenderimpl Senderimpl = New Javamailsenderimpl ();
Setting Mail Server Senderimpl.sethost ("mail.xxxxx");
User name and password required for SMTP authentication Senderimpl.setusername ("abc"); Senderimpl.setpassword ("Defdf");
Create mail message MimeMessage MailMessage = Senderimpl.createmimemessage (); Mimemessagehelper Messagehelper = New Mimemessagehelper (MailMessage);
Set recipient, sender, subject and text Messagehelper.setto ("xxx@your_mail_server.com"); Messagehelper.setfrom ("xxx@your_mail_server.com"); Messagehelper.setsubject ("Test"); Messagehelper.settext ( " Hello! spring! " + " ", true);
Send mail Senderimpl.send (MailMessage);
System.out.println ("Mail transfer ok ..."); } } |
3, send the message with the attachment
At this time to use
The code is as follows |
Copy Code |
Mimemessagehelper addattachement () method import java.io.File; Import Org.springframework.mail.javamail.JavaMailSenderImpl; Import Javax.mail.internet.MimeMessage; Import Org.springframework.core.io.FileSystemResource; Import Org.springframework.mail.javamail.MimeMessageHelper; public class Attachedfiledemo { public static void Main (string[] args) throws Exception { Javamailsenderimpl Senderimpl = New Javamailsenderimpl (); //SMTP authentication requires a username and password Senderimpl.setusername ("abc"); Senderimpl.setpassword ("defdf"); MimeMessage MailMessage = senderimpl.createmimemessage (); mimemessagehelper messagehelper = New Mimemessagehelper ( MailMessage, True); Messagehelper.setto ("def@erer.net"); messagehelper.setfrom ("bfg@tom.com"); messagehelper.setsubject ("Test"); Messagehelper.settext ( " Hello! spring! " + " ", true); filesystemresource file = New Filesystemresource ( new File ("D:/test.rar")); messagehelper.addattachment ("Test.rar", file); senderimpl.send (MailMessage); System.out.println ("OK") ; } } |
4, with the injection method to configure the mail, and before the almost pull
Smtp.xxx.com
True
Your email address.
Your email password.
Recipient address
Your address.
A Spring Mail Sender
The above configuration can be sent directly, look at the Testsendermail.java code:
The code is as follows |
Copy Code |
Package test.mail; Import org.springframework.mail.MailException; Import Org.springframework.mail.MailSender; Import Org.springframework.mail.SimpleMailMessage; public class Testsendermail { Private MailSender MailSender; Private Simplemailmessage MailMessage;
Public Testsendermail () {
}
Public Simplemailmessage Getmailmessage () { return mailmessage; } public void Setmailmessage (Simplemailmessage mailmessage) { This.mailmessage = MailMessage; } Public MailSender Getmailsender () { return mailsender; } public void Setmailsender (MailSender mailsender) { This.mailsender = MailSender; }
public void SendMail () { Simplemailmessage message = new Simplemailmessage (mailmessage); Set up email content, Message.settext ("Test spring sends email.");
try { Mailsender.send (message); catch (Mailexception e) { TODO auto-generated Catch block System.out.println ("O. Send email failed ... "); E.printstacktrace (); } } } |
Testapp.java
The code is as follows |
Copy Code |
Package Test.mail; Import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext; Public class TestApp { public static void main (string[] args) { //TODO auto-generated method Stub applicationcontext context = new Classpathxmlapplicationcontext ( "Test/mail/mail.xml"); testsendermail sender = (testsendermail) Context Getbean ("Testmailsender"); Sender.sendmail (); } } |