AppContextCos.xml configuration file
bean>
public static void main (String [] args) {ApplicationContext ac = new ClassPathXmlApplicationContext ( "appcontext.xml"); JavaMailSender mailSend = (JavaMailSender) ac.getBean ( "TestSendMessage"); MimeMessagePreparator mail = new MimeMessagePreparator () {public void prepare (MimeMessage message) throws MessagingException {// recipient message.setRecipient (Message.RecipientType.TO, new ( "renxianqi.2008 # 163.com") InternetAddress); // sender message.setFrom (new InternetAddress ( " Wujun1866 # 163.com ")); // Content Message.Settext (" this is a test! "); message.setSubject (" Title Title "); // Add attachment Multipart Multipart = new mimemultipart (); bodypart attachmentPart = New mimebodypart (); DataSource Source = New FileDataS Ource ("c: //1.bmp"); attachmentPart.setDataHandler (new data "); attachmentPart.setFileName (" 1.BMP "); //ttachmentPart.setText (" TEDDDDDD "); // sun. Misc.base64encoder enc = new sun.misc.base64encoder (); // messagebodypart.setFileName ("=? GBK? B?" enc.Encode (attachmentname.getbytes ()) "? ="); multipart.addbodypart AttachmentPart; Message.SetContent (Multipart);
}};
Try {mailsend.send (mail); system.out.print ("k");} catch (mailexception ex) {log.error ("Error:", EX);}}}
Use Javamail to send and receive emails, solve Chinese attachment problems
JavaMail is deeply in-depth