发送QQ邮件
package com.zyg.jas.common.tool.util;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
@Component
public class EmailUtil {
@Autowired
private JavaMailSender sender;
public static void sendMail(String emialFrom,String pwd,String emailTo,String title, String content) throws Exception{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.host", "smtp.qq.com");
props.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
msg.setSubject(title);
msg.setText(content);
msg.setFrom(new InternetAddress(emialFrom));
Transport transport = session.getTransport();
transport.connect(emialFrom, pwd);
InternetAddress to = new InternetAddress(emailTo);
InternetAddress [] to1 = {to};
transport.sendMessage(msg,to1);
transport.close();
}
public static void main(String[] args){
try {
sendMail("258519@qq.com","qq邮箱获取的授权码","194491@qq.com","jas验证吗", "111555");
} catch (Exception e) {
e.printStackTrace();
}
}
}