import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public static void sendMail(String to)
throws GeneralSecurityException, MessagingException {
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("Thanks,register success!");
msg.setContent(
"<a href='http://localhost:8000/fish/success.jsp'>","text/html;charset=UTF-8"); //想要加内容可以在这里添加
msg.setFrom(new InternetAddress("##@qq.com")); //##这里需要填写发送人的qq
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "##@qq.com",
"lxgmkkpvigqcfjcf"); //lxg....的参数就是qq邮箱的开启smtp服务的允许码,可以百度寻找如何获得
transport.sendMessage(msg, new Address[] { new InternetAddress(to) });
sendMail(email);
transport.close();
}
注:javax.mail的包需要到官网下载,可能编译时还会出现import com.sun.mail.util.MailSSLSocketFactory;这个找不到的错