我写得你看看吧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 = "mallmaster";
this.Password = "mallmaster";
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);
     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");
sendmail.setFromAddress("[email protected]");
sendmail.setFromName("test");
sendmail.setToAddress("[email protected]");
sendmail.setToName("test");
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!");
}
}
}

解决方案 »

  1.   

    sina不提供pop3,smtp服务,
    你可以用freemail.yeah.net.
      

  2.   

    sina不提供pop3,smtp服务?
    那为什么用outlook可以发送接受?
    它还不是用socket来通信?
      

  3.   

    谢谢jcq指教,我已成功发送,接分吧。
      

  4.   

    sina的必须验证,你的用户名和密码没有设置,如果你用[email protected]作为发送邮箱的话,那么你的用户就设为“test”,密码就是你的登陆密码。如果不行的话去http://www.21active.com/DataStorage/TechInfo/List.asp?TechType=1&TitleID=78
    看看会有收获的。