Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package at.stadtverwaltung.server;
- import java.io.File;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import at.stadtverwaltung.client.Text;
- import at.stadtverwaltung.client.presenter.rpc.EmailService;
- import com.extjs.gxt.ui.client.widget.MessageBox;
- public class EmailServiceImpl extends DatabaseServiceImpl implements EmailService {
- private static final long serialVersionUID = 1L;
- @Override
- public boolean sendMail(String from, String[] to, String[] cc, String[] bcc, String subject, String text) {
- // TODO Auto-generated method stub
- return false;
- }
- private static void sendMailByJavaMail(String smtpHost, String from, String[] to, String[] cc, String[] bcc, String subject, String text, File[] attachments) {
- /**
- * Set up the mail
- */
- int nrOfAttachments = attachments.length;
- String smtpUser = "";
- String smtpPW = "";
- boolean useSmtpAuthentication = false;
- try {
- // Get system properties and session
- Properties props = System.getProperties();
- Session session = Session.getInstance(props);
- // Define message and it's parts
- MimeMessage message = new MimeMessage(session);
- MimeBodyPart textPart = new MimeBodyPart();
- MimeBodyPart[] attachmentPart = new MimeBodyPart[nrOfAttachments];
- for (int i = 0; i < nrOfAttachments; i++)
- attachmentPart[i] = new MimeBodyPart();
- // Build multipart message
- Multipart multipart = new MimeMultipart();
- multipart.addBodyPart(textPart);
- for (int i = 0; i < nrOfAttachments; i++)
- multipart.addBodyPart(attachmentPart[i]);
- message.setContent(multipart);
- /**
- * Fill the message properties
- */
- // Set the SMTP Host
- props.put("mail.smtp.host", smtpHost);
- // We want to close the connection immediately after sending
- props.put("mail.smtp.quitwait", "false");
- // We want to use encryption if needed
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.ssl.protocols", "SSLv3 TLSv1");
- // If authentication is needed we set it to true
- if (useSmtpAuthentication) props.put("mail.smtp.auth", "true");
- else props.put("mail.smtp.auth", "false");
- // Set all recipients of any kind (TO, CC, BCC)
- message.setFrom(new InternetAddress(from));
- for (int i = 0; i < to.length; i++) {
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
- }
- for (int i = 0; i < cc.length; i++) {
- message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
- }
- for (int i = 0; i < bcc.length; i++) {
- message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
- }
- // Set subject, text and attachment
- message.setSubject(subject);
- textPart.setText(text);
- for (int i = 0; i < nrOfAttachments; i++) {
- attachmentPart[i++].attachFile(attachments[i]);
- }
- /**
- * Send message (if no authentication is used, we use the short variant to send a mail
- */
- if (useSmtpAuthentication) {
- Transport transport = session.getTransport("smtp");
- try {
- transport.connect(smtpHost, smtpUser, smtpPW);
- transport.sendMessage(message, message.getAllRecipients());
- }
- finally {
- transport.close();
- }
- }
- else { // No SMTP Authentication
- Transport.send(message);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- if (e instanceof MessagingException) { // SMTP Error
- MessageBox.alert(Text.ERROR, Text.ERROR_MAIL, null);
- e.printStackTrace();
- }
- else { // Other Error
- MessageBox.alert(Text.ERROR, Text.ERROR_MAIL, null);
- e.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment