📄 emailbean.java
字号:
package myMailSender; import java.io.IOException;import java.io.PrintWriter;import java.util.Properties;import javax.mail.*;import javax.mail.internet.*;import javax.servlet.*;import javax.servlet.http.*;public class EmailBean { //defaults private final static String DEFAULT_CONTENT = "Unknown content"; private final static String DEFAULT_SUBJECT= "Unknown subject"; private static String DEFAULT_SERVER = null; private static String DEFAULT_TO = null; private static String DEFAULT_FROM = null; private static String DEFAULT_MBOX= null; private static String DEFAULT_PROTOCOL= null; private static String DEFAULT_PASSWD= null; private static String DEFAULT_USER= null; static{ java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("myMailSender.mailDefaults"); DEFAULT_SERVER = bundle.getString("DEFAULT_SERVER"); DEFAULT_TO = bundle.getString("DEFAULT_TO"); DEFAULT_FROM = bundle.getString("DEFAULT_FROM"); System.out.println("DEFAULT_SERVER: " + DEFAULT_SERVER); } //JavaBean properties private String smtpHost; private String to; private String from; private String content; private String subject; private String user; private String passwd; private String protocol; private String mbox; public void sendMessage() throws Exception { Properties properties = System.getProperties(); //populate the 'Properties' object with the mail //server address, so that the default 'Session' //instance can use it. properties.put("mail.smtp.host", smtpHost); properties.put("mail.smtp.from", from); properties.put("mail.transport.protocol", protocol); //使用邮箱服务器如 SMTP.TOM.COM 发送邮件需要用户认证 //Session session = Session.getDefaultInstance(properties); properties.put( "mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(user,passwd); Session session = Session.getInstance(properties, auth); session.setDebug(true); Message mailMsg = new MimeMessage(session);//a new email message InternetAddress[] addresses = null; try { if (to != null) { //throws 'AddressException' if the 'to' email address //violates RFC822 syntax addresses = InternetAddress.parse(to, false); mailMsg.setRecipients(Message.RecipientType.TO, addresses); } else { throw new MessagingException("The mail message requires a 'To' address."); } if (from != null) { mailMsg.setFrom(new InternetAddress(from)); } else { throw new MessagingException( "The mail message requires a valid 'From' address."); } if (subject != null) mailMsg.setSubject(subject); if (content != null) mailMsg.setText(content); //Finally, send the mail message; throws a 'SendFailedException' //if any of the message's recipients have an invalid address Transport.send(mailMsg); } catch (Exception exc) { throw exc;} }//sendMessage public void setSmtpHost(String host){ if (check(host)){ this.smtpHost = host; } else { this.smtpHost = EmailBean.DEFAULT_SERVER; } } public void setTo(String to){ if (check(to)){ this.to = to; } else { this.to = EmailBean.DEFAULT_TO; } } public void setFrom(String from){ if (check(from)){ this.from = from; } else { this.from = EmailBean.DEFAULT_FROM; } } public void setContent(String content){ if (check(content)){ this.content = content; } else { this.content = EmailBean.DEFAULT_CONTENT; } } public void setSubject(String subject){ if (check(subject)){ this.subject = subject; } else { this.subject = EmailBean.DEFAULT_SUBJECT; } } public void setUser(String user){ if (check(user)){ this.user = user; } else { this.user = EmailBean.DEFAULT_USER; } } public void setPasswd(String passwd){ if (check(passwd)){ this.passwd = passwd; } else { this.passwd = EmailBean.DEFAULT_PASSWD; } } public void setProtocol(String protocol){ if (check(protocol)){ this.protocol = protocol; } else { this.protocol = EmailBean.DEFAULT_PROTOCOL; } } public void setMbox(String mbox){ if (check(mbox)){ this.mbox = mbox; } else { this.mbox = EmailBean.DEFAULT_MBOX; } } private boolean check(String value){ if(value == null || value.equals("")) return false; return true; }}class SMTPAuthenticator extends javax.mail.Authenticator { private String user; private String passwd; public SMTPAuthenticator(String user,String passwd){ this.user=user; this.passwd=passwd; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication (user, passwd); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -