http://www.csdn.net/Develop/read_article.asp?id=20621
我写的,希望有帮助
我写的,希望有帮助
解决方案 »
- 获取IP的方法出错,急,求解!
- SSH2 web 项目参数 如何初始化
- 关于在框架中屏蔽鼠标右键的问题
- 我用VB写的天气预报程序,大家来看一下!
- 谁有16位的MD5的源码,万分感谢~~~~~~~~~~~~~
- 有关c3p0的问题,十万火急。下面的程序还有什么问题呢?老是出空指针异常,急死我了
- 我是刚才留言的初学者,看到你的回复了不过能不能具体点,十分感谢!在线等
- 为何文件上传总出错(急,在线等)
- HELP!!!!jswdk如何显示汉字?对于汉字,总是显示乱码。
- 请问springboot分页数据在返回json数据时如何排除掉某些字段
- 新手求助?哪位高手帮我看看下面这段程序问题在哪里?执行后什么也不返回,也不报错。(在线等待)
- 如何使用jsp+javabean结构
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.163.com");
Session sess = Session.getDefaultInstance(props);
hostname=smtp.mail.yahoo.com.cn
username=baosenbj
password=team36253个参数按照有效的内容设置。
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;public final class Mailer extends Object implements Serializable { /* Bean Properties */
private String to = null;
private String from = null;
private String subject = null;
private String message = null;
public static Properties props = null;
public static Session session = null;
private static String username;
private static String password;
private static String smtphost = "";
private PrintWriter log; static {
/* Setting Properties for STMP host */
props = System.getProperties();
//props.put("mail.smtp.host", smtphost);
//props.put("mail.smtp.auth","true");
//username = "";
//password = "";
} public void Mailer(){
this.setConfig();
/* Setting Properties for STMP host */
//props = System.getProperties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
}
/* Setter Methods */
public void setTo(String to) {
this.to = to.trim();
} public void setFrom(String from) {
this.from = from.trim();
} public void setSubject(String subject) {
this.subject = subject;
} public void setMessage(String message) {
this.message = message;
}
/* Sends Email */
public void sendMail() throws Exception {
this.setConfig();
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
subAuthenticator subauth = new subAuthenticator(username,password);//smtp authenticator
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
session = Session.getInstance(props,subauth);
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
Transport.send(message);
}
catch(AuthenticationFailedException e1){
System.out.println("SMTP认证出错!");
}
catch (MessagingException e) {
throw new Exception(e.getMessage());
}
} /* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if((this.to == null)||(this.from == null)||(this.subject == null)||(this.message == null))
return false; if((this.to.indexOf("@") == -1) ||(this.to.indexOf(".") == -1))
return false; if((this.from.indexOf("@") == -1) ||(this.from.indexOf(".") == -1))
return false; return true;
} /**
* 读取属性,完成参数设置
*
*/
public void setConfig(){
InputStream is = getClass().getResourceAsStream("/mail.properties");
Properties props = new Properties();
try {
props.load(is);
}
catch (Exception e) {
System.err.println("不能读取属性文件. " + "请确保mail.properties在CLASSPATH指定的路径中");
return;
}
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
smtphost = props.getProperty("hostname");
System.out.println(smtphost);
if(smtphost == null){
log("没有指定SMTP服务器");
}
username = props.getProperty("username");
System.out.println(username);
password = props.getProperty("password");
}
} /**
* 将文本信息写入日志文件
* @param msg 日志
*/
private void log(String msg) {
log.println(new Date() + ": " + msg);
} /**
* 将文本信息与异常写入日志文件
* @param e 异常
* @param msg 日志
*/
private void log(Throwable e, String msg) {
log.println(new Date() + ": " + msg);
e.printStackTrace(log);
} /**
*此内部类定义了smtp认证方法
*/
public class subAuthenticator extends Authenticator{
private String userName;
private String password;
public subAuthenticator(String user,String pass){
userName=user;
password=pass;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName,password);
}
}
}