The example of this article describes how to use spring to send mail in java. Share it with everyone for your reference. The details are as follows:
Here I draw on the advantages of others and some of my own processing, and 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 type of test sends simple emails directly by email * * @author Administrator * */public class SingleMailSend { public static void main(String args[]) throws MessagingException { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // Set up the mail server // senderImpl.setHost("smtp.163.com"); senderImpl.setHost("smtp.qq.com"); // Create mail message // SimpleMailMessage mailMessage = new SimpleMailMessage(); MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true,"utf- 8"); // Set recipients and send mass emails String[] array = new String[] {"[email protected]","[email protected]"}; helper.setTo(array); //mailMessage.setTo("[email protected]"); helper.setFrom("[email protected] "); helper.setSubject("This is my topic!"); 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);//Set username according to your own situation // senderImpl.setUsername("[email protected]"); senderImpl.setUsername("[email protected]"); senderImpl.setPassword("your password"); // Set the password according to your own situation /* Properties prop = new Properties(); prop.put(" mail. smtp.auth ", " true "); // Set this parameter to true to let the server authenticate and verify whether 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 be helpful to everyone’s Java programming.