Java implementation SMTP mail sending

Source: Internet
Author: User

First, the preparatory work

first you need to have a send mailbox, the general mailbox has SMTP, POP3 services, such as QQ mailbox, landing QQ mailbox Open SMTP Service, open is the server will prompt you to set up a separate password, this password is the normal login password with the mailbox, this is the SMTP login to use the password, Therefore, it is necessary to note the SMTP independent password, the program will be used.
(the preparation is so simple, just have a mailbox to open the SMTP service)
Posted here QQ Mailbox Settings SMTP location: Login Email------Click "Settings"-------click on the "Account" tab, turn down to see the following interface

Second, in general, the sending of mail is only intermediate operation, so you can use multi-threading, open a new thread to send mail, the main program can continue to execute down.

1 Importjava.util.Properties;2 3 ImportJavax.mail.Authenticator;4 Importjavax.mail.MessagingException;5 Importjavax.mail.PasswordAuthentication;6 Importjavax.mail.Session;7 ImportJavax.mail.Transport;8 ImportJavax.mail.Message.RecipientType;9 Importjavax.mail.internet.InternetAddress;Ten ImportJavax.mail.internet.MimeMessage; One  A  Public classSendmailthreadextendsthread{ -      -     PrivateString Mailadr; the     PrivateString content; -     PrivateString subject; -      -      PublicSendmailthread (String Mailadr, string subject, string content) { +         Super(); -          This. Mailadr =Mailadr; +          This. Content =content; A          This. Subject =subject; at     } -  - @Override -      Public voidrun () { -         //TODO auto-generated Method Stub -         Super. Run (); in SendMail (mailadr,subject,content); -     } to      +     /* - * MAILADR recipient address the * Subject Mail Header * * Content Message text contents $      */Panax Notoginseng      Public voidsendMail (String mailadr,string subject,string content) { -         //Configure environment properties for sending messages the         FinalProperties props =NewProperties (); +         //indicates that SMTP sends a message and authentication is required AProps.put ("Mail.smtp.auth", "true"); theProps.put ("Mail.smtp.host", "smtp.qq.com"); +         //SMTP login account, password, need to turn on SMTP login -Props.put ("Mail.user", "[email protected]"); $         //access to the SMTP service needs to provide the password, not the mailbox login password, generally have a separate SMTP login password $Props.put ("Mail.password", "XXXXX"); -          -         //Building authorization information for SMTP authentication theAuthenticator Authenticator =NewAuthenticator () { - @OverrideWuyi             protectedpasswordauthentication getpasswordauthentication () { the                 //user name, password -String userName = Props.getproperty ("Mail.user"); WuString Password = props.getproperty ("Mail.password"); -                 return Newpasswordauthentication (userName, password); About             } $         }; -          -         //Create a mail session using environment properties and authorization information -Session mailsession =session.getinstance (props, authenticator); A         //Create a mail message +MimeMessage message =Newmimemessage (mailsession); the         //Set Sender -         Try{ $internetaddress form =Newinternetaddress ( theProps.getproperty ("Mail.user")); the message.setfrom (form); the  the             //Set Recipient -InternetAddress to =Newinternetaddress (MAILADR); in message.setrecipient (recipienttype.to, to); the              the             //set up cc About             //internetaddress cc = new InternetAddress ("[email protected]"); the             //message.setrecipient (recipienttype.cc, CC); the  the             //set Secret send, other recipients cannot see the BCC email address +             //internetaddress bcc = new InternetAddress ("[email protected]"); -             //message.setrecipient (recipienttype.cc, bcc); the             Bayi             //set the message header the Message.setsubject (subject); the             //set the content body of a message -Message.setcontent (Content, "Text/html;charset=utf-8"); -             //Send mail the transport.send (message); the}Catch(messagingexception e) { the e.printstacktrace (); the         } -     } the  the      Public Static voidMain (string[] args) { the         //TODO auto-generated Method Stub94Sendmailthread d =NewSendmailthread ("[Email protected]", "Test message Header", "Test mail 001 <br> content ..."); the D.start (); theSYSTEM.OUT.PRINTLN ("program continues ..."); the     }98  About}

Third, the main program calls

New Sendmailthread ("Email address", "Mail title", "Mail Content"); Mail.start ();
Supplement 01: The above program is a single recipient recipient, if you need bulk mail can modify the program, the following post mass mailing method
// mass mode one, with array New Internetaddress[2];adds[new internetaddress ("[Email protected]"); adds[New InternetAddress ("[Email protected]"); Message.setrecipients (recipienttype.to, adds); // mass mode two, directly with a string of multiple addresses with commas separated message.setrecipients (recipienttype.to, "[email protected],[email protected]");
Supplement 02: The Above program is a demo with QQ mailbox sent, you can also use NetEase and other mailboxes, modify this sentence can:
Props.put ("Mail.smtp.host", "smtp.163.com");

However, the test found that NetEase seems to only send to the NetEase mailbox, other mailboxes can not be sent, QQ mailbox will be able to send to any other mailbox.

Supplement 03: The program can print send information to find the problem when debugging; Create a message reply to the session and add this statement:
Mailsession.setdebug (true); 

The above content reference from the network, self-organized as above.

Java implementation SMTP mail sending

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.