//邮件包mail.jar自己从网上下载一个吧
package thisMail;
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 =null;
transport=mailSession.getTransport("smtp");
if(transport==null)
{
System.out.println("999999");
}
//Transport transport = mailSession.getTransport("smtp");
System.out.println("111");
System.out.println(host);
System.out.println(user);
System.out.println(password);
transport.connect(host, user, password);
System.out.println("222");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("333");
transport.close();
}catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
SenderWithSMTPVer sm=new SenderWithSMTPVer();
sm.setHost("smtp.163.com");
sm.setAccount("userName","password");
sm.send("
[email protected]","
[email protected]","这是标题","这是内容");
}
}