Preface
Sending emails is also commonly used in Java programming. However, the native jdk sending emails is still quite troublesome to use. The spring framework is completely like a god in the Java language. It is very convenient to send emails through the email tools of the spring framework. This article mainly explains the use of the email tools provided by spring in Java programming to send emails.
Encoding to send emails
1. First of all, we need a Spring framework environment
2. The core dependency packages required to send emails: spring-context-supportXXX.jar, activation-1.1.jar, javax.mail-1.5.2.jar
3. Import the above core packages into the project
The maven dependency configuration is as follows:
<!-- mail and spring-context-support for send email --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version></dependency><dependent> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.2</version></dependency><!-- /email -->4. After importing the above dependencies, we start writing a simple email to send sample
package service;import java.io.File;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;public class EmailSample { /** Log**/ private static final Log log = LogFactory.getLog(EmailSample.class); /** * Email testing tool class* * @param subject * Email subject* @param content * HTML format email content*/ public static void sendFileMail(String subject, String content) { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // Set host senderImpl.setHost("smtp.126.com"); // Set your own login email account senderImpl.setUsername("[email protected]"); // Email password senderImpl.setPassword("******"); try { // Create HTML mail message MimeMessage mailMessage = senderImpl.createMimeMessage(); // True means to start attachment mode. If the email does not need attachment to false, MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8"); // Set the email address of the recipient messageHelper.setTo("[email protected]"); // Set the email address of the sender {as consistent with the email logged in above} messageHelper.setFrom("[email protected]"); // Set the subject of the messageHelper.setSubject(subject); // true Indicates that the email messageHelper.setText("<html><title>This is an email</title><body>" + content + "</body></html>", true); // If no attachment is needed, you can omit it here ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- File( "e:/test.jpg")); // Read attachment 2 FileSystemResource file2 = new FileSystemResource(new File( "e:/test.txt")); // Add attachment 1 messageHelper.addAttachment("test.jpg", file1); // Add attachment 2// The attachment name may appear garbled messageHelper.addAttachment(MimeUtility.encodeWord("test.txt"), file2); // If attachment is not needed, you can omit it here --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Send email senderImpl.send(mailMessage); log.info("Email Send Success!"); } catch (Exception e) { log.error("Email Send Error!" + e.getMessage()); } } public static void main(String[] agrs) { // Note that the test requires modifying your own email service provider host, login email user, email password, attachment, recipient address sendFileMail("Test email", "<H1>Test email title</H1>"); }}[Precautions]: Modify parameters when running the main method test: service provider host, login user, login password, recipient address, attachment (no attachments can be omitted)
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.