Spring mail sending example

xiaoxiao2021-04-04  238

AppContextCos.xml configuration file

smtp.163.com mailbox password True

renxianqi .2008 @ 163.com wujun1866@163.com TEST

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

转载请注明原文地址:https://www.9cbs.com/read-131757.html

New Post(0)