How to implement the Java Send mail function
1. First download the local mail-sending software so that the test
1). After installing MailServer, add two local users, as follows
2). After installing Foxmail, add two users as before, and the user name is the same, use Foxmail to send and receive mail conveniently. Note: You must add users to the mailserver before sending, and the user name is consistent. Also as follows:
To be filled in as local when configured
2. Import the appropriate package
3. Use Java to implement email delivery
The code is as follows:
Import java.util.Properties;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import Javax.mail.Message.RecipientType;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
public class MailDemo1 {
public static void Main (string[] args) throws exception{
Properties prop = new properties ();
Prop.setproperty ("Mail.transport.protocol", "SMTP");//protocol
Prop.setproperty ("Mail.smtp.host", "localhost");//Host Name
Prop.setproperty ("Mail.smtp.auth", "true");//whether permission control is turned on
Prop.setproperty ("Mail.debug", "true");//If set to True the message will be printed when the message is sent
Create a session between a program and a mail server
Session session = Session.getinstance (prop);
Get Mail Object
Message msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress ("[email protected]");
Msg.setrecipients (recipienttype.to, New internetaddress[]{new internetaddress ("[email protected]")});
Msg.setsubject ("This is an email from a Java program");
Msg.settext ("This is the body of the message ... Content .... ");
Enter your account password
Transport trans = Session.gettransport ();
Trans.connect ("AA", "123");
Trans.sendmessage (MSG, msg.getallrecipients ());
}
}
Fourth Week job: Java implementation mail send