[Java] JavaMail Simple Case

Source: Internet
Author: User

NetEase offers a free smtp/pop3 service that can be used for programming tests, see what is POP3, SMTP, and IMAP for details?

Only need to have a netease email account, and open the SMTP/POP3 function of the account, you can send mail to the SMTP server through the program, and have the SMTP server to send to the recipient's mailbox, or through POP3 to get my mail NetEase mailbox.

Here is a simple case, using JavaMail to send mail to NetEase's SMTP server, then you can find a program in the recipient mailbox to send mail.

 PackageMail;Importjava.util.Properties;ImportJavax.mail.*;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage; Public classSendEmail { Public Static voidMain () {String to= "[Email protected]"; String from= "[Email protected]"; FinalString username = "[email protected]"; FinalString password = "Password"; String Host= "Smtp.163.com"; Properties Props=NewProperties (); Props.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", host); Props.put ("Mail.smtp.port", "25"); Session Session=session.getinstance (Props,NewJavax.mail.Authenticator () {protectedpasswordauthentication getpasswordauthentication () {return Newpasswordauthentication (username, password);                }        }); Try{Message Message=NewMimeMessage (session); Message.setfrom (Newinternetaddress (from));                        Message.setrecipients (Message.RecipientType.TO, Internetaddress.parse (to)); Message.setsubject ("Ssssssss222"); Message.settext ("Ttttttttttttttttt222");                        Transport.send (message); System.out.println ("Sent"); }Catch(Exception e) {System.out.println (e);  for(Stacktraceelement s:e.getstacktrace ()) {System.out.println (s); }        }    }}

Resources

Avamail api-sending Simple Email

[Java] JavaMail Simple Case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.