Java USES spring to implement the method of sending mail
- 2020-04-01 04:17:52
- OfStack
This article illustrates how Java USES spring to implement mail. Share with you for your reference. The details are as follows:
Here draw on the advantages of others and their own some processing, 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;
public class SingleMailSend {
public static void main(String args[]) throws MessagingException {
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
//Set up mail server
//senderImpl.setHost("smtp.163.com");
senderImpl.setHost("smtp.qq.com");
//Create a mail message
// SimpleMailMessage mailMessage = new SimpleMailMessage();
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true,"utf-8");
//Set up recipients for group mail
String[] array = new String[]
{"18601463269@126.com","1965571954@qq.com"};
helper.setTo(array);
//mailMessage.setTo("iamzken@163.com");
helper.setFrom("1965571954@qq.com");
helper.setSubject(" This is my theme! ");
helper.setText("<p style='color:red;'> This is my content! </p>",true);
//Add attachments
ClassPathResource resource = new ClassPathResource("test.jpg");
helper.addAttachment("hello.jpg", resource);
//Set username, depending on your situation
//senderImpl.setUsername("iamzken@163.com");
senderImpl.setUsername("1965571954@qq.com");
senderImpl.setPassword(" Your password "); //Depending on your situation, set the password
/* Properties prop = new Properties();
prop.put(" mail.smtp.auth ", " true "); //Set this parameter to true and let the server authenticate that the username and password are correct
prop.put(" mail.smtp.timeout ", " 25000 ");
senderImpl.setJavaMailProperties(prop);*/
//Send E-mail
senderImpl.send(mailMessage);
System.out.println(" Email sent successfully .. ");
}
}
I hope this article has been helpful to your Java programming.