package com.assess.util;import java.io.file;import Java.util.arraylist;import java.util.list;import Java.util.properties;import Javax.activation.datahandler;import Javax.activation.filedatasource;import Javax.mail.message;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;import javax.mail.internet.MimeUtility; public classSendmailutil {/** Refer to this article *http://www.cnblogs.com/xdp-gacl/p/4216311.html* * * smtp.sohu.com Sohu Mailbox Host * smtp.163.com 163 mailbox host, Default Port * smtp.qq.com QQ Mailbox Host **/ /** * FROM Sender Mailbox * passwd Sender Mailbox Password * To recipient mailbox * Subject subject * TXT content * files attachment * Host Service Host * Protocol Protocol (SMTP) * AUTH authentication (true) **/ public StaticBoolean Send (String from, String passwd,string to, string subject, string Txt,list<File>files, String host,string protocol,string Auth) { Try{Properties prop=NewProperties (); Prop.setproperty ("Mail.host", host); Prop.setproperty ("Mail.transport.protocol", protocol); Prop.setproperty ("Mail.smtp.auth", auth); //5 Steps to send a message using JavaMail//1. Create sessionSession session =session.getinstance (prop); //turn on the session debug mode so you can see the status of the program sending emailSession.setdebug (true); //2. Get Transport object by sessionTransport ts =Session.gettransport (); //3, using the mailbox user name and password to connect to the mail server,//when sending a message, the sender needs to submit the user name and password of the mailbox to the SMTP server .//the user name and password are verified before the message is sent to the recipient Normally. Ts.connect (host, from, passwd); //4. Create a messageMessage message = Createmixedmail (session, from, to, subject, txt, files); //5. Send mailts.sendmessage (message, message.getallrecipients ()); Ts.close (); }Catch(Exception E) {return false; } return true; } /** * send text and attachments to mail **/ public StaticMimeMessage Createmixedmail (Session session,string from, String to,string subject,string txt,list<file>Files) throws Exception {//Create a messageMimeMessage message =NewMimeMessage (session); //set basic information for a messageMessage.setfrom (NewInternetAddress ( from)); Message.setrecipient (Message.RecipientType.TO,Newinternetaddress (to)); Message.setsubject (subject); //BodyMimeBodyPart Text =NewMimeBodyPart (); Text.setcontent (txt,"Text/html;charset=utf-8"); //Image//MimeBodyPart image = new MimeBodyPart ();//image.setdatahandler (new datahandler (new filedatasource ("src\\check.png" ));//Image.setcontentid ("aaa.jpg"); //AccessoriesList<mimebodypart> Mimebodyparts =NewArraylist<mimebodypart>(); for(intI=0; I<files.size (); i++) {mimebodypart Attach=NewMimeBodyPart (); DataHandler DH=NewDataHandler (NewFiledatasource (files.Get(i))); Attach.setdatahandler (dh); Attach.setfilename (mimeutility.encodeword (dh.getname ())); Mimebodyparts.add (attach); } //describe relationships: body and pictureMimemultipart MP1 =NewMimemultipart (); Mp1.addbodypart (text); //Mp1.addbodypart (image);Mp1.setsubtype ("related"); //describe relationships: body and attachmentsMimemultipart MP2 =NewMimemultipart (); for(intI=0; I<mimebodyparts.size (); i++) {mp2.addbodypart (mimebodyparts.Get(i)); } //Mp2.addbodypart (attach2); //BodyPart representing the bodyMimeBodyPart content =NewMimeBodyPart (); Content.setcontent (mp1); Mp2.addbodypart (content); Mp2.setsubtype ("Mixed"); Message.setcontent (mp2); Message.savechanges (); //Message.writeto (new FileOutputStream ("d:\\mixedmail.eml")); //return to the created message returnmessage; }}
package com.assess.util;import java.io.file;import java.util.arraylist;import java.util.List; public classMain {Private StaticFinal String HOST ="smtp.163.com"; Private StaticFinal String PROTOCOL ="SMTP"; Private StaticFinal String AUTH ="true"; Private StaticFinal String user_name ="135****[email protected]";//Sender Mailbox Private StaticFinal String PASSWORD ="1234";//Password Private StaticFinal String Recriver ="139****[email protected]";//Recipient Mailbox public Static voidmain (string[] Args) {List<File> files =NewArraylist<file>(); File F=NewFile ("src\\ memo. rar"); File F2=NewFile ("Src\\check.png"); Files.add (f); Files.add (f2); if(sendmailutil.send (user_name, PASSWORD, recriver,"Topic Information","Content Information", files, HOST, PROTOCOL, AUTH)) {System. out. println ("Success"); } }}
Java Send email