日期:2014-05-20 浏览次数:21045 次
class SendMail implements ActionListener{
public void actionPerformed(ActionEvent evt){
try
{
Properties props=new Properties(); //获得系统属性
props.put("mail.smtp.auth", "true"); //设置身份验证为true
//创建邮件会话对象,并启动身份验证
Session session=Session.getDefaultInstance(props,new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(usernameField.getText(),
passwordField.getText());
}
});
final Message msg=new MimeMessage(session);
Address to=new InternetAddress(toField.getText());
Address from=new InternetAddress(fromField.getText());
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
// msg.setSentDate(new Data());
msg.setSubject(transferChinese(subjectField.getText()));
msg.setContent(message.getText(), "GBK");
//启动线程来处理
Runnable r=new Runnable(){
public void run()
{
try
{
Transport.send(msg);
}
catch(Exception e){
e.printStackTrace();
}
}
};
Thread t=new Thread(r);
t.start();
message.setText("");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
props.put("mail.smtp.host","smtp.qq.com");//设置邮件服务器的域名或IP
------解决方案--------------------
private String host = "smtp.qq.com";
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Authenticator auth = new Email_Autherticator();
Session session = Session.getInstance(props, auth);
------解决方案--------------------
楼主给你发站内了