1. Create Mail transmitter
First define the Javamailsenderimpl object and set it SMTP-related information, which is equivalent to our own mailbox, as follows: Java code Javamailsenderimpl MailSender = new Javamailsenderimpl (); Mailsender.sethost ("smtp.qq.com"); Mailsender.setusername ("mosaic@qq.com"); Mailsender.setpassword ("asterisks");
Of course, the better way is to use configuration files for configuration, here is only to introduce, ignore hard coding first.
The above host is the mailbox service provider's SMTP address, the user name, the password is the user's own mailbox. In addition to the above you can also set
Setport (int port), Setprotocol (String protocol), etc., can be considered for the time being.
So we're kind of creating a mail transmitter,
2, start to write mail, write the content of the message
Javamailsenderimpl supports Mimemessages and simplemailmessages.
Mimemessages is a complex mail template that supports text, attachments, HTML, pictures, and so on.
Simplemailmessages implements the Mimemessagehelper, a normal mail template that supports text.
First of all, take simplemailmessages as an example to introduce Java code simplemailmessage SMM = new Simplemailmessage (); Set the message parameter Smm.setfrom (Mailsender.getusername ()); Smm.setto ("mosaic@126.com"); Smm.setsubject ("Hello World"); Smm.settext ("Hello World via Spring Mail Sender"); Send mail Mailsender.send (SMM);
So, we have completed a Simple mail writing, for complex Mail, write and send the following Java code//using JavaMail mimemessage, pay more complex message format and content mimemessage msg = Mailsender.createmimemessage (); Create a Mimemessagehelper object that handles the MimeMessage helper class Mimemessagehelper = new Mimemessagehelper (msg, true); Use the auxiliary class MimeMessage to set the parameter Helper.setfrom (Mailsender.getusername ()); Helper.setto ("mosaic@126.com"); Helper.setsubject ("Hello attachment"); Helper.settext ("This are a mail with attachment"); Loads the file resource as an attachment Classpathresource file = new Classpathresource ("chrysanthemum.jpg"); Add Attachment helper.addattachment ("attachment.jpg", file); Send mail Mailsender.send (SMM);
Where Mimemessagehelper is the auxiliary class mimemessages. The above contains a resource file for the attachment to send. The normal file is sent in the following way: Java code filesystemresource file = new Filesystemresource ("c:\\users\\image1.jpg"); Helper.addinline ("file", file);
3, send mail
2 already contains the sent code, just use the Javamailsenderimpl send interface. Support Type is Java code void Send (MimeMessage mimemessage) Send the given JavaMail MIME message. void Send (mimemessage[] mimemessages) Send the given array of JavaMail MIME messages in batch. void Send (Mimemessagepreparator mimemessagepreparator) Send the JavaMail MIME message prepared by the given Mimemessagepre Parator. void Send (mimemessagepreparator[] mimemessagepreparators) Send the JavaMail MIME messages prepared by the given MIMEMESSAG Epreparators. void Send (Simplemailmessage simplemessage) send the given Simple mail message. void Send (simplemailmessage[] simplemessages) Send the given array of Simple mail messages in batch.
Here's how to send a rich text file and send a message with velocity for the template.
4. Send HTML file
You only need to set the HTML to True when Mimemessagehelper settext. SetText describes the following: XML code setText (String text, boolean HTML) Set the given text directly as content in Non-multipart mode or as D Efault in multipart mode.
Sample code (including attachments) is as follows: Java code//second argument true, representing text content for HTML//Note tags, src= ' cid:file ', ' CID ' is contentid abbreviation, ' File ' is a tag that needs to be invoked in the following code to replace the Mimemessagehelper Addinline method with a file Helper.settext ("<body><p>hello Html Email </p></body> ', true); Filesystemresource file = new Filesystemresource ("c:\\users\\image1.jpg"); Helper.addinline ("file", file);