发送邮件的工具类
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class SendEmailUtils {
public static String myEmailAccount = "";
public static String myEmailPassword = "";
public static String myEmailSMTPHost = "smtp.qq.com";
public static int sendMail(String username,String cEmail,String subject,String content) throws Exception{
int createMimeMessage = 0;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(myEmailAccount, username, "UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(cEmail, "进", "UTF-8"));
message.setSubject(subject, "UTF-8");
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
createMimeMessage = 1;
return createMimeMessage;
}
}