Java implements the email sending function, and java mail sends emails
(Http://www.cnblogs.com/zhangdiIT/p/8184293.html copy Original)
Mailbox verification is a very common function. Basically, every website will use it. java also has a dedicated jar to process mail sending and other services, here is just a simple implementation of the mail sending function, the specific jar package will not be provided, I Will paste all the packages to be referenced, so that you can copy them easily. Go straight to the topic:
Step 1: encapsulate the sender account and password
Import javax. mail. authenticator; import javax. mail. passwordAuthentication;/*** sender account PASSWORD * @ author zhangdi **/public class MailAuthenticator extends Authenticator {public static String USERNAME = ""; public static String PASSWORD = ""; public MailAuthenticator () {}protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (USERNAME, PASSWORD );}}
Step 2: Provide sending operations
Import java. util. date; import java. util. properties; import javax. mail. message; import javax. mail. multipart; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeBodyPart; import javax. mail. internet. mimeMessage; import javax. mail. internet. mimeMultipart;/*** mail sending operation class ** @ author zhangdi **/public class MailOperation {/*** send mail * @ Param user sender's email address * @ param password authorization code (note that it is not the email logon password) * @ param host * @ param from sender * @ param to recipient's email * @ param subject * @ param content * @ return success successful sending failure failed sending * @ throws Exception */public String sendMail (String user, string password, String host, String from, String to, String subject, String content) throws Exception {if (! = Null) {Properties props = System. getProperties (); props. put ("mail. smtp. host ", host); props. put ("mail. smtp. auth "," true "); MailAuthenticator auth = new MailAuthenticator (); MailAuthenticator. USERNAME = user; MailAuthenticator. PASSWORD = password; Session session = Session. getInstance (props, auth); session. setDebug (true); try {MimeMessage message = new MimeMessage (session); message. setFrom (new Int ErnetAddress (from); if (! To. trim (). equals ("") message. addRecipient (Message. recipientType. TO, new InternetAddress (. trim (); message. setSubject (subject); MimeBodyPart mbp1 = new MimeBodyPart (); // body mbp1.setContent (content, "text/html; charset = UTF-8"); Multipart mp = new MimeMultipart (); // entire Email: Body + attachment mp. addBodyPart (mbp1); // mp. addBodyPart (mbp2); message. setContent (mp); message. setSentDate (new Date (); message. saveChanges (); Transport trans = session. getTransport ("smtp"); trans. send (message); System. out. println (message. toString ();} catch (Exception e) {e. printStackTrace (); return "failure";} return "success" ;}else {return "failure ";}}}
Step 3: Test
Public static void main (String [] args) {MailOperation operation = new MailOperation (); String user = "your email address"; String password = "your email authorization code "; string host = "smtp.163.com"; String from = "your email address"; String to = "target email address"; // recipient String subject = "enter email subject "; // The email content StringBuffer sb = new StringBuffer (); String yzm = RandomUtil. getRandomString (6); sb. append ("<! DOCTYPE> "+" <div bgcolor = '# f1fcfa' style = 'border: 1px solid # d9f4ee; font-size: 14px; line-height: 22px; color: #005aa0; padding-left: 1px; padding-top: 5px; padding-bottom: 5px; '> <span style = 'font-weight: bold;'> tips: </span> "+" <div style = 'width: 950px; font-family: arial; '> welcome to the NET micro-activity. Your registration code is: <br/>
Note: email sending is simple, but there are several points to note:
1. the sender's password is not the password of your logon email address, but the POP3, SMTP, or IMAP authorization code, for example, it is intercepted from the 163 email address );
2. If you use QQ mail as the sender's mailbox, You need to configure SSL certificates and other information, this is because of the requirements of QQ mail (of course, it is easy to use enterprise mail or other email providers such as 163 );
3. If the email address provided by other email service providers is used as the sender, you must pay attention to some sensitive words when editing the email content, if your email contains sensitive content, the email service provider will not send the email to you;
4. It can be called in the main method, but if you want to deploy it in a web Container (such as tomcat), you need to introduce two jar packages and