List of usage examples for javax.mail Message setFrom
public abstract void setFrom(Address address) throws MessagingException;
From source file:mail.MailService.java
/** * Erstellt eine MIME-Mail.// w w w .jav a 2 s . co m * @param email * @throws MessagingException * @throws IOException */ public String createMail1(Mail email, Config config) throws MessagingException, IOException { Properties props = new Properties(); props.put("mail.smtp.host", "mail.java-tutor.com"); Session session = Session.getDefaultInstance(props); Message msg = new MimeMessage(session); // msg.setHeader("MIME-Version" , "1.0"); // msg.setHeader("Content-Type" , "text/plain"); // Absender InternetAddress addressFrom = new InternetAddress(email.getAbsender()); msg.setFrom(addressFrom); // Empfnger InternetAddress addressTo = new InternetAddress(email.getEmpfaenger()); msg.setRecipient(Message.RecipientType.TO, addressTo); msg.setSubject(email.getBetreff()); msg.setSentDate(email.getAbsendeDatum()); String txt = Utils.toString(email.getText()); msg.setText(txt); msg.saveChanges(); // Mail in Ausgabestrom schreiben ByteArrayOutputStream bOut = new ByteArrayOutputStream(); try { msg.writeTo(bOut); } catch (IOException e) { if (config.isTest()) System.out.println("Fehler beim Schreiben der Mail in Schritt 1"); throw e; } return removeMessageId(bOut.toString(Charset.defaultCharset().name())); }
From source file:org.collectionspace.chain.csp.persistence.file.TestGeneral.java
/** * Sets up and sends email message providing you have set up the email address to send to *///ww w . ja va 2s .co m @Test public void testEmail() { Boolean doIreallyWantToSpam = false; // set to true when you have configured the email addresses /* please personalises these emails before sending - I don't want your spam. */ String from = "admin@collectionspace.org"; String[] recipients = { "" }; String SMTP_HOST_NAME = "localhost"; String SMTP_PORT = "25"; String message = "Hi, Test Message Contents"; String subject = "A test from collectionspace test suite"; String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); //REM - Replace. This is pre-JDK 1.4 code, from the days when JSSE was a separate download. Fix the imports so they refer to the classes in javax.net.ssl If you really want to get hold of a specific instance, you can use Security.getProvider(name). You'll find the appropriate names in the providers documentation. boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); //props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth", "false"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props); session.setDebug(debug); if (doIreallyWantToSpam) { Message msg = new MimeMessage(session); InternetAddress addressFrom; try { addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); 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 and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); if (doIreallyWantToSpam) { Transport.send(msg); assertTrue(doIreallyWantToSpam); } } catch (AddressException e) { log.debug(e.getMessage()); assertTrue(false); } catch (MessagingException e) { log.debug(e.getMessage()); assertTrue(false); } } //assertTrue(doIreallyWantToSpam); }
From source file:org.apache.jmeter.reporters.MailerModel.java
/** * Sends a mail with the given parameters using SMTP. * * @param from// w ww .ja v a 2 s. co m * the sender of the mail as shown in the mail-client. * @param vEmails * all receivers of the mail. The receivers are seperated by * commas. * @param subject * the subject of the mail. * @param attText * the message-body. * @param smtpHost * the smtp-server used to send the mail. * @param smtpPort the smtp-server port used to send the mail. * @param user the login used to authenticate * @param password the password used to authenticate * @param mailAuthType {@link MailAuthType} Security policy * @param debug Flag whether debug messages for the mail session should be generated * @throws AddressException If mail address is wrong * @throws MessagingException If building MimeMessage fails */ public void sendMail(String from, List<String> vEmails, String subject, String attText, String smtpHost, String smtpPort, final String user, final String password, MailAuthType mailAuthType, boolean debug) throws AddressException, MessagingException { InternetAddress[] address = new InternetAddress[vEmails.size()]; for (int k = 0; k < vEmails.size(); k++) { address[k] = new InternetAddress(vEmails.get(k)); } // create some properties and get the default Session Properties props = new Properties(); props.put(MAIL_SMTP_HOST, smtpHost); props.put(MAIL_SMTP_PORT, smtpPort); // property values are strings Authenticator authenticator = null; if (mailAuthType != MailAuthType.NONE) { props.put(MAIL_SMTP_AUTH, "true"); switch (mailAuthType) { case SSL: props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); break; case TLS: props.put(MAIL_SMTP_STARTTLS, "true"); break; default: break; } } if (!StringUtils.isEmpty(user)) { authenticator = new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }; } Session session = Session.getInstance(props, authenticator); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); msg.setText(attText); Transport.send(msg); }
From source file:org.infoglue.cms.util.mail.MailService.java
/** * *//* www.j a va 2 s. c om*/ private Message createMessage(String from, String to, String bcc, String subject, String content) throws SystemException { try { final Message message = new MimeMessage(this.session); message.setContent(content, "text/html"); message.setFrom(createInternetAddress(from)); message.setRecipient(Message.RecipientType.TO, createInternetAddress(to)); if (bcc != null) message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc)); message.setSubject(subject); message.setText(content); message.setDataHandler(new DataHandler(new StringDataSource(content, "text/html"))); return message; } catch (MessagingException e) { throw new Bug("Unable to create the message.", e); } }
From source file:com.photon.phresco.util.Utility.java
public static void sendNewPassword(String[] toAddr, String fromAddr, String user, String subject, String body, String username, String password, String host, String screen, String build) throws PhrescoException { List<String> lists = Arrays.asList(toAddr); if (fromAddr == null) { fromAddr = "phresco.do.not.reply@gmail.com"; username = "phresco.do.not.reply@gmail.com"; password = "phresco123"; }//from w w w .j a v a 2s . c om Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getDefaultInstance(props, new LoginAuthenticator(username, password)); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddr)); List<Address> emailsList = getEmailsList(lists); Address[] dsf = new Address[emailsList.size()]; message.setRecipients(Message.RecipientType.BCC, emailsList.toArray(dsf)); message.setSubject(subject); message.setContent(body, "text/html"); Transport.send(message); } catch (MessagingException e) { throw new PhrescoException(e); } }
From source file:mail.MailService.java
/** * Erstellt eine kanonisierte MIME-Mail. * @param email//ww w . j a va 2 s . c o m * @throws MessagingException * @throws IOException */ public String createMail2(Mail email, Config config) throws MessagingException, IOException { byte[] mailAsBytes = email.getText(); int laenge = mailAsBytes.length + getCRLF().length; byte[] bOout = new byte[laenge]; for (int i = 0; i < mailAsBytes.length; i++) { bOout[i] = mailAsBytes[i]; } byte[] neu = getCRLF(); int counter = 0; for (int i = mailAsBytes.length; i < laenge; i++) { bOout[i] = neu[counter]; counter++; } email.setText(bOout); Properties props = new Properties(); props.put("mail.smtp.host", "mail.java-tutor.com"); Session session = Session.getDefaultInstance(props); Message msg = new MimeMessage(session); // msg.setHeader("MIME-Version" , "1.0"); // msg.setHeader("Content-Type" , "text/plain"); // Absender InternetAddress addressFrom = new InternetAddress(email.getAbsender()); msg.setFrom(addressFrom); // Empfnger InternetAddress addressTo = new InternetAddress(email.getEmpfaenger()); msg.setRecipient(Message.RecipientType.TO, addressTo); msg.setSubject(email.getBetreff()); msg.setSentDate(email.getAbsendeDatum()); msg.setText(Utils.toString(email.getText())); msg.saveChanges(); /* // Erstellen des Content MimeMultipart content = new MimeMultipart("text"); MimeBodyPart part = new MimeBodyPart(); part.setText(email.getText()); part.setHeader("MIME-Version" , "1.0"); part.setHeader("Content-Type" , part.getContentType()); content.addBodyPart(part); System.out.println(content.getContentType()); Message msg = new MimeMessage(session); msg.setContent(content); msg.setHeader("MIME-Version" , "1.0"); msg.setHeader("Content-Type" , content.getContentType()); InternetAddress addressFrom = new InternetAddress(email.getAbsender()); msg.setFrom(addressFrom); InternetAddress addressTo = new InternetAddress(email.getEmpfnger()); msg.setRecipient(Message.RecipientType.TO, addressTo); msg.setSubject(email.getBetreff()); msg.setSentDate(email.getAbsendeDatum()); */ //msg.setContent(email.getText(), "text/plain"); // Mail in Ausgabestrom schreiben ByteArrayOutputStream bOut = new ByteArrayOutputStream(); try { msg.writeTo(bOut); } catch (IOException e) { System.out.println("Fehler beim Schreiben der Mail in Schritt 2"); throw e; } // String out = bOut.toString(); // int pos1 = out.indexOf("Message-ID"); // int pos2 = out.indexOf("@localhost") + 13; // String output = out.subSequence(0, pos1).toString(); // output += (out.substring(pos2)); return removeMessageId(bOut.toString(Charset.defaultCharset().name())); }
From source file:com.photon.phresco.util.Utility.java
public static void sendTemplateEmail(String[] toAddr, String fromAddr, String user, String subject, String body, String username, String password, String host, String screen, String build) throws PhrescoException { List<String> lists = Arrays.asList(toAddr); if (fromAddr == null) { fromAddr = "phresco.do.not.reply@gmail.com"; username = "phresco.do.not.reply@gmail.com"; password = "phresco123"; }/* w w w. j a v a2 s. c o m*/ Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getDefaultInstance(props, new LoginAuthenticator(username, password)); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddr)); List<Address> emailsList = getEmailsList(lists); Address[] dsf = new Address[emailsList.size()]; message.setRecipients(Message.RecipientType.BCC, emailsList.toArray(dsf)); message.setSubject(subject); DataSource source = new FileDataSource(body); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName("Error.txt"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); MimeBodyPart messageBodyPart2 = new MimeBodyPart(); DataSource source2 = new FileDataSource(screen); messageBodyPart2.setDataHandler(new DataHandler(source2)); messageBodyPart2.setFileName("Error.jpg"); multipart.addBodyPart(messageBodyPart2); MimeBodyPart mainPart = new MimeBodyPart(); String content = "<b>Phresco framework error report<br><br>User Name: " + user + "<br> Email Address: " + fromAddr + "<br>Build No: " + build; mainPart.setContent(content, "text/html"); multipart.addBodyPart(mainPart); message.setContent(multipart); Transport.send(message); } catch (MessagingException e) { throw new PhrescoException(e); } }
From source file:com.spartasystems.holdmail.util.TestMailClient.java
public void sendResourceEmail(String resourceName, String sender, String recipient, String subject) { try {/* www . ja v a2s . c om*/ InputStream resource = TestMailClient.class.getClassLoader().getResourceAsStream(resourceName); if (resource == null) { throw new MessagingException("Couldn't find resource at: " + resourceName); } Message message = new MimeMessage(session, resource); // wipe out ALL exisitng recipients message.setRecipients(Message.RecipientType.TO, new Address[] {}); message.setRecipients(Message.RecipientType.CC, new Address[] {}); message.setRecipients(Message.RecipientType.BCC, new Address[] {}); // then set the new data message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setFrom(InternetAddress.parse(sender)[0]); message.setSubject(subject); Transport.send(message); logger.info("Outgoing mail forwarded to " + recipient); } catch (MessagingException e) { throw new HoldMailException("couldn't send mail: " + e.getMessage(), e); } }
From source file:esg.node.components.notification.ESGNotifier.java
private boolean sendNotification(String userid, String userAddress, String messageText) { Message msg = null; boolean success = false; String myAddress = null;/*from w w w . jav a2s .c o m*/ try { msg = new MimeMessage(session); msg.setHeader("X-Mailer", "ESG DataNode IshMailer"); msg.setSentDate(new Date()); myAddress = props.getProperty("mail.admin.address", "esg-admin@llnl.gov"); msg.setFrom(new InternetAddress(myAddress)); msg.setSubject(subject + "ESG File Update Notification"); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(userAddress)); //msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(userAddress)); msg.setText(messageText); Transport.send(msg); success = true; } catch (MessagingException ex) { log.error("Problem Sending Email Notification: to " + userid + ": " + userAddress + " (" + subject + ")\n" + messageText + "\n", ex); } msg = null; //gc niceness return success; }
From source file:checkwebsite.Mainframe.java
/** * * @param email//from w w w . j av a 2 s . co m * @param msg */ protected void notify(String email, String msg) { // Recipient's email ID needs to be mentioned. String to = email;//change accordingly // go to link below and turn on Access for less secure apps //https://www.google.com/settings/security/lesssecureapps // Sender's email ID needs to be mentioned String from = "";//Enter your email id here final String username = "";//Enter your email id here final String password = "";//Enter your password here // 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); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Report of your website : " + wurl); // // Now set the actual message // message.setText("Hello, " + msg + " this is sample for to check send " // + "email using JavaMailAPI "); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("Please! find your website report in attachment"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "WebsiteReport.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); lblWarning.setText("report sent..."); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } }