package sendMail;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class SendMainl {

private static final String SMTP_HOST="smtp.sohu.com";
private static final String SENDER_NAME="北京甚美慧科软件开发公司";
private static final String SENDER_EMAIL_ADDRESS="[email protected]"; public void sendConfirmation(){
StringBuffer message=new StringBuffer();
message.append("用户名称:");
message.append("密码:");
        
sendMessage("[email protected]","请查收您在古玩网的登陆密码!",message.toString());
}
protected void sendMessage(String recipient,String subject,String message){
Properties props=new Properties();
props.put("mail.smtp.auth","true");
props.put("mail.host",SMTP_HOST);
        props.put("mail.smtp.user", "zbl-123");
        props.put("mail.smtp.password", "你的密码!");
Session session=Session.getDefaultInstance(props,null);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(SENDER_EMAIL_ADDRESS,SENDER_NAME));
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(recipient));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
Transport transport = session.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),props.getProperty("mail.smtp.user"),props.getProperty("mail.smtp.password"));//2
            transport.sendMessage(msg,msg.getAllRecipients());
}catch(Exception e){
System.out.println(e);
}

}public static void main(String[] args){
SendMainl sendMail=new SendMainl();
sendMail.sendConfirmation();

}
}



解决方案 »

  1.   

    SMTP--发送邮件和pop3/imap接收邮件协议信息必需配置正确,发、接邮件才能正常。二楼的代码已经说明清楚了。
      

  2.   

    装个winmailserver.xml如下
              <Resource name="mail/Session" auth="Container"
                        type="javax.mail.Session"/>
              <ResourceParams name="mail/Session">
                <parameter>
                  <name>mail.smtp.host</name>
                  <value>localhost</value>
                  <name>mail.smtp.auth</name>
                  <value>true</value>
                  <name>mail.smtp.user</name>
                  <value>lovejava</value>
                  <name>mail.smtp.password</name>
                  <value>123</value>
                              
                </parameter>
              </ResourceParams>