SOURCE Download Address
Package com.in.smtp;
Import javax.mail.*;
Import java.util.*;
Import javax.mail.internet.*;
public class Emailforsmtp {
String host = "";
String username = "";
String password = "";
public void Sethost (String host) {
This.host = host;
}
public void setuserinformaition (String username,string password) {
This.username = Username;
This.password = password;
}
public void SendEmail (String sendpeople,string arrviedpeople,string emailname,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 msg = new MimeMessage (mailsession);
Msg.setfrom (New InternetAddress (sendpeople));
Msg.addrecipient (message.recipienttype.to,new internetaddress (arrviedpeople));//Recipient
Msg.setsubject (emailname);//Mail subject
Msg.settext (content);//Message contents
Msg.savechanges ();
Transport transport = Mailsession.gettransport ("SMTP");
Transport.connect (host, username, password);
Transport.sendmessage (MSG, msg.getallrecipients ());
Transport.close ();
catch (Addressexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchproviderexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Messagingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
--------------------------------------------------------------------------------------------------------------- ---
Client
Import COM.IN.SMTP.EMAILFORSMTP;
public class Client {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
EMAILFORSMTP EFS = new EMAILFORSMTP ();
Efs.sethost ("smtp.163.com");
Efs.setuserinformaition ("Manytao", "wangtao521");
Efs.sendemail ("
}
Ssss@163.com "," bbb_bbb@163.com "," AAAA "," bbbbb ");
}