List of usage examples for javax.mail.internet MimeMessage setFrom
public void setFrom(String address) throws MessagingException
From source file:com.mycompany.login.mb.EmailBean.java
public void envia() throws AddressException, MessagingException { Session session = Session.getInstance(this.propriedades, this.authentication); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("anderson.freitas@lifemed.com.br")); message.setRecipients(Message.RecipientType.TO, "anderson.freitas@lifemed.com.br"); message.setSentDate(new Date()); message.setSubject("Teste envio jsf"); message.setContent("Sua solicitao foi aprovada: OS n" + this.os, "text/plain"); Transport.send(message);/*from w w w.ja v a 2 s. c o m*/ }
From source file:edu.ku.brc.helpers.EMailHelper.java
/** * Send an email. Also sends it as a gmail if applicable, and does password checking. * @param host host of SMTP server/*from www. j av a 2 s. c o m*/ * @param uName username of email account * @param pWord password of email account * @param fromEMailAddr the email address of who the email is coming from typically this is the same as the user's email * @param toEMailAddr the email addr of who this is going to * @param subject the Textual subject line of the email * @param bodyText the body text of the email (plain text???) * @param fileAttachment and optional file to be attached to the email * @return true if the msg was sent, false if not */ public static boolean sendMsgAsGMail(final String host, final String uName, final String pWord, final String fromEMailAddr, final String toEMailAddr, final String subject, final String bodyText, final String mimeType, @SuppressWarnings("unused") final String port, @SuppressWarnings("unused") final String security, final File fileAttachment) { String userName = uName; String password = pWord; Boolean fail = false; ArrayList<String> userAndPass = new ArrayList<String>(); Properties props = System.getProperties(); props.put("mail.smtp.host", host); //$NON-NLS-1$ props.put("mail.smtp.auth", "true"); //$NON-NLS-1$ //$NON-NLS-2$ props.put("mail.smtp.port", "587"); //$NON-NLS-1$ //$NON-NLS-2$ props.put("mail.smtp.starttls.enable", "true"); //$NON-NLS-1$ //$NON-NLS-2$ boolean usingSSL = false; if (usingSSL) { props.put("mail.smtps.port", "587"); //$NON-NLS-1$ //$NON-NLS-2$ props.put("mail.smtp.starttls.enable", "true"); //$NON-NLS-1$ //$NON-NLS-2$ } Session session = Session.getInstance(props, null); session.setDebug(instance.isDebugging); if (instance.isDebugging) { log.debug("Host: " + host); //$NON-NLS-1$ log.debug("UserName: " + userName); //$NON-NLS-1$ log.debug("Password: " + password); //$NON-NLS-1$ log.debug("From: " + fromEMailAddr); //$NON-NLS-1$ log.debug("To: " + toEMailAddr); //$NON-NLS-1$ log.debug("Subject: " + subject); //$NON-NLS-1$ } try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromEMailAddr)); if (toEMailAddr.indexOf(",") > -1) //$NON-NLS-1$ { StringTokenizer st = new StringTokenizer(toEMailAddr, ","); //$NON-NLS-1$ InternetAddress[] address = new InternetAddress[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { String toStr = st.nextToken().trim(); address[i++] = new InternetAddress(toStr); } msg.setRecipients(Message.RecipientType.TO, address); } else { InternetAddress[] address = { new InternetAddress(toEMailAddr) }; msg.setRecipients(Message.RecipientType.TO, address); } msg.setSubject(subject); //msg.setContent( aBodyText , "text/html;charset=\"iso-8859-1\""); // create the second message part if (fileAttachment != null) { // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setContent(bodyText, mimeType);//"text/html;charset=\"iso-8859-1\""); //mbp1.setContent(bodyText, "text/html;charset=\"iso-8859-1\""); MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(fileAttachment); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); } else { // add the Multipart to the message msg.setContent(bodyText, mimeType); } // set the Date: header msg.setSentDate(new Date()); // send the message int cnt = 0; do { cnt++; SMTPTransport t = (SMTPTransport) session.getTransport("smtp"); //$NON-NLS-1$ try { t.connect(host, userName, password); t.sendMessage(msg, msg.getAllRecipients()); fail = false; } catch (MessagingException mex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, mex); instance.lastErrorMsg = mex.toString(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); instance.lastErrorMsg = instance.lastErrorMsg + ", " + ex.toString(); //$NON-NLS-1$ } //wrong username or password, get new one if (mex.toString().equals("javax.mail.AuthenticationFailedException")) //$NON-NLS-1$ { fail = true; userAndPass = askForUserAndPassword((Frame) UIRegistry.getTopWindow()); if (userAndPass == null) {//the user is done return false; } // else //try again userName = userAndPass.get(0); password = userAndPass.get(1); } } finally { log.debug("Response: " + t.getLastServerResponse()); //$NON-NLS-1$ t.close(); } } while (fail && cnt < 6); } catch (MessagingException mex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, mex); instance.lastErrorMsg = mex.toString(); //mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); instance.lastErrorMsg = instance.lastErrorMsg + ", " + ex.toString(); //$NON-NLS-1$ } return false; } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, ex); ex.printStackTrace(); } if (fail) { return false; } //else return true; }
From source file:cl.preguntame.controller.PlataformaController.java
@ResponseBody @RequestMapping(value = "/email", method = RequestMethod.POST) public String correo(HttpServletRequest req) { try {// w w w. j a v a 2s. com String host = "smtp.gmail.com"; Properties prop = System.getProperties(); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", host); prop.put("mail.smtp.user", "hector.riquelme1169@gmail.com"); prop.put("mail.smtp.password", "rriiqquueellmmee"); prop.put("mail.smtp.port", 587); prop.put("mail.smtp.auth", "true"); Session sesion = Session.getDefaultInstance(prop, null); MimeMessage mensaje = new MimeMessage(sesion); mensaje.setFrom(new InternetAddress()); mensaje.setRecipient(Message.RecipientType.TO, new InternetAddress("hector.riquelme1169@gmail.com")); mensaje.setSubject("CONTACTO MIS CONCEPTOS"); mensaje.setText(req.getParameter("mensaje_contacto")); Transport transport = sesion.getTransport("smtp"); transport.connect(host, "hector.riquelme1169@gmail.com", "rriiqquueellmmee"); transport.sendMessage(mensaje, mensaje.getAllRecipients()); transport.close(); } catch (Exception e) { } return req.getParameter("mensaje_contacto") + " - " + req.getParameter("email_contacto"); }
From source file:jease.cms.service.Mails.java
/** * Sends an email synchronously.// ww w. j a v a2 s . co m */ public void send(String sender, String recipients, String subject, String text) throws MessagingException { if (properties != null) { Session session = Session.getInstance(properties.asProperties(), new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getUser(), properties.getPassword()); } }); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sender)); message.setReplyTo(new InternetAddress[] { new InternetAddress(sender) }); message.setRecipients(Message.RecipientType.TO, recipients); message.setSubject(subject, "utf-8"); message.setSentDate(new Date()); message.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); message.setHeader("Content-Transfer-Encoding", "quoted-printable"); message.setText(text, "utf-8"); Transport.send(message); } }
From source file:com.wso2telco.workflow.notification.EmailService.java
public void sendEmail(final String emailAddress, final String subject, final String content) { new Thread() { @Override/*from w w w.ja v a 2 s. co m*/ public void run() { Map<String, String> workflowProperties = WorkflowProperties.loadWorkflowPropertiesFromXML(); String emailHost = workflowProperties.get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_HOST); String fromEmailAddress = workflowProperties .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_ADDRESS); String fromEmailPassword = workflowProperties .get(Constants.KEY_WORKFLOW_EMAIL_NOTIFICATION_FROM_PASSWORD); Properties props = System.getProperties(); props.put("mail.smtp.host", emailHost); props.put("mail.smtp.user", fromEmailAddress); props.put("mail.smtp.password", fromEmailPassword); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); try { Session session = Session.getDefaultInstance(props, null); InternetAddress toAddress = new InternetAddress(emailAddress); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmailAddress)); message.addRecipient(Message.RecipientType.TO, toAddress); message.setSubject(subject); message.setContent(content, "text/html; charset=UTF-8"); Transport transport = session.getTransport("smtp"); transport.connect(emailHost, fromEmailAddress, fromEmailPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { log.error("Email sending failed. ", e); } } }.start(); }
From source file:com.consol.citrus.demo.devoxx.service.MailService.java
/** * Send mail via SMTP connection.//from w ww . j av a 2s. c o m * @param to * @param subject * @param body */ public void sendMail(String to, String subject, String body) { Properties props = new Properties(); props.put("mail.smtp.host", mailServerHost); props.put("mail.smtp.port", mailServerPort); props.put("mail.smtp.auth", true); Authenticator authenticator = new Authenticator() { private PasswordAuthentication pa = new PasswordAuthentication(username, password); @Override public PasswordAuthentication getPasswordAuthentication() { return pa; } }; Session session = Session.getInstance(props, authenticator); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; message.setRecipients(Message.RecipientType.TO, address); message.setSubject(subject); message.setSentDate(new Date()); message.setText(body); Transport.send(message); } catch (MessagingException e) { log.error("Failed to send mail!", e); } }
From source file:com.otz.plugins.transport.aws.ses.SpringEmailSender.java
public void send(String templateName, Locale locale, String to, Map<String, String> parameters) throws MessagingException { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(emailTemplateRepository.getFrom(templateName, locale))); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(emailTemplateRepository.getSubject(templateName, locale)); msg.setContent(emailTemplateRepository.getContent(templateName, locale, parameters), emailTemplateRepository.getContentType(templateName, locale)); try {//from w w w.ja va2s . co m // Create a transport. transport = session.getTransport(); transport.connect(emailConfigParameters.getHost(), emailConfigParameters.getAccessKey(), emailConfigParameters.getSecretKey()); transport.sendMessage(msg, msg.getAllRecipients()); } catch (Exception e) { // TODO deal with failure to send // send message to hipchat e.printStackTrace(); } }
From source file:ru.codemine.ccms.mail.EmailService.java
public void sendSimpleMessage(String address, String subject, String content) { try {//ww w .j a v a 2 s . c om Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", "true"); props.put("mail.mime.charset", "UTF-8"); props.put("mail.smtp.ssl.enable", ssl); Session session = Session.getInstance(props, new EmailAuthenticator(username, password)); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(username, "")); message.setRecipient(Message.RecipientType.TO, new InternetAddress(address)); message.setSubject(subject); message.setText(content, "utf-8", "html"); Transport transport = session.getTransport("smtp"); transport.connect(); transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException | UnsupportedEncodingException ex) { log.error( "? ? email, : " + ex.getLocalizedMessage()); ex.printStackTrace(); } }
From source file:edu.cornell.mannlib.vitro.webapp.utils.MailUtil.java
public void sendMessage(String messageText, String subject, String from, String to, List<String> deliverToArray) throws IOException { Session s = FreemarkerEmailFactory.getEmailSession(req); try {/*from w ww .ja v a 2 s .co m*/ int recipientCount = (deliverToArray == null) ? 0 : deliverToArray.size(); if (recipientCount == 0) { log.error("To establish the Contact Us mail capability the system administrators must " + "specify at least one email address in the current portal."); } MimeMessage msg = new MimeMessage(s); // Set the from address msg.setFrom(new InternetAddress(from)); // Set the recipient address if (recipientCount > 0) { InternetAddress[] address = new InternetAddress[recipientCount]; for (int i = 0; i < recipientCount; i++) { address[i] = new InternetAddress(deliverToArray.get(i)); } msg.setRecipients(Message.RecipientType.TO, address); } // Set the subject and text msg.setSubject(subject); // add the multipart to the message msg.setContent(messageText, "text/html"); // set the Date: header msg.setSentDate(new Date()); Transport.send(msg); // try to send the message via smtp - catch error exceptions } catch (Exception ex) { log.error("Exception sending message :" + ex.getMessage(), ex); } }
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 w w .j a va 2 s. c om*/ }