public void close() throws MessagingException { this.t.close(); }
t.sendMessage(msg, msg.getAllRecipients()); System.out.println("Response: " + t.getLastServerResponse()); t.close();
@Override public void run() { try { smtpTrans.connect(); smtpTrans.sendMessage(msg, msg.getAllRecipients()); smtpTrans.close(); } catch (SendFailedException e) { s_logger.error(" Failed to send email alert " + e); } catch (MessagingException e) { s_logger.error(" Failed to send email alert " + e); } } });
smtpTrans.close();
smtpTrans.close(); } else { throw new CloudRuntimeException("Unable to send email invitation; smtp ses");
close(); } catch (MessagingException ex2) {
close(); } catch (MessagingException ex2) {
public void close() throws MessagingException { this.t.close(); }
smtpTrans.close();
public void sendEmail(){ Properties props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); props.put("mail.smtp.sasl.enable", "false"); Session session = Session.getInstance(props); final URLName unusedUrlName = null; SMTPTransport transport = new SMTPTransport(session, unusedUrlName); final String emptyPassword = null; transport.connect("smtp.gmail.com", 587, "username@gmail.com", emptyPassword); // you can get the access token when you do Auth2 for gmail, I haven't tried this with just a password byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", "username@gmail.com", accessToken).getBytes(); response = BASE64EncoderStream.encode(response); transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235); Message message = new MimeMessage(session); //add needed stuff to the message before sending it. transport.sendMessage(message, message.getAllRecipients()); transport.close();
SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); t.connect("smtp.gmail.com", senderMail, pass); t.sendMessage(message, message.getAllRecipients()); t.close();
/** * @param conf * @throws MessagingException */ public void init(Map<String, Object> conf) throws MessagingException { logger = Logger.getLogger(MailService.class); session = createSession(conf); from = conf.get(MAIL_SMTP_FROM).toString(); boolean useSsl = false; if(conf.containsKey(MAIL_SMTP_STARTTLS_ENABLE)) { useSsl = Boolean.parseBoolean(conf.get(MAIL_SMTP_STARTTLS_ENABLE).toString()); } String transportString = useSsl ? "smtps" : "smtp"; try { SMTPTransport transport = (SMTPTransport) session.getTransport(transportString); if(!transport.isConnected()) { transport.connect(); } transport.close(); } catch (NoSuchProviderException e) { String message = "Can't initialise Mail Service. Can't make transport for " + transportString; logger.fatal(message, e); throw new MessagingException(message); } }
transport.close();
Properties props = System.getProperties(); props.put("mail.smtps.host","smtp.mailgun.org"); props.put("mail.smtps.auth","true"); Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("abc@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com", false)); msg.setSubject(subject); msg.setText(body); msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse("abc@gmail.com")); msg.setContent(body, "text/html"); SMTPTransport t = (SMTPTransport)session.getTransport("smtps"); t.connect("smtp.mailgun.com", "postmaster@sandbox***********.mailgun.org", "0ae971*********"); t.sendMessage(msg, msg.getAllRecipients()); System.out.println("Response: " + t.getLastServerResponse()); t.close();
String token = GoogleAuthUtil.getToken(context, userEmail, "oauth2:https://mail.google.com/"); SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, userEmail, token, true); SMTPMessage smtpMessage = new SMTPMessage(smtpSession); smtpMessage.setFrom(new InternetAddress(userEmail)); smtpMessage.addRecipient(Message.RecipientType.TO, toAddress); smtpMessage.setSubject(subject); smtpMessage.setContent(multipart, multipart.getContentType()); smtpMessage.saveChanges(); smtpTransport.sendMessage(smtpMessage, smtpMessage.getAllRecipients()); smtpTransport.close();
public void sendMail(String recipient, String message) throws MessagingException { if ("".equals(from)) { throw new MessagingException(); } Session session = Session.getDefaultInstance(props, authenticator); session.setDebug(false); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); msg.addHeader("name", fromActor); msg.setSubject(subject); InternetAddress recipientAddress = new InternetAddress(recipient); msg.setRecipient(Message.RecipientType.TO, recipientAddress); String htmlMessage = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s", message); MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8"); mimeMultipart.addBodyPart(mimeBodyPart); msg.setContent(mimeMultipart); // sending the message SMTPTransport transport = (SMTPTransport) session.getTransport(this.protocol); transport.connect(); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); }
private void closeTransport(Mail mail, HostAddress outgoingMailServer, SMTPTransport transport) { if (transport != null) { try { // James-899: transport.close() sends QUIT to the server; if that fails // (e.g. because the server has already closed the connection) the message // should be considered to be delivered because the error happened outside // of the mail transaction (MAIL, RCPT, DATA). transport.close(); } catch (MessagingException e) { LOGGER.error("Warning: could not close the SMTP transport after sending mail ({}) to {} at {} for {}; " + "probably the server has already closed the connection. Message is considered to be delivered. Exception: {}", mail.getName(), outgoingMailServer.getHostName(), outgoingMailServer.getHost(), mail.getRecipients(), e.getMessage()); } transport = null; } }