List of usage examples for javax.mail Transport send
public static void send(Message msg) throws MessagingException
From source file:com.zxy.commons.email.MailMessageUtils.java
/** * ??eml/*w ww.j a v a 2 s .co m*/ * * @param inputStream inputStream * @param from from * @param tos tos * @param properties * @param isCloseInputStream ???InputStream * @throws EmailException EmailException * @throws MessagingException MessagingException */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public static void sendEml(InputStream inputStream, String from, List<String> tos, Map<String, String> properties, boolean isCloseInputStream) throws EmailException, MessagingException { try { // inputStream = new SharedFileInputStream(mailPath); Session session = getEmail().getMailSession(); // session.getProperties().setProperty("mail.smtp.ehlo", "true"); MimeMessage message = new MimeMessage(session, inputStream); if (!Strings.isNullOrEmpty(from)) { message.setFrom(new InternetAddress(from)); } // for (Map.Entry<String, String> entry : properties.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); message.setHeader(name, value); } if (tos == null || tos.isEmpty()) { Transport.send(message); } else { InternetAddress[] internetAddresses = new InternetAddress[tos.size()]; int index = 0; for (String to : tos) { internetAddresses[index] = new InternetAddress(to); index++; } Transport.send(message, internetAddresses); } } finally { if (isCloseInputStream) { IOUtils.closeQuietly(inputStream); } } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Sends an email in text-format in iso-8859-1-encoding * /*from ww w . j a va 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:com.pinterest.deployservice.email.SMTPMailManagerImpl.java
public void send(String to, String title, String message) throws Exception { Session session = Session.getDefaultInstance(properties, getAuthenticator()); // Create a default MimeMessage object. MimeMessage mimeMessage = new MimeMessage(session); // Set From: header field of the header. mimeMessage.setFrom(new InternetAddress(adminAddress)); // Set To: header field of the header. mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field mimeMessage.setSubject(title);/*from w w w.j a v a 2 s . com*/ // Now set the actual message mimeMessage.setText(message); // Send message Transport.send(mimeMessage); }
From source file:ro.agrade.jira.qanda.listeners.DirectEmailMessageHandler.java
@Override protected void sendMail(String[] recipients, String subject, String message, String from) throws MessagingException { SMTPMailServer server = mailServerManager.getDefaultSMTPMailServer(); if (server == null) { LOG.debug("Email server is not configured. QandA is unable to send mails ..."); return;/*from ww w . j a v a 2s. c o m*/ } LOG.debug("Email message: initializing."); //Set the host smtp address Properties props = new Properties(); String proto = server.getMailProtocol().getProtocol(); props.put("mail.transport.protocol", proto); props.put("mail." + proto + ".host", server.getHostname()); props.put("mail." + proto + ".port", server.getPort()); String username = server.getUsername(); String password = server.getPassword(); Authenticator auth = null; if (username != null && password != null) { auth = new SMTPAuthenticator(username, password); props.put("mail." + proto + ".auth", "true"); } Session session; try { session = auth != null ? Session.getDefaultInstance(props, auth) : Session.getDefaultInstance(props); } catch (SecurityException ex) { LOG.warn("Could not get default session. Attempting to create a new one."); session = auth != null ? Session.getInstance(props, auth) : Session.getInstance(props); } // create a message MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); if (from == null) { from = server.getDefaultFrom(); } // set the from address if (from != null) { InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); } if (recipients != null && recipients.length > 0) { // set TO address(es) InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); } // Setting the Subject msg.setSubject(subject); // Setting text content MimeBodyPart contentPart = new MimeBodyPart(); contentPart.setContent(message, "text/html; charset=utf-8"); multipart.addBodyPart(contentPart); msg.setContent(multipart); Transport.send(msg); LOG.debug("Email message sent successfully."); }
From source file:NotificationMessage.java
static void sendMail(String toUser, Severity s) { // Recipient's email ID needs to be mentioned. //String to = "abhiyank@gmail.com";//change accordingly // Sender's email ID needs to be mentioned String from = "mdtprojectteam16@gmail.com";//change accordingly final String username = "mdtprojectteam16";//change accordingly final String password = "mdtProject16";//change accordingly // Assuming you are sending email through relay.jangosmtp.net String host = "smtp.gmail.com"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }/*from w w w .j ava 2 s . c om*/ }); try { // Create a default MimeMessage object. javax.mail.Message message1 = new MimeMessage(session); // Set From: header field of the header. message1.setFrom(new InternetAddress(from)); // Set To: header field of the header. message1.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toUser)); // Set Subject: header field message1.setSubject("Alert Message From Patient"); // Now set the actual message message1.setText(messageMap.get(s)); // Send message Transport.send(message1); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:com.gitlab.anlar.lunatic.server.ServerTest.java
private void sendMultiPartEmail(String from, String to, String subject, String body) throws MessagingException { Properties props = createEmailProps(serverPort); Session session = Session.getInstance(props); Message msg = createBaseMessage(from, to, subject, session); MimeBodyPart p1 = new MimeBodyPart(); p1.setText(body);/*w w w. j a va 2 s .c o m*/ MimeBodyPart p2 = new MimeBodyPart(); p2.setText("Second part"); Multipart mp = new MimeMultipart(); mp.addBodyPart(p1); mp.addBodyPart(p2); msg.setContent(mp); Transport.send(msg); }
From source file:com.github.aynu.mosir.core.enterprise.mail.MailServiceImpl.java
/** * ?/*from www . ja va 2s . c om*/ * @param message * @throws EnterpriseException ? */ private static void send(final Message message) throws EnterpriseException { Validate.notNull(message); try { Transport.send(message); } catch (final MessagingException e) { throw new EnterpriseException(e); } }
From source file:com.tdclighthouse.commons.mail.util.MailClient.java
public void sendMail(String from, String[] to, Mail mail) throws MessagingException, AddressException { // a brief validation if ((from == null) || "".equals(from) || (to.length == 0) || (mail == null)) { throw new IllegalArgumentException(); }//from ww w. j av a 2 s . c o m Session session = getSession(); // Define a new mail message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); for (int i = 0; i < to.length; i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } message.setSubject(mail.getSubject(), "utf-8"); // use a MimeMultipart as we need to handle the file attachments Multipart multipart = new MimeMultipart("alternative"); if ((mail.getMessageBody() != null) && !"".equals(mail.getMessageBody())) { // add the message body to the mime message BodyPart textPart = new MimeBodyPart(); textPart.setContent(mail.getMessageBody(), "text/plain; charset=utf-8"); // sets type to "text/plain" multipart.addBodyPart(textPart); } if (mail.getHtmlBody() != null) { BodyPart pixPart = new MimeBodyPart(); pixPart.setContent(mail.getHtmlBody(), "text/html; charset=utf-8"); multipart.addBodyPart(pixPart); } // add any file attachments to the message addAtachments(mail.getAttachments(), multipart); // Put all message parts in the message message.setContent(multipart); // Send the message Transport.send(message); }
From source file:com.app.mail.DefaultMailSender.java
@Override public void sendPasswordResetToken(String emailAddress, String passwordResetToken) { Session session = _authenticateOutboundEmailAddress(); try {//from w w w . j a v a2 s .co m Message emailMessage = _populatePasswordResetToken(emailAddress, passwordResetToken, session); Transport.send(emailMessage); } catch (Exception e) { _log.error("Unable to send password reset token", e); } }
From source file:com.srotya.tau.nucleus.qa.QAAlertRules.java
@Test public void testASMTPServerAvailability() throws UnknownHostException, IOException, MessagingException { MimeMessage msg = new MimeMessage(Session.getDefaultInstance(new Properties())); msg.setFrom(new InternetAddress("alert@srotya.com")); msg.setRecipient(RecipientType.TO, new InternetAddress("alert@srotya.com")); msg.setSubject("test mail"); msg.setContent("Hello", "text/html"); Transport.send(msg); MailService ms = new MailService(); ms.init(new HashMap<>()); Alert alert = new Alert(); alert.setBody("test"); alert.setId((short) 0); alert.setMedia("test"); alert.setSubject("test"); alert.setTarget("alert@srotya.com"); ms.sendMail(alert);//from ww w . j av a 2 s . c o m assertEquals(2, AllQATests.getSmtpServer().getReceivedEmailSize()); System.err.println("Mail sent"); }