List of usage examples for javax.mail.internet MimeMessage setFrom
public void setFrom(String address) throws MessagingException
From source file:org.apache.lens.server.query.QueryEndNotifier.java
/** Send mail. * * @param host the host * @param port the port * @param email the email * @param mailSmtpTimeout the mail smtp timeout * @param mailSmtpConnectionTimeout the mail smtp connection timeout */ public static void sendMail(String host, String port, Email email, int mailSmtpTimeout, int mailSmtpConnectionTimeout) throws Exception { Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.timeout", mailSmtpTimeout); props.put("mail.smtp.connectiontimeout", mailSmtpConnectionTimeout); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(email.getFrom())); for (String recipient : email.getTo().trim().split("\\s*,\\s*")) { message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); }//w w w . j av a 2 s . com if (email.getCc() != null && email.getCc().length() > 0) { for (String recipient : email.getCc().trim().split("\\s*,\\s*")) { message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(recipient)); } } message.setSubject(email.getSubject()); message.setSentDate(new Date()); MimeBodyPart messagePart = new MimeBodyPart(); messagePart.setText(email.getMessage()); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messagePart); message.setContent(multipart); Transport.send(message); }
From source file:org.apache.hupa.server.utils.TestUtils.java
/** * Creates a new mime message.//from ww w . j a va 2 s .c o m * It is possible to specify which parts to create (text, html or both) and * the number of attachments * * @param session * @param text * @param html * @param nfiles * @return * @throws MessagingException * @throws IOException */ public static Message createMockMimeMessage(Session session, String text, String html, int nfiles) throws MessagingException, IOException { ArrayList<FileItem> items = new ArrayList<FileItem>(); for (int i = 1; i <= nfiles; i++) { FileItem fileItem; fileItem = TestUtils.createMockFileItem("file_" + i + ".bin"); items.add(fileItem); } MimeMessage message = new MimeMessage(session); message.setFrom(MessageUtils.getRecipients("from@dom.com")[0]); message.setRecipients(RecipientType.TO, MessageUtils.getRecipients("to@dom.com")); message.setRecipients(RecipientType.CC, MessageUtils.getRecipients("cc@dom.com")); message.setRecipients(RecipientType.BCC, MessageUtils.getRecipients("bcc@dom.com")); message.setSubject("Subject"); return SendMessageBaseServiceImpl.composeMessage(message, text, html, items); }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Sends an email in text-format in iso-8859-1-encoding * /*www .j a v a 2 s. com*/ * @param subject the subject of the new mail * @param message the content of the mail * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendMail(String subject, String message, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "iso-8859-1"); msg.setSentDate(new Date()); msg.setText(message, "iso-8859-1"); Transport.send(msg); } catch (Exception e) { log.error("Error sending mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Sends an email in text-format in iso-8859-1-encoding * //w ww . j a v a2 s .c o m * @param subject the subject of the new mail * @param message the content of the mail * @param from the sender-address * @param to the receiver-address(es) * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendMail(String subject, String message, String from, String[] to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); for (int i = to.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.TO, to[i]); } msg.setSubject(subject, "iso-8859-1"); msg.setSentDate(new Date()); msg.setText(message, "iso-8859-1"); Transport.send(msg); } catch (Exception e) { log.error("Error sending mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * //www. jav a 2 s . co m * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "iso-8859-1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(alternativeTextMessage); multiPart.addBodyPart(bodyPart); bodyPart = new MimeBodyPart(); bodyPart.setContent(htmlMessage, "text/html"); multiPart.addBodyPart(bodyPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.yccheok.jstock.alert.GoogleMail.java
private static MimeMessage createEmail(String to, String cc, String from, String subject, String bodyText) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); InternetAddress tAddress = new InternetAddress(to); InternetAddress cAddress = Utils.isNullOrEmpty(cc) ? null : new InternetAddress(cc); InternetAddress fAddress = new InternetAddress(from); email.setFrom(fAddress); if (cAddress != null) { email.addRecipient(javax.mail.Message.RecipientType.CC, cAddress); }/*from w w w. ja va2s . c om*/ email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress); email.setSubject(subject); email.setText(bodyText); return email; }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * // www. j av a2 s . c o m * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * // w w w . ja v a2 s .c o m * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String[] to, String[] cc, String[] bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && cc.length != 0) { for (int i = cc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.CC, cc[i]); } } if (bcc != null && bcc.length != 0) { for (int i = bcc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.BCC, bcc[i]); } } if (to != null && to.length != 0) { for (int i = to.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.TO, to[i]); } } msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:com.sat.common.CustomReport.java
/** * Sendmail.//from w w w.ja va 2 s. co m * * @param msg * the msg * @throws AddressException * the address exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void sendmail(String msg) throws AddressException, IOException { ConfigFileHandlerManager miscConfigFileHandlerManager = new ConfigFileHandlerManager(); miscConfigFileHandlerManager.loadPropertiesBasedonPropertyFileName("com.cisco.lms.lms"); Validate miscValidate = new Validate(); String from = miscValidate.readsystemvariable("MAIL.FROM"); String host = miscValidate.readsystemvariable("MAIL.SMTP.HOST"); String to = miscValidate.readsystemvariable("MAIL.TO"); String subject = miscValidate.readsystemvariable("MAIL.SUBJECT"); String personal = miscValidate.readsystemvariable("MAIL.FROM.PERSONAL"); // Mail to details Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); javax.mail.Session session = javax.mail.Session.getDefaultInstance(properties); // compose the message try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from, personal)); message.addRecipients(Message.RecipientType.TO, to); message.setSubject(subject); Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(msg, "text/html"); mp.addBodyPart(htmlPart); message.setContent(mp); Transport.send(message); System.out.println(msg); System.out.println("message sent successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } catch (Exception e) { System.out.println("\tException in sending mail to configured mailers list"); } }
From source file:org.latticesoft.util.resource.MessageUtil.java
public static void sendEmail(String fromAddr, String toAddr, String subject, String body, Properties mailConfig) {//from ww w.j ava 2 s . c om try { //Here, no Authenticator argument is used (it is null). //Authenticators are used to prompt the user for user //name and password. Session session = Session.getDefaultInstance(mailConfig); MimeMessage message = new MimeMessage(session); //the "from" address may be set in code, or set in the //config file under "mail.from" ; here, the latter style is used message.setFrom(new InternetAddress(fromAddr)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddr)); message.setSubject(subject); message.setText(body); Transport.send(message); } catch (MessagingException me) { if (log.isErrorEnabled()) { log.error(me); } } }