Package com.rupeng.javaMail;
Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMessage.RecipientType;
public class Test {
public static void Main (string[] args) throws Addressexception, Messagingexception {
Properties Props=new properties ();
Props.setproperty ("Mail.smtp.host", "smtp.sina.com");
Props.setproperty ("Mail.smtp.auth", "true");
1. Create Session Object
Session session=session.getdefaultinstance (props,new Authenticator ()
{
Protected Passwordauthentication getpasswordauthentication ()
{
return new Passwordauthentication ("Rupengfrom", "Rupengfrom");
}
});
Create and compose messages
Message Message=new MimeMessage (session);
Set Sender
Message.setfrom (New InternetAddress ("[email protected]");
Set recipient
Message.setrecipient (Recipienttype.to,new internetaddress ("[email protected]");
Set Title
Message.setsubject ("Test Mail");
Set the contents of a message
Message.settext ("Hello javamail1564856456456!");
3. Send mail
Transport.send (message);
System.out.println ("111");
}
}
Send text messages using Javaxmail