25号端口,,自己搞对了,,
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class SenderWithSMTPVer
{
String host="";
String user="";
String password="";
public void setHost(String host)
{
this.host=host;
}
public void setAccount(String user,String password)
{
this.user=user;
this.password=password;
}
public void send(String from,String to,String subject,String content)
{
Properties props=new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
try
{
Session mailSession=Session.getDefaultInstance(props);
mailSession.setDebug(true);
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
message.saveChanges();
Transport transport=mailSession.getTransport("smtp");
transport.connect(host,user,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
SenderWithSMTPVer sm=new SenderWithSMTPVer();
sm.setHost("smtp.126.com");
sm.setAccount("zhaijianhui","842111");
sm.send("[email protected]","[email protected]","hello2","this is the second test javamail");}
}