import java.util.Properties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class Test {
public void sentMail(){
try{
Properties p = new Properties();
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.timeout", 25000);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("10.16.77.240");
mailSender.setUsername("123456789@qq.com");
mailSender.setPassword("123456");
mailSender.setJavaMailProperties(p);
SimpleMailMessage smm = new SimpleMailMessage();
smm.setFrom(mailSender.getUsername());
smm.setTo("***@126.com");
smm.setSubject("Hello world");
smm.setText("Hello world via spring mail sender");
mailSender.send(smm);
}catch(Exception ex){
ex.printStackTrace();
System.out.println("ERROR");
}
}
public static void main(String[] args) {
Test test = new Test();
test.sentMail();
System.out.println("发送成功");
}
}
邮箱服务器必须进行授权,否则邮件发送不出去