إرسال بريد إلكتروني مع المرفقات بواسطة JavaMailSender من SpringFramework

هذه مقالة قصيرة "كيفية" حول إرسال رسائل البريد الإلكتروني مع SpringFramework. سأقدم أمثلة زوجين وسأظهر مشكلة شائعة .

إنها مهمة سهلة يمكن تنفيذها مثل هذا:

public void send(String subject, String from, String to, File file) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", file); javaMailSender.send(message); } 

لكن هذا الوضع ليس نموذجيًا عندما يعمل تطبيق الويب مع نظام الملفات مباشرةً. دعونا ننظر في المثال عندما ننشئ المستند في الذاكرة ونريد إرفاقه في بريدنا الإلكتروني.

هناك طريقتان في فئة MimeMessageHelper يمكن أن تساعدنا:

 public class MimeMessageHelper { ... public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) { ... } public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { ... } } 

دعونا نرى بعض الأمثلة.

1. المرفق هو واجهة InputStreamSource

إنه أمر صعب ، لأنه في هذه الحالة يمكن للمطور الحصول على IllegalArgumentException مع رسالة:

" يحتوي المورد الذي تم تمريره على دفق مفتوح: وسيطة غير صالحة. تتطلب JavaMail مصدر InputStreamSource ينشئ دفقًا جديدًا لكل مكالمة. "

يحدث هذا بسبب وجود فحص خاص في الأسلوب MimeMessageHelper # addAttachment () :

 if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) { throw new IllegalArgumentException( "Passed-in Resource contains an open stream: invalid argument. " + "JavaMail requires an InputStreamSource that creates a fresh stream for every call."); } 

على سبيل المثال ، يعود تطبيق InputStreamResource دائمًا صوابًا من الأسلوب isOpen () الذي يجعل من المستحيل استخدام هذا التطبيق كمرفق:

  public class InputStreamResource extends AbstractResource { //... @Override public boolean isOpen() { return true; } //... } 

مثال العمل هو:

 public void send() { try { final ByteArrayOutputStream stream = createInMemoryDocument("test document text"); final InputStreamSource attachment = new ByteArrayResource(stream.toByteArray()); sendMimeMessageWithAttachments( "subject", "random@random.com", "random@random.com", attachment); } catch (IOException | MailException | MessagingException e) { logger.warn(e.getMessage(), e); } } private void sendMimeMessageWithAttachments(String subject, String from, String to, InputStreamSource source) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", source); javaMailSender.send(message); } private ByteArrayOutputStream createInMemoryDocument(String documentBody) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(documentBody.getBytes()); return outputStream; } 

2. المرفق هو واجهة DataSource

هذا المثال لا يحتوي على مطبات واضحة جدا:

 public void send() { try { final ByteArrayOutputStream document = createInMemoryDocument("test document text"); final InputStream inputStream = new ByteArrayInputStream(document.toByteArray()); final DataSource attachment = new ByteArrayDataSource(inputStream, "application/octet-stream"); sendMimeMessageWithAttachments( "subject", "anonymous@xyz-mail.com", "anonymous@xyz-mail.com", attachment); } catch (IOException | MailException | MessagingException e) { logger.warn(e.getMessage(), e); } } private void sendMimeMessageWithAttachments(String subject, String from, String to, DataSource dataSource) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setReplyTo(from); helper.setText("stub", false); helper.addAttachment("document.txt", dataSource); javaMailSender.send(message); } 

سيكون من المفيد إلقاء نظرة على فصل فصل الربيع .

Source: https://habr.com/ru/post/ar439176/


All Articles