First you need to download a javax.mail package, import the package into the project, and then go directly to the code.
Package com.javaee.email;
Import java.util.Properties;
Import javax.mail.Address;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage; public class Sendmail_two {public static void main (string[] args) {String message = "This is the first email to be sent using Java
"; try {//The first parameter is a QQ mailbox server (different mailboxes are different servers), the second parameter is the sender of the message, the third is the sender's password, the fourth parameter is the mail recipient, and the fifth parameter is the message message sent SendMail ("smtp.qq.com
"," xxxxx@qq.com "," ******* "," xxxxxxx@qq.com ", message);
catch (Exception e) {e.printstacktrace (); }/** * @param args Add by Zxx, Feb 5, 2009 */public static void SendMail (String smtphost
, string from,string Password, string to,string message) throws exception{//TODO auto-generated method stub
Properties Props = new properties (); Props.setproperty ("Mail.smtp.auth", "true");
Props.setproperty ("Mail.transport.protocol", "SMTP");
Session session = Session.getinstance (props);
Session.setdebug (TRUE);
msg = new MimeMessage (session);
Msg.settext (message);
Msg.setfrom (New InternetAddress (from));
Transport transport = Session.gettransport ();
Transport.connect (SMTPHost, From.split ("@") [0], password);
Transport.sendmessage (msg, new Address[]{new internetaddress (To)});
Transport.close (); }
}
Send mail is so simple, it should be noted that different mail is different mail server, Sina's mail server is smtp.sina.com.
Also noteworthy is that if 454 authentication failed appears during the program's Operation , please open SMTP flag first! Error Resolution:
Enter the sender's mailbox, find the settings, find the account, and select Open the POP3/SMTP service in POP3/IMAP/SMTP.