C H AP TE R 8 SEN DI NG E -M AI L in Java

Painting Data Matrix 2d barcode in Java C H AP TE R 8 SEN DI NG E -M AI L

C H AP TE R 8 SEN DI NG E -M AI L
Data Matrix ECC200 Encoder In Java
Using Barcode creation for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
Read Data Matrix 2d Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Note MIME is essentially a mechanism for encoding binary files into text for transmission over mediums
Making Quick Response Code In Java
Using Barcode encoder for Java Control to generate, create Quick Response Code image in Java applications.
www.OnBarcode.com
Create Barcode In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
that do not understand binary data. In the early days of e-mail transmissions, not all mail servers would correctly handle binary files and so the encoding was necessary. Although the mechanism is no longer necessary for this specific reason, MIME has become the accepted standard and must therefore be used for sending binary data by e-mail. The standard has also been adopted in other circumstances, and related parts of the standard are used for other purposes, notably for identifying file types. As a result, the acronym does not automatically indicate any connection with e-mail when used in other contexts.
UPC-A Supplement 2 Generator In Java
Using Barcode generation for Java Control to generate, create UCC - 12 image in Java applications.
www.OnBarcode.com
EAN128 Generator In Java
Using Barcode generator for Java Control to generate, create GS1 128 image in Java applications.
www.OnBarcode.com
The interface is shown in Listing 8-4 and is mostly concerned with the manipulation of MIME messages. However, it extends MailSender, so as a matter of convenience you can use a JavaMailSender implementation in any context where you need a MailSender implementation.
Printing Barcode In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Leitcode Generation In Java
Using Barcode printer for Java Control to generate, create Leitcode image in Java applications.
www.OnBarcode.com
Listing 8-4. The Spring JavaMailSender Interface
Create Data Matrix In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create DataMatrix image in Visual Studio .NET applications.
www.OnBarcode.com
Paint Data Matrix 2d Barcode In Java
Using Barcode maker for BIRT Control to generate, create Data Matrix ECC200 image in BIRT applications.
www.OnBarcode.com
public interface JavaMailSender extends MailSender { MimeMessage createMimeMessage(); MimeMessage createMimeMessage(InputStream contentStream) throws MailException; void send(MimeMessage mimeMessage) throws MailException; void send(MimeMessage[] mimeMessages) throws MailException; void send(MimeMessagePreparator mimeMessagePreparator) throws MailException; void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException; } All of the examples in this chapter use the JavaMailSenderImpl implementation of the JavaMailSender interface. Listing 8-5 shows the configuration of this bean in the application context configuration file.
Read ANSI/AIM Code 39 In Visual C#
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Encoding GS1 - 12 In None
Using Barcode encoder for Word Control to generate, create UPC-A Supplement 5 image in Office Word applications.
www.OnBarcode.com
Listing 8-5. Configuring a JavaMailSender Bean Implementation
Decode PDF 417 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Draw EAN 128 In None
Using Barcode creator for Online Control to generate, create UCC - 12 image in Online applications.
www.OnBarcode.com
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.example.com"/> </bean>
Universal Product Code Version A Generation In VS .NET
Using Barcode printer for .NET Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
Code 39 Extended Scanner In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
CHAPTER 8 SE NDING E-MAIL
Generate USS Code 39 In Objective-C
Using Barcode maker for iPad Control to generate, create Code 39 Extended image in iPad applications.
www.OnBarcode.com
Recognize UPC-A In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
You will need to amend the host value (highlighted in bold in Listing 8-5) to the domain name of your own SMTP mail gateway. You cannot send e-mail by using the examples in this chapter without access to a mail gateway. Setting up your own gateway is beyond the scope of this book.
Barcode Decoder In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Paint Linear In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create Linear image in Visual Studio .NET applications.
www.OnBarcode.com
Sending Plain Text
As a matter of convenience for this and the other examples in this chapter, I have created a base class for the DAO implementations that accept the property values that are common between all three. This class is shown in Listing 8-6.
Listing 8-6. An Abstract Base Class for the MailDAO Implementations
abstract public class AbstractMailDaoImpl implements EmailDao { protected String fromAddress; protected String rcptAddress; protected String subject; @Required public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } @Required public void setRcptAddress(String rcptAddress) { this.rcptAddress = rcptAddress; } @Required public void setSubject(String subject) { this.subject = subject; } abstract public void sendTimesheetUpdate(Timesheet timesheet); } Listing 8-7 shows a concrete implementation of the DAO derived from this class. Via the parent, we have access to the properties specifying the basic addressing information: the sender and the recipient. We also have access to the subject of the message. From the timesheet entity passed in by the service, we draw the account name of the user who carried out the update operation that the notification relates to.
C H AP TE R 8 SEN DI NG E -M AI L
The logic of the sendTimesheetUpdate() method is then implemented as you would expect: we create a SimpleMailMessage object to represent the e-mail to be sent, populate the address information and the subject, create a string for the text of the e-mail and populate that, and call the MailSender s send method passing in the composed message object. The Spring implementation takes care of the handshaking with the remote mail server. If for any reason this fails (if the server is offline, our Internet connection is down, or the server rejects the message for any other reason), a Spring MailException will be thrown, allowing us to report or recover from the problem.
Listing 8-7. An Implementation of a Simple Mail DAO
public class SimpleMailDaoImpl extends AbstractMailDaoImpl { private static final Logger log = Logger.getLogger(SimpleMailDaoImpl.class); private MailSender mailSender; public void sendTimesheetUpdate(final Timesheet timesheet) { try { final SimpleMailMessage message = new SimpleMailMessage(); message.setTo(rcptAddress); message.setFrom(fromAddress); message.setSubject(subject); message.setText("A timesheet has been updated by user: " + timesheet.getConsultant().getAccountName()); mailSender.send(message); } catch (MailException e) { log.error("Failed to send timesheet update message", e); throw e; } } @Required public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } } Listing 8-8 shows the configuration of this implementation; we have defined an abstract configuration bean that specifies the common properties of the beans to be configured, and then configured our specific implementation with this as its parent.
Copyright © OnBarcode.com . All rights reserved.