给你一份我的看看:import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;public class Mailer
{
private String mailHost;
private String fromAddress;
private String fromName;
private String toAddress;
private String toName;
private String toCC;
private String toBCC;
private String subjectMsg;
private String bodyMsg; private boolean isIDValidate;
private String UserName;
private String Password; public Mailer(String mailHost,String fromAddress,String fromName,String toAddress,String toName,String subjectMsg,String bodyMsg,String UserName,String Password,boolean isIDValidate)
{
this.mailHost = mailHost;
this.fromAddress = fromAddress;
this.fromName = fromName;
this.toAddress = toAddress;
this.toName = toName;
this.subjectMsg = subjectMsg;
this.bodyMsg = bodyMsg;
this.UserName = "test";
this.Password = "test";
this.isIDValidate = isIDValidate;
this.toCC = "";
this.toBCC = "";
} public Mailer(String mailHost,String fromAddress,String fromName,String toAddress,String toName,String UserName,String Password,boolean isIDValidate)
{
this(mailHost,fromAddress,fromName,toAddress,toName,"","",UserName,Password,isIDValidate);
} public Mailer(String mailHost,String fromAddress,String toAddress,String UserName,String Password,boolean isIDValidate)
{
this(mailHost,fromAddress,"",toAddress,"",UserName,Password,isIDValidate);
} public Mailer()
{
this("","","","","",false);
} public void setMailHost(String mailHost)
{
this.mailHost = mailHost;
} public void setFromAddress(String fromAddress)
{
this.fromAddress = fromAddress;
} public void setFromName(String fromName)
{
this.fromName = fromName;
} public void setToAddress(String toAddress)
{
this.toAddress = toAddress;
} public void setToName(String toName)
{
this.toName = toName;
}
public void setToCC(String toCC)
{
this.toCC = toCC;
}
public void setToBCC(String toBCC)
{
this.toBCC = toBCC;
}
public void setSubjectMsg(String subjectMsg)
{
this.subjectMsg = subjectMsg;
} public void setBodyMsg(String bodyMsg)
{
this.bodyMsg = bodyMsg;
} public void setUserName(String UserName)
{
this.UserName = UserName;
} public void setPassword(String Password)
{
this.Password = Password;
} public void setIsIDValidate(boolean isIDValidate)
{
this.isIDValidate = isIDValidate;
} public boolean sendmail()
{
try
{
Properties props = new Properties();
props.put("mail.smtp.host",mailHost); Session mailSession = null; //if (this.isIDValidate)
//{
props.put("mail.smtp.auth","true");
mailSession = Session.getInstance(props,new MailAuthenticator(this.UserName,this.Password));
// mailSession.setPasswordAuthentication(new URLName(mailHost),new PasswordAuthentication(UserName,Password));
//}
//else
//{
// mailSession = Session.getInstance(props,null);
//} Address from = new InternetAddress(fromAddress,fromName); Address to = new InternetAddress(toAddress,toName);
Address CC,BCC;
Message msg = new MimeMessage(mailSession);
bodyMsg+="test"; msg.setContent(bodyMsg,"text/plain;charset=GBK");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO,to);
if(!this.toCC.equals("")){
CC = new InternetAddress(toCC,"");
msg.setRecipient(Message.RecipientType.CC,CC);
}
if(!this.toBCC.equals("")){
BCC = new InternetAddress(toBCC,"");
msg.setRecipient(Message.RecipientType.BCC,BCC);
}
msg.setSubject(subjectMsg); Transport.send(msg);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
} public static void main(String [] args)
{
Mailer sendmail = new Mailer();
sendmail.setMailHost("smtp.sina.com.cn");
sendmail.setFromAddress("[email protected]");
sendmail.setFromName("test");
sendmail.setToAddress("[email protected]");
sendmail.setToName("test1");
sendmail.setSubjectMsg("test");
sendmail.setBodyMsg("This is test about send email.");
sendmail.setUserName("");//设置验证用户名
sendmail.setPassword("");//设置验证密码
sendmail.setIsIDValidate(false);//设置是否要身份验证
if (sendmail.sendmail())
{
System.out.println("Send Succ!");
}
else
{
System.out.println("Send fail!");
}
}
}
import javax.mail.*;
import javax.mail.internet.*;public class Mailer
{
private String mailHost;
private String fromAddress;
private String fromName;
private String toAddress;
private String toName;
private String toCC;
private String toBCC;
private String subjectMsg;
private String bodyMsg; private boolean isIDValidate;
private String UserName;
private String Password; public Mailer(String mailHost,String fromAddress,String fromName,String toAddress,String toName,String subjectMsg,String bodyMsg,String UserName,String Password,boolean isIDValidate)
{
this.mailHost = mailHost;
this.fromAddress = fromAddress;
this.fromName = fromName;
this.toAddress = toAddress;
this.toName = toName;
this.subjectMsg = subjectMsg;
this.bodyMsg = bodyMsg;
this.UserName = "test";
this.Password = "test";
this.isIDValidate = isIDValidate;
this.toCC = "";
this.toBCC = "";
} public Mailer(String mailHost,String fromAddress,String fromName,String toAddress,String toName,String UserName,String Password,boolean isIDValidate)
{
this(mailHost,fromAddress,fromName,toAddress,toName,"","",UserName,Password,isIDValidate);
} public Mailer(String mailHost,String fromAddress,String toAddress,String UserName,String Password,boolean isIDValidate)
{
this(mailHost,fromAddress,"",toAddress,"",UserName,Password,isIDValidate);
} public Mailer()
{
this("","","","","",false);
} public void setMailHost(String mailHost)
{
this.mailHost = mailHost;
} public void setFromAddress(String fromAddress)
{
this.fromAddress = fromAddress;
} public void setFromName(String fromName)
{
this.fromName = fromName;
} public void setToAddress(String toAddress)
{
this.toAddress = toAddress;
} public void setToName(String toName)
{
this.toName = toName;
}
public void setToCC(String toCC)
{
this.toCC = toCC;
}
public void setToBCC(String toBCC)
{
this.toBCC = toBCC;
}
public void setSubjectMsg(String subjectMsg)
{
this.subjectMsg = subjectMsg;
} public void setBodyMsg(String bodyMsg)
{
this.bodyMsg = bodyMsg;
} public void setUserName(String UserName)
{
this.UserName = UserName;
} public void setPassword(String Password)
{
this.Password = Password;
} public void setIsIDValidate(boolean isIDValidate)
{
this.isIDValidate = isIDValidate;
} public boolean sendmail()
{
try
{
Properties props = new Properties();
props.put("mail.smtp.host",mailHost); Session mailSession = null; //if (this.isIDValidate)
//{
props.put("mail.smtp.auth","true");
mailSession = Session.getInstance(props,new MailAuthenticator(this.UserName,this.Password));
// mailSession.setPasswordAuthentication(new URLName(mailHost),new PasswordAuthentication(UserName,Password));
//}
//else
//{
// mailSession = Session.getInstance(props,null);
//} Address from = new InternetAddress(fromAddress,fromName); Address to = new InternetAddress(toAddress,toName);
Address CC,BCC;
Message msg = new MimeMessage(mailSession);
bodyMsg+="test"; msg.setContent(bodyMsg,"text/plain;charset=GBK");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO,to);
if(!this.toCC.equals("")){
CC = new InternetAddress(toCC,"");
msg.setRecipient(Message.RecipientType.CC,CC);
}
if(!this.toBCC.equals("")){
BCC = new InternetAddress(toBCC,"");
msg.setRecipient(Message.RecipientType.BCC,BCC);
}
msg.setSubject(subjectMsg); Transport.send(msg);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
} public static void main(String [] args)
{
Mailer sendmail = new Mailer();
sendmail.setMailHost("smtp.sina.com.cn");
sendmail.setFromAddress("[email protected]");
sendmail.setFromName("test");
sendmail.setToAddress("[email protected]");
sendmail.setToName("test1");
sendmail.setSubjectMsg("test");
sendmail.setBodyMsg("This is test about send email.");
sendmail.setUserName("");//设置验证用户名
sendmail.setPassword("");//设置验证密码
sendmail.setIsIDValidate(false);//设置是否要身份验证
if (sendmail.sendmail())
{
System.out.println("Send Succ!");
}
else
{
System.out.println("Send fail!");
}
}
}
这种方法不行呢
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*; public class SimpleSendMessage { public static void main(String[] args) { // Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from with
// valid information.
// host - must be a valid smtp server that you currently have
// access to.
// to - whoever is going to get your email
// from - whoever you want to be. Just remember that many smtp
// servers will validate the domain of the from address
// before allowing the mail to be sent.
String host = "smtp.163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "mail test";
String messageText = "I am sending a message using ";
boolean sessionDebug = false; // Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true"); //允许smtp校验
try {
Session session =Session.getInstance(props,new MailAuthenticator("yourName","password"));
PasswordAuthentication psw=new PasswordAuthentication("yourName","password");
session.setPasswordAuthentication(new URLName(host),psw);
//Transport transport = session.getTransport("smtp");
//transport.connect(host,"yeaker","quizhun");
//session.setPasswordAuthentication("yeaker","quizhun"); // Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
session.setDebug(sessionDebug); // Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText); // Hand the message to the default transport service
// for delivery.
Transport.send(msg);
}
catch(MessagingException mex){
mex.printStackTrace();
//System.out.println("can't send mail");
}
} //end main()
}
class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
} public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
}