2011-01-03 10 views
5

Jak utworzyć wiadomość e-mail zawierającą tekst i wersję HTML dla tej samej treści?Tworzenie wieloczęściowej wiadomości w formacie MIME Szablon wolnomatnika za pośrednictwem Spring 3 JavaMail

Oczywiście chciałbym wiedzieć, jak skonfigurować szablon wolnej gry lub nagłówek wiadomości, która zostanie wysłana.

Kiedy patrzę na źródła wiadomosc wieloczęściowe w formacie MIME, które otrzymuję w skrzynce odbiorczej co raz w podczas gdy to, co jest w środku:

This is a multi-part message in MIME format. 

------=_NextPart_000_B10D_01CBAAA8.F29DB300 
Content-Type: text/plain 
Content-Transfer-Encoding: 7bit 

...Text here... 

------=_NextPart_000_B10D_01CBAAA8.F29DB300 
Content-Type: text/html; 
charset="iso-8859-1" 
Content-Transfer-Encoding: quoted-printable 

<html><body> html code here ... </body></html> 

Odpowiedz

4

Jeżeli zauważysz jakiekolwiek niezgodności powiadom mnie wiedzieć. Musiałem to wydobyć z całkiem złożonego obiektu, dlatego tak to wygląda.

//some important imports 
import freemarker.template.Template; 
import org.springframework.mail.javamail.*; 
import org.springframework.context.*; 
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 
import javax.mail.internet.MimeMessage; 

private JavaMailSender mailSender; 
private MessageSource messageSource; 
private ExecutorService executor = Executors.newFixedThreadPool(50); 

MimeMessagePreparator preparator = new MimeMessagePreparator() { 
    public void prepare(MimeMessage mimeMessage) throws Exception { 
     MimeMessageHelper message = new MimeMessageHelper(mimeMessage); 

      message.setFrom(from); 
      message.setTo(recipient); 
      message.setSubject(subject); 

      // Now the message body. 
      Multipart mp = new MimeMultipart(); 

      BodyPart textPart = new MimeBodyPart(); 
      Template textTemplate = freemarkerConfig.getConfiguration().getTemplate(textEmailTemplate); // "/WEB-INF/emailText/*.ftl" 
      final StringWriter textWriter = new StringWriter(); 
      textEmailTemplate.process(modelMap, textWriter); 
      textPart.setText(textWriter.toString()); // sets type to "text/plain" 


      BodyPart pixPart = new MimeBodyPart(); 
      Template pixTemplate = freemarkerConfig.getConfiguration().getTemplate(pixEmailTemplate); // "/WEB-INF/emailPix/*.ftl" 
      final StringWriter pixWriter = new StringWriter(); 
      textEmailTemplate.process(modelMap, pixWriter); 
      pixPart.setContent(pixWriter.toString(), "text/html"); 

      // Collect the Parts into the MultiPart 
      mp.addBodyPart(textPart); 
      mp.addBodyPart(pixPart); 
      // Put the MultiPart into the Message 
      message.setContent(mp);     

    } 
}; 

executor.submit(new SendMail(preparator)); 

class SendMail extends Thread { 
    MimeMessagePreparator preparator; 

    SendMail(MimeMessagePreparator preparator) { 
     this.preparator = preparator; 
    } 

    public void run() { 
     mailSender.send(preparator); 
     } 
} 
1

Podczas korzystania Wiosna można zrobić:

String plainText = "MyPleinText"; 
    String htmlText = "<html><body><h1>MyHTML</h1></body></html>"; 
    MimeMessage message = this.mailSender.createMimeMessage(); 
    MimeMessageHelper helper = new MimeMessageHelper(message, true, MAIL_ENCODING); 
    helper.setText(plainText,htmlText); 

I będzie wykonać zadanie. Nie ma rzeczy Freemarker zaangażowanych.

MailSender może być:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
    <property name="host" value="${smtp.host}" /> 
    <property name="port" value="${smtp.port}" /> 
    <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 
Powiązane problemy