📄 mailsender.java
字号:
package mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.PasswordAuthentication;
public class MailSender {
//默认构造函数
public MailSender() {
setUsername("scutsyb");
setSMTPhostName("smtp.163.com");
setMailTo("cnkenio@163.com");
setSubject("Procar System.");
setContent("Thank you for support.");
}
//只定义发送对象,其他使用默认值
public MailSender(String mailto) {
setUsername("scutsyb");
setPassword("20052005");
setSMTPhostName("smtp.163.com");
setMailTo(mailto);
setSubject("Procar System.");
setContent("Thank you for support.");
}
//定义所有参数的构造函数(用于认证发送)
public MailSender(String user,String pwd,String smtp,String mailto,String sub,String cont) {
setUsername(user);
setPassword(pwd);
setSMTPhostName(smtp);
setMailTo(mailto);
setSubject(sub);
setContent(cont);
}
// 定义必要参数的构造函数(用于认证发送)
public MailSender(String mailto, String sub, String cont) {
setUsername("scutsyb");
setPassword("20052005");
setSMTPhostName("smtp.163.com");
setMailTo(mailto);
setSubject(sub);
setContent(cont);
}
//定义必要参数(用于非认证发送)
public MailSender( String user,String smtp,String mailto,String sub,String cont) {
setUsername(user);
setSMTPhostName(smtp);
setMailTo(mailto);
setSubject(sub);
setContent(cont);
}
private String username;//用户账号
private String password;//用户密码
private String SMTPhostName;//SMTP服务器
private String MailTo;//发送目标邮箱
private String Subject;//邮件主题
private String Content;//邮件内容
public void setUsername(String username)
{
this.username = username;
}
public void setPassword(String password)
{
this.password = password;
}
public void setSMTPhostName(String SMTPhostName)
{
this.SMTPhostName = SMTPhostName;
}
public void setMailTo(String MailTo)
{
this.MailTo = MailTo;
}
public void setSubject(String Subject)
{
this.Subject = Subject;
}
public void setContent(String Content)
{
this.Content = Content;
}
//发送需要认证的邮件
public void SendAuthMail()
{
try{
Properties props = new Properties();
Session sendMailSession;
Transport transport;
Authenticator authenticator = new EmailAuthenticator(username,password);
props.put("mail.smtp.host", SMTPhostName);
props.put("mail.smtp.user",username);
props.put("mail.smtp.password",password);
props.put("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props, authenticator);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(username+"@163.com"));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(MailTo));
newMessage.setSubject(Subject);
newMessage.setSentDate(new Date());
newMessage.setText(Content);
transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage);
}
catch(MessagingException m)
{
System.out.println(m.toString());
}
}
//发送不认证的邮件
public void SendNoAuthMail()
{
try{
Properties props = new Properties();
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", SMTPhostName);
sendMailSession = Session.getInstance(props, null);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(username));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(MailTo));
newMessage.setSubject(Subject);
newMessage.setSentDate(new Date());
newMessage.setText(Content);
transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage);
}
catch(MessagingException m)
{
System.out.println(m.toString());
}
}
}
//认证类
class EmailAuthenticator extends javax.mail.Authenticator{
public EmailAuthenticator(String username,String password) {
setUsername(username);
setPassword(password);
}
private String username;
private String password;
public void setUsername(String username)
{
this.username = username;
}
public void setPassword(String password)
{
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -