List of usage examples for javax.mail.internet InternetAddress InternetAddress
public InternetAddress(String address) throws AddressException
From source file:ru.org.linux.util.EmailService.java
public void sendEmail(String nick, String email, boolean isNew) throws MessagingException { StringBuilder text = new StringBuilder(); text.append("?!\n\n"); if (isNew) {/*from ww w . j a v a 2s.com*/ text.append( "\t? ? http://www.linux.org.ru/ ?? ?? ?,\n"); } else { text.append( "\t? ? http://www.linux.org.ru/ ?? ?,\n"); } text.append(" ? ? (e-mail).\n\n"); text.append( " ? ? ? ?: '"); text.append(nick); text.append("'\n\n"); text.append( "? , - ? ? ?!\n\n"); if (isNew) { text.append( "? ??? ? http://www.linux.org.ru/,\n"); text.append( " ? ? ? ? ?.\n\n"); } else { text.append( "? ? ? ? http://www.linux.org.ru/,\n"); text.append(" ? ? .\n\n"); } String regcode = User.getActivationCode(configuration.getSecret(), nick, email); text.append( "? ?? http://www.linux.org.ru/activate.jsp\n\n"); text.append(" : ").append(regcode).append("\n\n"); text.append(" ?!\n"); Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage emailMessage = new MimeMessage(mailSession); emailMessage.setFrom(new InternetAddress("no-reply@linux.org.ru")); emailMessage.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email)); emailMessage.setSubject("Linux.org.ru registration"); emailMessage.setSentDate(new Date()); emailMessage.setText(text.toString(), "UTF-8"); Transport.send(emailMessage); }
From source file:com.mobileman.projecth.business.MailManagerTest.java
/** * /*w ww . ja v a 2 s .c o m*/ * @throws Exception */ @Test public void sendTellAFriendMessage() throws Exception { String sender = "mitglied@projecth.com"; mailManager.sendTellAFriendMessage("Jan Novak", "test@test.com", "receiver1@test.com,receiver2@test.com", "body"); List<WiserMessage> messages = wiser.getMessages(); assertEquals(3, messages.size()); assertEquals(new InternetAddress(sender), messages.get(0).getMimeMessage().getFrom()[0]); assertEquals(new InternetAddress("receiver1@test.com"), messages.get(0).getMimeMessage().getRecipients(Message.RecipientType.TO)[0]); assertEquals("Mitteilung von projecth", messages.get(0).getMimeMessage().getSubject()); assertEquals(new InternetAddress(sender), messages.get(1).getMimeMessage().getFrom()[0]); assertEquals(new InternetAddress("receiver2@test.com"), messages.get(1).getMimeMessage().getRecipients(Message.RecipientType.TO)[0]); assertEquals("Mitteilung von projecth", messages.get(1).getMimeMessage().getSubject()); assertEquals(new InternetAddress(sender), messages.get(2).getMimeMessage().getFrom()[0]); assertEquals(new InternetAddress("test@test.com"), messages.get(2).getMimeMessage().getRecipients(Message.RecipientType.TO)[0]); assertEquals("Mitteilung von projecth", messages.get(2).getMimeMessage().getSubject()); mailManager.sendTellAFriendMessage(null, "test@test.com", "receiver1@test.com,receiver2@test.com", "body"); }
From source file:com.app.test.mail.DefaultMailSenderTest.java
@Test public void testPopulateCancellationMessage() throws Exception { _initializeVelocityTemplate(_clazz, _classInstance); Method populateMessage = _clazz.getDeclaredMethod("_populateMessage", String.class, String.class, String.class, Session.class); populateMessage.setAccessible(true); Message message = (Message) populateMessage.invoke(_classInstance, "user@test.com", "Cancellation Successful", "cancellation_email.vm", _session); Assert.assertEquals("Auction Alert <test@test.com>", message.getFrom()[0].toString()); Assert.assertEquals("Cancellation Successful", message.getSubject()); Assert.assertEquals(_CANCELLATION_EMAIL, message.getContent()); InternetAddress[] internetAddresses = new InternetAddress[1]; internetAddresses[0] = new InternetAddress("user@test.com"); Assert.assertArrayEquals(internetAddresses, message.getRecipients(Message.RecipientType.TO)); }
From source file:eu.scape_project.planning.application.Feedback.java
/** * Method responsible for sending feedback per mail. * /* www . j a v a2s . c o m*/ * @param userEmail * email address of the user. * @param userComments * comments from the user * @param location * the location of the application where the error occurred * @param applicationName * the name of the application * @throws MailException * if the feedback could not be sent */ public void sendFeedback(String userEmail, String userComments, String location, String applicationName) throws MailException { try { Properties props = System.getProperties(); props.put("mail.smtp.host", config.getString("mail.smtp.host")); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(config.getString("mail.from"))); message.setRecipient(RecipientType.TO, new InternetAddress(config.getString("mail.feedback"))); message.setSubject("[" + applicationName + "] from " + location); StringBuilder builder = new StringBuilder(); builder.append("Date: ").append(dateFormat.format(new Date())).append("\n\n"); // User info if (user == null) { builder.append("No user available.\n\n"); } else { builder.append("User: ").append(user.getUsername()).append("\n"); if (user.getUserGroup() != null) { builder.append("Group: ").append(user.getUserGroup().getName()).append("\n"); } } builder.append("UserMail: ").append(userEmail).append("\n\n"); // Comments builder.append("Comments:\n"); builder.append(SEPARATOR_LINE); builder.append(userComments).append("\n"); builder.append(SEPARATOR_LINE).append("\n"); message.setText(builder.toString()); message.saveChanges(); Transport.send(message); log.debug("Feedback mail sent successfully to {}", config.getString("mail.feedback")); } catch (MessagingException e) { log.error("Error sending feedback mail to {}", config.getString("mail.feedback"), e); throw new MailException("Error sending feedback mail to " + config.getString("mail.feedback"), e); } }
From source file:com.freemarker.mail.GMail.java
public boolean sendMailUsingJavaMailAPI(String to, String message1, HttpServletRequest req, String mid, String createdby, String pname, String mname) { String FinalMessage = new FreeMarkerMailTemplateCreater().createAndReturnTemplateDataMapping(message1, getTemplateLocation(req), mid, createdby, pname, mname); String from = "analytixdstest@gmail.com"; final String username = "analytixdstest@gmail.com"; final String password = "analytix000"; 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"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }/*from ww w .j a va 2 s. co m*/ }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setContent(FinalMessage, "text/html; charset=utf-8"); message.saveChanges(); message.setSubject("Analytix Test Mail"); Transport.send(message); return true; } catch (MessagingException e) { return false; } }
From source file:Sender.java
/** Do the work: send the mail to the SMTP server. */ public void doSend() { // We need to pass info to the mail server as a Properties, since // JavaMail (wisely) allows room for LOTS of properties... Properties props = new Properties(); // Your LAN must define the local SMTP server as "mailhost" // for this simple-minded version to be able to send mail... props.put("mail.smtp.host", "mailhost"); // Create the Session object session = Session.getDefaultInstance(props, null); session.setDebug(true); // Verbose! try {/* w w w . ja v a 2 s . c om*/ // create a message mesg = new MimeMessage(session); // From Address - this should come from a Properties... mesg.setFrom(new InternetAddress("nobody@host.domain")); // TO Address InternetAddress toAddress = new InternetAddress(message_recip); mesg.addRecipient(Message.RecipientType.TO, toAddress); // CC Address InternetAddress ccAddress = new InternetAddress(message_cc); mesg.addRecipient(Message.RecipientType.CC, ccAddress); // The Subject mesg.setSubject(message_subject); // Now the message body. mesg.setText(message_body); // XXX I18N: use setText(msgText.getText(), charset) // Finally, send the message! Transport.send(mesg); } catch (MessagingException ex) { while ((ex = (MessagingException) ex.getNextException()) != null) { ex.printStackTrace(); } } }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testSaveNewMessage() throws IOException, MessagingException { File outputFile = File.createTempFile("mailUtilsTest", ".eml"); outputFile.deleteOnExit();/*from w w w. j a v a 2 s . co m*/ MimeMessage message = new MimeMessage(MailSession.getDefaultSession()); message.addFrom(new InternetAddress[] { new InternetAddress("test@example.com") }); message.addRecipients(RecipientType.TO, "recipient@example.com"); message.setContent("test body", "text/plain"); message.saveChanges(); MailUtils.writeMessage(message, new FileOutputStream(outputFile)); MimeMessage loadedMessage = MailUtils.loadMessage(outputFile); String recipients = ArrayUtils.toString(loadedMessage.getRecipients(RecipientType.TO)); assertEquals("{recipient@example.com}", recipients); String from = ArrayUtils.toString(loadedMessage.getFrom()); assertEquals("{test@example.com}", from); }
From source file:cz.filmtit.userspace.Emailer.java
/** * Sends an email with parameters that has been collected before in the fields of the class. * @return Sign if the email has been successfully sent */// w ww . j a va 2 s. co m public boolean send() { if (hasCollectedData()) { // send mail; javax.mail.Session session; logger.info("Emailer", "Create session for mail login " + (String) configuration.getProperty("mail.filmtit.address") + " password" + (String) configuration.getProperty("mail.filmtit.password")); session = javax.mail.Session.getDefaultInstance(configuration, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication((String) configuration.getProperty("mail.filmtit.address"), (String) configuration.getProperty("mail.filmtit.password")); } }); session.setDebug(true); javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session); try { message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.email)); message.setFrom(new InternetAddress((String) configuration.getProperty("mail.filmtit.address"))); message.setSubject(this.subject); message.setText(this.message); Transport transportSSL = session.getTransport(); transportSSL.connect((String) configuration.getProperty("mail.smtps.host"), Integer.parseInt(configuration.getProperty("mail.smtps.port")), (String) configuration.getProperty("mail.filmtit.address"), (String) configuration.getProperty("mail.filmtit.password")); // account used transportSSL.sendMessage(message, message.getAllRecipients()); transportSSL.close(); } catch (MessagingException ex) { logger.error("Emailer", "An error while sending an email. " + ex); } return true; } logger.warning("Emailer", "Emailer has not collected all data to be able to send an email."); return false; }
From source file:ru.org.linux.user.LostPasswordController.java
private void sendEmail(User user, String email, Timestamp resetDate) throws MessagingException { Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress("no-reply@linux.org.ru")); String resetCode = UserService.getResetCode(configuration.getSecret(), user.getNick(), email, resetDate); msg.addRecipient(RecipientType.TO, new InternetAddress(email)); msg.setSubject("Your password @linux.org.ru"); msg.setSentDate(new Date()); msg.setText("?!\n\n" + "? ?? ? ?? http://www.linux.org.ru/reset-password\n\n" + " " + user.getNick() + ", ?: " + resetCode + "\n\n" + "!"); Transport.send(msg);//from w ww . ja v a2 s .c o m }
From source file:dk.netarkivet.common.utils.EMailUtils.java
/** * Send an email, possibly forgiving errors. * * @param to The recipient of the email. Separate multiple recipients with * commas. Supports only adresses of the type 'john@doe.dk', not * 'John Doe <john@doe.dk>' * @param from The sender of the email.// w w w .j a v a2 s . c o m * @param subject The subject of the email. * @param body The body of the email. * @param forgive On true, will send the email even on invalid email * addresses, if at least one recipient can be set, on false, will * throw exceptions on any invalid email address. * * * @throws ArgumentNotValid If either parameter is null, if to, from or * subject is the empty string, or no recipient * can be set. If "forgive" is false, also on * any invalid to or from address. * @throws IOFailure If the message cannot be sent for some reason. */ public static void sendEmail(String to, String from, String subject, String body, boolean forgive) { ArgumentNotValid.checkNotNullOrEmpty(to, "String to"); ArgumentNotValid.checkNotNullOrEmpty(from, "String from"); ArgumentNotValid.checkNotNullOrEmpty(subject, "String subject"); ArgumentNotValid.checkNotNull(body, "String body"); Properties props = new Properties(); props.put(MAIL_FROM_PROPERTY_KEY, from); props.put(MAIL_HOST_PROPERTY_KEY, Settings.get(CommonSettings.MAIL_SERVER)); Session session = Session.getDefaultInstance(props); Message msg = new MimeMessage(session); // to might contain more than one e-mail address for (String toAddressS : to.split(",")) { try { InternetAddress toAddress = new InternetAddress(toAddressS.trim()); msg.addRecipient(Message.RecipientType.TO, toAddress); } catch (AddressException e) { if (forgive) { log.warn("To address '" + toAddressS + "' is not a valid email " + "address", e); } else { throw new ArgumentNotValid("To address '" + toAddressS + "' is not a valid email " + "address", e); } } catch (MessagingException e) { if (forgive) { log.warn("To address '" + toAddressS + "' could not be set in email", e); } else { throw new ArgumentNotValid("To address '" + toAddressS + "' could not be set in email", e); } } } try { if (msg.getAllRecipients().length == 0) { throw new ArgumentNotValid("No valid recipients in '" + to + "'"); } } catch (MessagingException e) { throw new ArgumentNotValid("Message invalid after setting" + " recipients", e); } try { InternetAddress fromAddress = null; fromAddress = new InternetAddress(from); msg.setFrom(fromAddress); } catch (AddressException e) { throw new ArgumentNotValid("From address '" + from + "' is not a valid email " + "address", e); } catch (MessagingException e) { if (forgive) { log.warn("From address '" + from + "' could not be set in email", e); } else { throw new ArgumentNotValid("From address '" + from + "' could not be set in email", e); } } try { msg.setSubject(subject); msg.setContent(body, MIMETYPE); msg.setSentDate(new Date()); Transport.send(msg); } catch (MessagingException e) { throw new IOFailure("Could not send email with subject '" + subject + "' from '" + from + "' to '" + to + "'. Body:\n" + body, e); } }