自动发送我认为不可能真正实现,还是需要人工操作的。
你可以自己写个bean的。package mail;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class SendMail{
private String from;
private String to;
private String subject;
private String content;
private String smtp;
private String user;
private String pass;
private String mess;
public void sendMail(){
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.host",smtp);
props.setProperty("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);
}
});
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content);
transport = sendMailSession.getTransport();
transport.send(newMessage);
transport.close();
this.mess="信件发送成功!";
}catch(SendFailedException e){
System.out.println(e.toString());
this.mess=e.toString();
}catch(MessagingException e){
System.out.println(e.toString());
this.mess=e.toString();
}
}
public void setFrom(String from){
this.from=from;
}
public void setTo(String to){
this.to=to;
}
public void setSubject(String subject){
this.subject=subject;
}
public void setContent(String content){
this.content=content;
}
public void setSmtp(String smtp){
this.smtp=smtp;
}
public void setUser(String user){
this.user=user;
}
public void setPass(String pass){
this.pass=pass;
}
public String getMess(){
return this.mess;
}
}这是一个发信的bean。希望可对你有帮助。
你可以自己写个bean的。package mail;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class SendMail{
private String from;
private String to;
private String subject;
private String content;
private String smtp;
private String user;
private String pass;
private String mess;
public void sendMail(){
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.host",smtp);
props.setProperty("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);
}
});
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
newMessage.setText(content);
transport = sendMailSession.getTransport();
transport.send(newMessage);
transport.close();
this.mess="信件发送成功!";
}catch(SendFailedException e){
System.out.println(e.toString());
this.mess=e.toString();
}catch(MessagingException e){
System.out.println(e.toString());
this.mess=e.toString();
}
}
public void setFrom(String from){
this.from=from;
}
public void setTo(String to){
this.to=to;
}
public void setSubject(String subject){
this.subject=subject;
}
public void setContent(String content){
this.content=content;
}
public void setSmtp(String smtp){
this.smtp=smtp;
}
public void setUser(String user){
this.user=user;
}
public void setPass(String pass){
this.pass=pass;
}
public String getMess(){
return this.mess;
}
}这是一个发信的bean。希望可对你有帮助。
比如网站注册,注册成功触发发信事件 或调用发信的JAVABEAN就可以了。