Java Email Send

Source: Internet
Author: User

Today, I encapsulated the e-mail sent, made an external interface, currently supports text/html type, intends to support the direct text type and HTML file type tomorrow, first upload the current encapsulation code. (2016/8/29).

I wrote this code when the text garbled phenomenon, adjusted for one hours did not find the problem, in English can be sent normally, but contains Chinese is garbled and sometimes send mail failed. But one hours after the adjustment of their own suddenly good, no matter what I change the encoding type can be normal decoding, I compared to the original code is also a touch, I do not think about it, and there is no problem found.

I am Maven managed project, so import two jar package dependencies in Pom.xml file

<!--send email-->    <dependency>      <groupId>commons-email</groupId>      < artifactid>commons-email</artifactid>      <version>1.1</version>    </dependency>      <dependency>          <groupId>javax.activation</groupId>          <artifactid>activation </artifactId>          <version>1.1.1</version>      </dependency>

Create a SendMail class package data

 PackageCom.yuyang.Common;ImportOrg.apache.commons.mail.MultiPartEmail;ImportJavax.mail.Message;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeBodyPart;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMultipart;ImportJavax.swing.plaf.PanelUI;Importjava.util.Date;Importjava.util.Properties;/*** Created by Yangyu on 16/8/29.*/ Public classSendMail {Private StaticProperties props =NULL;//initializing linked mail session information//Mail Delivery Protocol    Private Final StaticString PROTOCOL = "SMTP"; //SMTP mail server    Private Final StaticString HOST = "smtp.163.com"; //SMTP mail server default port    Private Final StaticString PORT = "25"; //whether to require identity authentication    Private Final StaticString Is_auth = "true"; //whether to enable debug mode (enable debug mode to print a response message when the client interacts with the server)    Private Final StaticString Is_enabled_debug_mod = "true"; Static{Props=NewProperties (); Props.setproperty ("Mail.transport.protocol", PROTOCOL); Props.setproperty ("Mail.smtp.host", HOST); Props.setproperty ("Mail.smtp.port", PORT); Props.setproperty ("Mail.smtp.auth", Is_auth); Props.setproperty ("Mail.debug", Is_enabled_debug_mod); }    /*** * send a simple HTML message **/      Public Static voidSendtextemail (String to,string subject,string content)throwsexception{//Create session Instance objectSession session =session.getdefaultinstance (props); //create an object to send information to and join the configuration fileMimeMessage message =NewMimeMessage (session); //Set SenderMessage.setfrom (NewInternetAddress ("[Email protected]")); //Set ThemeMessage.setsubject (subject); //Set RecipientMessage.setrecipient (MimeMessage.RecipientType.TO,Newinternetaddress (to)); //Set Send TimeMessage.setsentdate (NewDate ()); //Set HTML content to message body, specify MIME type text/html type, and specify character encoding to Utf-8message.setcontent (content,"Text/html;charset=utf-8"); //save and generate the final message contentmessage.savechanges (); //Send mail//Get Transport Instance ObjectTransport Transport =Session.gettransport (); //Open ConnectionTransport.connect ("[Email protected]", "blue208115501"); //pass the Message object to the transport object and send it outtransport.sendmessage (Message, message.getallrecipients ()); //Close ConnectionTransport.close (); }}

Finally, I tested it with a junit4.

Import Com.yuyang.Common.SendMail; Import org.junit.Test; /**  */Publicclass  sendmailtest    {    @Testpublic void throws Exception {        sendmail.sendtextemail ("[email protected]", "Test", "<span style= ' color:red; ' > This is your verification code:123123</span> ");}    }

Java Email Send

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.