|
1 RegisterViewBean.java
[php]
/*
* Created on 2004-7-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package mail;
/**
* @author yzq
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RegisterViewBean
{
private String USER_ID;
private String PASSWORD;
private String COMPANY_NAME;
private String CONTACT_PERSON;
private String PHONE;
private String FAX;
private String AREA_CODE;
private String COUNTRY_CODE;
private String GENDER;
private String EMAIL;
private String HOMEPAGE_URL;
private String POSTCODE;
private String ADDRESS;
public RegisterViewBean(){}
public RegisterViewBean(String USER_ID,String PASSWORD,String COMPANY_NAME,String CONTACT_PERSON,String PHONE,String FAX,String AREA_CODE,String COUNTRY_CODE,String GENDER,String EMAIL,String HOMEPAGE_URL,String POSTCODE,String ADDRESS)
{
this.USER_ID = USER_ID;
this.PASSWORD = PASSWORD;
this.COMPANY_NAME = COMPANY_NAME;
this.CONTACT_PERSON = CONTACT_PERSON;
this.PHONE = PHONE;
this.FAX = FAX;
this.AREA_CODE = AREA_CODE;
this.COUNTRY_CODE = COUNTRY_CODE;
this.GENDER = GENDER;
this.EMAIL = EMAIL;
this.HOMEPAGE_URL = HOMEPAGE_URL;
this.POSTCODE = POSTCODE;
this.ADDRESS = ADDRESS;
}
public String getUSER_ID()
{
return USER_ID;
}
public void setUSER_ID(String value)
{
USER_ID = value;
}
public String getPASSWORD()
{
return PASSWORD;
}
public void setPASSWORD(String value)
{
PASSWORD = value;
}
public String getCOMPANY_NAME()
{
return COMPANY_NAME;
}
public void setCOMPANY_NAME(String value)
{
COMPANY_NAME = value;
}
public String getCONTACT_PERSON()
{
return CONTACT_PERSON;
}
public void setCONTACT_PERSON(String value)
{
CONTACT_PERSON = value;
}
public String getPHONE()
{
return PHONE;
}
public void setPHONE(String value)
{
PHONE = value;
}
public String getFAX()
{
return FAX;
}
public void setFAX(String value)
{
FAX = value;
}
public String getAREA_CODE()
{
return AREA_CODE;
}
public void setAREA_CODE(String value)
{
AREA_CODE = value;
}
public String getCOUNTRY_CODE()
{
return COUNTRY_CODE;
}
public void setCOUNTRY_CODE(String value)
{
COUNTRY_CODE = value;
}
public String getGENDER()
{
return GENDER;
}
public void setGENDER(String value)
{
GENDER = value;
}
public String getEMAIL()
{
return EMAIL;
}
public void setEMAIL(String value)
{
EMAIL = value;
}
public String getHOMEPAGE_URL()
{
return HOMEPAGE_URL;
}
public void setHOMEPAGE_URL(String value)
{
HOMEPAGE_URL = value;
}
public String getPOSTCODE()
{
return POSTCODE;
}
public void setPOSTCODE(String value)
{
POSTCODE = value;
}
public String getADDRESS()
{
return ADDRESS;
}
public void setADDRESS(String value)
{
ADDRESS = value;
}
}
[/php]
2 Register.java
[php]
/*
* Created on 2004-7-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package mail;
/**
* @author yzq
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
//import java.util.*;
public class Register
{
RegisterViewBean rvb = new RegisterViewBean();
String[] info ={
"USER_ID:",
"ASSWORD",
"COMPANY_NAME:",
"CONTACT_PERSON:",
"HONE:",
"FAX:",
"AREA_CODE:",
"COUNTRY_CODE:",
"GENDER:",
"EMAIL:",
"HOMEPAGE_URL:",
"OSTCODE:",
"ADDRESS:"
};
public static void main(String args[])
{
new Register().regist();
}
public void regist()
{
int i = 0;
String userInput;
for(i=0;i<info.length;i++)
{
System.out.println(info);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
try
{
while((userInput = stdIn.readLine()) != null)
{
setRVB(i,userInput);
System.out.println("register"+info+userInput);
break;
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
System.out.println(rvb.getUSER_ID()+"has success register,mail will be send...");
new Msgsend(rvb).sendMsg("admin@yzq.com",rvb.getEMAIL());
}
public void setRVB(int i,String s)
{
switch(i)
{
case 0: rvb.setUSER_ID(s);
case 1: rvb.setPASSWORD(s);
case 2: rvb.setCOMPANY_NAME(s);
case 3: rvb.setCONTACT_PERSON(s);
case 4: rvb.setPHONE(s);
case 5: rvb.setFAX(s);
case 6: rvb.setAREA_CODE(s);
case 7: rvb.setCOUNTRY_CODE(s);
case 8: rvb.setGENDER(s);
case 9: rvb.setEMAIL(s);
case 10: rvb.setHOMEPAGE_URL(s);
case 11: rvb.setPOSTCODE(s);
case 12: rvb.setADDRESS(s);
}
}
}
[/php]
3 Msgsend.java
[php]
/*
* Created on 2004-7-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package mail;
/**
* @author yzq
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
//import java.io.*;
import java.text.*;
//import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class Msgsend
{
String subject = null;
String Text = null;
String mailhost = null;
public Msgsend(RegisterViewBean rb)
{
mailhost = "localhost";
Text = getUserText(rb);
subject = "欢迎光临";
}
public String sendMsg(String from,String to)
{
boolean debug = false;
String err = null;
try
{
Properties props = System.getProperties();
if(mailhost != null)
{
props.put("mail.smtp.host",mailhost);
}
Session session = Session.getDefaultInstance(props,null);
if(debug)
{
session.setDebug(true);
}
Message msg = new MimeMessage(session);
if(from != null)
{
msg.setFrom(new InternetAddress(from));
}
else
{
msg.setFrom();
}
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setText(Text);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Mail was sent successfully!");
}
catch(Exception e)
{
e.printStackTrace();
err = e.toString();
err = "注册成功,但是邮件服务出现故障,Email:"+to;
}
return err;
}
public String getUserText(RegisterViewBean rb)
{
String userText = "";
String nowDate = DateFormat.getDateInstance().format(new Date());
userText = rb.getCONTACT_PERSON()+"先生/小姐:\n"
+"您好!\n 欢迎您注册为傲雪寒梅网的用户,以下是您的注册资料.\n"
+"公司名称:"+rb.getCOMPANY_NAME()+"\n"
+"联系人:"+rb.getCONTACT_PERSON()+"\n"
+"电话:"+rb.getPHONE()+"\n"
+"地址:"+rb.getADDRESS()+"\n"
+"邮编:"+rb.getPOSTCODE()+"\n"
+"用户名:"+rb.getUSER_ID()+"\n"
+"你的密码是:"+rb.getPASSWORD()+"\n"
+"傲雪寒梅网络公司";
return userText;
}
}
[/php]
编译没问题,但是运行却说Could not connect SMTP host
这个问题是自己的原因还是其他?望大家指点,谢谢
注:
请注意发贴标题 |
|