package test;
import java.util.Date;
import java.util.Properties;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* 发送邮件的测试程序(适用qq企业邮箱)
* 通过qq企业邮箱: xxx@qq.com 发送邮件
* @author fcl
*
*/
public class EmailTest {
public static final String EMAIL_FORM="support@xxxx.com";
public static final String EMAIL_PASSWORD="xxx";
public static void main(String args[]) throws Exception {
EmailTest mail = new EmailTest();
String email_to = "XXXX@qq.com";
mail.sendEmail("email_to", "fcl", "fcl");
}
public static void sendEmail(String email_to,String username,String name) throws Exception{
JavaMailSenderImpl senderimpl = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
senderimpl.setJavaMailProperties(properties);
// 设定mail server smtp.exmail.qq.com
senderimpl.setHost("smtp.exmail.qq.com");
senderimpl.setPort(465);
//设置用户名和密码
senderimpl.setUsername(EMAIL_FORM);
senderimpl.setPassword(EMAIL_PASSWORD);
// 1. 创建一封邮件
MimeMessage message = senderimpl.createMimeMessage();
// 2. From: 发件人
message.setFrom(new InternetAddress(EMAIL_FORM, "XXXXX", "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email_to, "普通用户", "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject("发现主题", "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent("这是发送的内容", "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
senderimpl.send(message);
System.out.println("Send success.....");
}
import java.util.Date;
import java.util.Properties;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* 发送邮件的测试程序(适用qq企业邮箱)
* 通过qq企业邮箱: xxx@qq.com 发送邮件
* @author fcl
*
*/
public class EmailTest {
public static final String EMAIL_FORM="support@xxxx.com";
public static final String EMAIL_PASSWORD="xxx";
public static void main(String args[]) throws Exception {
EmailTest mail = new EmailTest();
String email_to = "XXXX@qq.com";
mail.sendEmail("email_to", "fcl", "fcl");
}
public static void sendEmail(String email_to,String username,String name) throws Exception{
JavaMailSenderImpl senderimpl = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
senderimpl.setJavaMailProperties(properties);
// 设定mail server smtp.exmail.qq.com
senderimpl.setHost("smtp.exmail.qq.com");
senderimpl.setPort(465);
//设置用户名和密码
senderimpl.setUsername(EMAIL_FORM);
senderimpl.setPassword(EMAIL_PASSWORD);
// 1. 创建一封邮件
MimeMessage message = senderimpl.createMimeMessage();
// 2. From: 发件人
message.setFrom(new InternetAddress(EMAIL_FORM, "XXXXX", "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email_to, "普通用户", "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject("发现主题", "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent("这是发送的内容", "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
senderimpl.send(message);
System.out.println("Send success.....");
}