示例#1
0
  /**
   * main de prueba
   *
   * @param args Se ignoran.
   */
  public static void main(String[] args) {
    try {
      // Propiedades de la conexión
      Properties props = new Properties();
      props.setProperty("mail.smtp.host", "smtp.gmail.com");
      props.setProperty("mail.smtp.starttls.disable", "true");
      props.setProperty("mail.smtp.auth.plain.disable", "true");
      props.setProperty("mail.smtp.port", "465");
      props.setProperty("mail.smtp.user", "*****@*****.**");
      props.setProperty("mail.smtp.auth", "true");

      // Preparamos la sesion
      Session session = Session.getDefaultInstance(props);
      session.setDebug(true);
      // Construimos el mensaje
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress("*****@*****.**"));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress("*****@*****.**"));
      message.setSubject("Hola");
      message.setText("Mensajito con Java Mail" + "de los buenos." + "poque si");

      // Lo enviamos.
      Transport t = session.getTransport("smtp");
      t.connect("*****@*****.**", "admmphinv2012");
      t.sendMessage(message, message.getAllRecipients());

      // Cierre.
      t.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#2
0
  public static void sendSmtpMessage(Session session, Message message, Address[] recipients)
      throws MessagingException {

    // message = cloneMimeMessage(message);
    message.setSentDate(new Date());
    setHeaderFieldValue(message, HEADER_X_MAILER, X_MAILER);
    message.saveChanges();

    LOG.info(
        "Sending message '"
            + message.getSubject()
            + "' from '"
            + Str.format(message.getFrom())
            + "' to '"
            + Str.format(recipients)
            + "'.");

    Transport trans = session.getTransport("smtp");
    Properties properties = session.getProperties();
    trans.connect(
        properties.getProperty("mail.smtp.host"),
        properties.getProperty("mail.smtp.auth.user"),
        properties.getProperty("mail.smtp.auth.password"));
    trans.sendMessage(message, recipients);
    trans.close();
  }
示例#3
0
  // send emails
  public void sendEmail(String to, String mess) {

    try {
      String host = "smtp.gmail.com";
      String username = "******";
      String password = "******";
      String email = "*****@*****.**";

      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.user", username);
      props.put("mail.smtp.password", password);
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");

      Session s = Session.getDefaultInstance(props, null);
      MimeMessage m = new MimeMessage(s);
      m.setFrom(new InternetAddress(email));

      m.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      m.setSubject("Secret Santa");
      m.setText(mess);

      Transport t = s.getTransport("smtp");
      t.connect(host, 465, username, password);
      t.sendMessage(m, m.getRecipients(Message.RecipientType.TO));
      t.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  @Override
  public void run() {
    LOGGER.info("***********Inside send e-Mail Thread************");
    LOGGER.info("Sending email to:: E-mail Address::" + emailModel.getToaddess());
    Session mailSession = createSmtpSession();
    mailSession.setDebug(true);
    List<String> toAddresses = new ArrayList<String>();

    try {
      Transport transport = mailSession.getTransport();
      MimeMessage message = new MimeMessage(mailSession);

      message.setSubject(emailModel.getSubject());
      message.setFrom(new InternetAddress(emailModel.getFromAddress()));
      message.setContent(emailModel.getContent(), "text/html");

      toAddresses.add(emailModel.getToaddess());
      transport.connect();
      Iterator<String> itr = toAddresses.iterator();
      while (itr.hasNext()) {
        String toAddress = (String) itr.next();
        message.addRecipients(Message.RecipientType.TO, toAddress);
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
      }
      LOGGER.info("Successfully sent email to:: E-mail Address::" + emailModel.getToaddess());
    } catch (MessagingException e) {
      LOGGER.error("Cannot Send email", e);
    }
  }
示例#5
0
  public static void SendMail(String to, String subject, String Content) {
    try {

      // setup the mail server properties
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");

      // set up the message
      Session session = Session.getInstance(props);

      Message message = new MimeMessage(session);

      // add a TO address
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

      // add a multiple CC addresses
      // message.setRecipients(Message.RecipientType.CC,
      // InternetAddress.parse("[email protected],[email protected]"));

      message.setSubject(subject);
      message.setContent(Content, "text/plain");

      Transport transport = session.getTransport("smtp");
      transport.connect("smtp.gmail.com", 587, "aaa", "pass");
      transport.sendMessage(message, message.getAllRecipients());
      // System.out.println("Send email via gmail...");
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
示例#6
0
  public void send(String mailto, String subject, String textMessage, String contentType)
      throws FileNotFoundException, MessagingException {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    Properties props = new Properties();
    props.put("mail.smtp.user", smtpUsername);
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", smtpPort);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.port", smtpPort);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl", "true");

    Authenticator auth = new SMTPAuthenticator();
    Session smtpSession = Session.getInstance(props, auth);
    smtpSession.setDebug(true);

    Message message = new MimeMessage(smtpSession);
    InternetAddress[] address = {new InternetAddress(mailto)};
    message.setRecipients(Message.RecipientType.TO, address);
    message.setSubject(subject);
    message.setSentDate(new Date());
    message.setContent(textMessage, contentType);

    Transport tr = smtpSession.getTransport("smtp");
    tr.connect(smtpHost, smtpUsername, smtpPassword);
    tr.sendMessage(message, message.getAllRecipients());
    tr.close();
  }
示例#7
0
  /** 发�?邮件 */
  public boolean sendOut() {
    try {
      mimeMsg.setContent(mp);
      mimeMsg.saveChanges();
      System.out.println("正在发�?邮件....");

      Session mailSession = Session.getInstance(props, null);
      Transport transport = mailSession.getTransport("smtp");
      transport.connect((String) props.get("mail.smtp.host"), username, password);
      transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
      //          如果抄�?人为null  不添加抄送人
      if (mimeMsg.getRecipients(Message.RecipientType.CC) != null)
        transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
      // transport.send(mimeMsg);

      System.out.println("发送邮件成功");
      transport.close();

      return true;
    } catch (Exception e) {
      System.err.println("邮件发送失败" + e);
      e.printStackTrace();
      return false;
    }
  }
示例#8
0
  public static void sendEmail() throws Exception {
    String msgText = "测试邮件";
    // 获取系统属性
    Properties p = System.getProperties();
    // 设置邮件服务器:看看当地是否有邮局
    p.put("mail.smtp.host", "smtp.126.com");
    p.put("mail.smtp.auth", "true");

    // 创建一个邮件会话,准备发送邮件:买信封
    Session s = Session.getDefaultInstance(p, null);
    // 准备一个发送邮件的对象,即邮差
    Transport trans = s.getTransport("smtp");
    // 准备一封邮件:买信纸
    Message m = new MimeMessage(s);
    // 设置邮件的标题,即主题
    m.setSubject("问卷调查用户信息:同学");
    // 设置日期
    m.setSentDate(new Date());
    // 邮件正文
    m.setContent(msgText, "text/html;  charset=gb2312"); // 用于发邮件 设置邮件的标题,即主题
    // 设置发件人:写自己的地址和名字
    Address from = new InternetAddress("*****@*****.**");
    m.setFrom(from);
    // 设置收件人:写对方的地址和名字
    Address to = new InternetAddress("*****@*****.**");
    // 将收件人加到邮件中:将收件人写到信纸上
    m.addRecipient(Message.RecipientType.TO, to);
    // 连接服务器认证
    trans.connect("smtp.126.com", "*****@*****.**", "owenjunefirst");
    // 发送邮件,即投递
    trans.sendMessage(m, m.getAllRecipients());
    System.out.println(msgText + "\n");
  }
示例#9
0
  @Override
  public void send(MimeMessage message) throws MailerException {
    Session session = Session.getDefaultInstance(getConfiguration());

    if (this.from != null) {
      try {
        message.setFrom(from);
      } catch (MessagingException e) {
        throw new MailerException(e);
      }
    }

    try {
      Transport tr = session.getTransport("smtp");
      if (getConfigurationProperty(Configuration.SMTP.AUTH, "true").equalsIgnoreCase("true")) {
        tr.connect(
            getConfigurationProperty(Configuration.SMTP.USER),
            getConfigurationProperty(Configuration.SMTP.PASSWORD));
      } else {
        tr.connect();
      }
      tr.sendMessage(message, message.getAllRecipients());
      tr.close();
    } catch (MessagingException e) {
      throw new MailerException(e);
    }
  }
示例#10
0
  public void test() throws Exception {

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtps.quitwait", "false");
    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(true);

    Transport transport = mailSession.getTransport();
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(username.getText()));
    message.setSubject(subject.getText());

    String s = msgfield.getText();
    message.setContent(s, "text/plain");
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailID.getText()));
    System.out.println("8i m here ");
    try {
      transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, username.getText(), password.getText());
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, "invalid username or password");
    }
    System.out.println("8i m here also yaar");
    // transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
    transport.sendMessage(message123, message123.getAllRecipients());
    transport.close();
    System.out.println(s);
  }
示例#11
0
 private SessionAndTransport(String key, Properties props, Authenticator auth)
     throws NoSuchProviderException {
   this.key = key;
   this.session = createSession(key, props, auth);
   this.transport = session.getTransport("smtp");
   touch();
 }
示例#12
0
  public static void sendEmail(
      InternetAddress from, InternetAddress to, String content, String requester)
      throws MessagingException, UnsupportedEncodingException {

    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(false);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("MN Traffic Generation is used by " + requester);
    message.setContent(content, "text/plain");

    message.addRecipient(Message.RecipientType.TO, to);

    InternetAddress[] replyto = new InternetAddress[1];
    replyto[0] = from;

    message.setFrom(from);
    message.setReplyTo(replyto);
    message.setSender(from);

    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

    transport.close();
  }
示例#13
0
  public void enviarEmail() {
    FacesContext context = FacesContext.getCurrentInstance();
    AutenticaUsuario autenticaUsuario =
        new AutenticaUsuario(GmailBean.CONTA_GMAIL, GmailBean.SENHA_GMAIL);
    Session session = Session.getDefaultInstance(this.configuracaoEmail(), autenticaUsuario);
    session.setDebug(true);

    try {

      Transport envio = null;
      MimeMessage email = new MimeMessage(session);
      email.setRecipient(Message.RecipientType.TO, new InternetAddress(this.para));
      email.setFrom(new InternetAddress(this.de));
      email.setSubject(this.assunto);
      email.setContent(this.mensagem, "text/plain");
      email.setSentDate(new Date());
      envio = session.getTransport("smtp");
      envio.connect(GmailBean.SERVIDOR_SMTP, GmailBean.CONTA_GMAIL, GmailBean.SENHA_GMAIL);
      email.saveChanges();
      envio.sendMessage(email, email.getAllRecipients());
      envio.close();

      context.addMessage(null, new FacesMessage("E-mail enviado com sucesso"));

    } catch (AddressException e) {
      FacesMessage msg =
          new FacesMessage("Erro ao montar mensagem de e-mail! Erro: " + e.getMessage());
      context.addMessage(null, msg);
      return;
    } catch (MessagingException e) {
      FacesMessage msg = new FacesMessage("Erro ao enviar e-mail! Erro: " + e.getMessage());
      context.addMessage(null, msg);
      return;
    }
  }
示例#14
0
  // Send the specified message.
  public void sendMessage(int type, Message message) throws Exception {
    // Display message dialog to get message values.
    MessageDialog dialog;
    dialog = new MessageDialog(this, null, type, message);
    if (!dialog.display()) {
      // Return if dialog was cancelled.
      return;
    }

    // Create a new message with values from dialog.
    Message newMessage = new MimeMessage(session);
    newMessage.setFrom(new InternetAddress(dialog.getFrom()));

    newMessage.setSubject(dialog.getSubject());
    newMessage.setSentDate(new Date());
    newMessage.setText(dialog.getContent());

    final Address[] recipientAddresses = InternetAddress.parse(dialog.getTo());
    newMessage.setRecipients(Message.RecipientType.TO, recipientAddresses);

    Transport transport = session.getTransport("smtps");
    transport.connect(getSmtpServer(), GMAIL_SMTP_PORT, getUsername(), getPassword());
    transport.sendMessage(newMessage, recipientAddresses);
    transport.close();
  }
示例#15
0
  private static void sendMimeMessage(
      String from,
      String to,
      String subject,
      String content,
      List<String> filenames,
      Session session) {

    try {
      String[] toArr = {to};
      DataSource[] attachments = (filenames == null) ? null : new DataSource[filenames.size()];

      if (filenames != null) {
        int i = 0;
        for (String filename : filenames) {
          attachments[i++] = new FileDataSource(filename);
        }
      }

      MimeMessage message = createMimeMessage(session, from, toArr, subject, content, attachments);
      message.saveChanges();

      Transport transport = session.getTransport();
      try {
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
      } finally {
        transport.close();
      }
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
  public static void sendEmail(String title, String message, String email)
      throws MessagingException {

    Properties props = new Properties();
    props.setProperty("mail.smtp.auth", "true"); // 设置访问smtp服务器需要认证
    props.setProperty("mail.transport.protocol", "smtp"); // 设置访问服务器的协议

    Session session = Session.getDefaultInstance(props);
    session.setDebug(true); // 打开debug功能

    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(user)); // 设置发件人,163邮箱要求发件人与登录用户必须一致(必填),其它邮箱不了解
    msg.setText(message); // 设置邮件内容
    msg.setSubject("Test"); // 设置邮件主题

    Transport trans = session.getTransport();
    try {
      trans.connect("smtp.163.com", 25, user, pwd); // 连接邮箱smtp服务器,25为默认端口
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      trans.sendMessage(msg, new Address[] {new InternetAddress(email)}); // 发送邮件
    } catch (Exception e) {
      e.printStackTrace();
    }

    trans.close(); // 关闭连接
  }
示例#17
0
  public boolean send() {
    try {
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "smtp.gmail.com");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port", "465");
      props.put("mail.debug", "true");
      props.put("mail.smtp.socketFactory.port", "465");
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
      Session session = Session.getDefaultInstance(props, new GJMailAuthenticator());
      session.setDebug(true);
      Transport transport = session.getTransport();
      InternetAddress addressFrom = new InternetAddress("*****@*****.**");
      MimeMessage message = new MimeMessage(session);
      message.setSender(addressFrom);
      message.setSubject(subject);
      message.setContent(text, "text/html");
      InternetAddress addressTo = new InternetAddress(to);
      message.setRecipient(Message.RecipientType.TO, addressTo);
      transport.connect();
      Transport.send(message);
      transport.close();
      System.out.println("DONE");

    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }

    return true;
  }
示例#18
0
  public void SendEmail(String email, String contestToken) {
    String to = email;
    String from = "Tyumen_ACM_Society";
    String host = "smtp.gmail.com";
    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", host);
    props.put("mail.stmp.user", "*****@*****.**");
    props.put("mail.smtp.password", "475508th");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "pop3");
    props.put("mail.transport.protocol", "smtp");

    Session session = Session.getDefaultInstance(props, new SmtpAuthenticator());

    try {
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Registration on acm.utmn.ru");
      message.setText(
          "Hello! Thank you for registration on acm.utmn.ru! Use next token to submit tasks: "
              + contestToken);
      Transport t = session.getTransport("smtp");
      t.send(message);
      System.out.println("Message was successfully sent.");
    } catch (MessagingException mex) {
      mex.printStackTrace();
    }
  }
 public void envioCorreo(String mensaje, String modo) {
   String subject = "";
   String destination = obtenerParametros("destinatario");
   String mailHost = obtenerParametros("mailHost");
   String source = obtenerParametros("origen");
   if (modo.equals("Fallo")) subject = "Aplicaci\363n de Carga y Validaci\363n de datos. ERROR!!";
   else subject = "Aplicacion de Carga y Validaci\363n de datos. MENSAJE INFORMATIVO";
   Properties properties = new Properties();
   properties.put("mail.smtp.host", mailHost);
   properties.put("mail.from", source);
   Session session = Session.getInstance(properties, null);
   try {
     Message message = new MimeMessage(session);
     InternetAddress address[] = {new InternetAddress(destination)};
     message.setRecipients(javax.mail.Message.RecipientType.TO, address);
     message.setFrom(new InternetAddress(source));
     message.setSubject(subject);
     message.setContent(mensaje + "\n", "text/plain");
     Transport transport = session.getTransport(address[0]);
     transport.connect();
     transport.sendMessage(message, address);
   } catch (Exception e1) {
     e1.printStackTrace();
   }
 }
示例#20
0
 @Override
 public void sendEmail(String id, String total, String email) {
   Properties prop = new Properties();
   prop.setProperty("mail.transport.protocol", "smtp");
   Message message = null;
   Transport transport = null;
   Session session = null;
   try {
     session = Session.getDefaultInstance(prop);
     session.setDebug(true);
     // 创建一封新有邮件
     message = (Message) new MimeMessage(session);
     // 标题、正文内容、发件人地址
     message.setSubject("订单支付成功邮件(系统邮件)");
     message.setContent("订单编号为:" + id + ",金额 为: " + total + ",已经支付成功!", "text/html;charset=utf-8");
     message.setFrom(new InternetAddress("*****@*****.**"));
     // 设置用户名密码,收件人地址,发送邮件
     transport = session.getTransport();
     // 通过用户名与密码, 链接邮件服务器
     transport.connect("smtp.sina.com", "soft03_test", "soft03_test");
     transport.sendMessage(message, new Address[] {new InternetAddress(email)});
   } catch (Exception e) {
     throw new RuntimeException(e);
   } finally {
     try {
       // 关闭客户端(释放资源)
       transport.close();
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
 }
示例#21
0
  public static void main(String[] args) throws MessagingException, IOException {
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(Paths.get("mail", "mail.properties"))) {
      props.load(in);
    }
    List<String> lines = Files.readAllLines(Paths.get(args[0]), Charset.forName("UTF-8"));

    String from = lines.get(0);
    String to = lines.get(1);
    String subject = lines.get(2);

    StringBuilder builder = new StringBuilder();
    for (int i = 3; i < lines.size(); i++) {
      builder.append(lines.get(i));
      builder.append("\n");
    }

    Console console = System.console();
    String password = new String(console.readPassword("Password: "));

    Session mailSession = Session.getDefaultInstance(props);
    // mailSession.setDebug(true);
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(builder.toString());
    Transport tr = mailSession.getTransport();
    try {
      tr.connect(null, password);
      tr.sendMessage(message, message.getAllRecipients());
    } finally {
      tr.close();
    }
  }
示例#22
0
  public static void send(
      String nomContact,
      Integer numFacture,
      Double montantCommande,
      Integer nbLots,
      String typeEnvoi,
      String adresseContact)
      throws Exception {

    Properties props = System.getProperties();
    props.put("mail.smtps.host", "smtp.orange.fr");
    props.put("mail.smtps.auth", "true");

    Session session = Session.getInstance(props, null);
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress("*****@*****.**"));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adresseContact, false));

    String corpsMessage =
        "Bonjour "
            + nomContact
            + "\n\n"
            + "votre commande n°"
            + numFacture
            + " d'un montant de "
            + montantCommande
            + "€ TTC vient d'être traitée.\n\n"
            + typeEnvoi
            + " \n\n"
            + "L'équipe du ciné cap vert vous remercie de votre confiance.\n\n\n\n"
            + " P.S.: Retrouvez en pièce jointe le mode d'emploi des chèques cinéma sur notre interface de réservation en ligne";

    msg.setSubject("Votre commande de " + nbLots + " lots de chèques cinéma vient d'être traitée.");
    msg.setHeader("X-Mailer", "Test En-tete");
    msg.setSentDate(new Date());

    // create and fill the first message part
    MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setText(corpsMessage);

    // create the second message part
    MimeBodyPart mbp2 = new MimeBodyPart();

    // attach the file to the message
    mbp2.attachFile("ccvad.png");

    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);
    mp.addBodyPart(mbp2);

    // add the Multipart to the message
    msg.setContent(mp);

    SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
    t.connect("smtp.orange.fr", "*****@*****.**", "popcorn21800");
    t.sendMessage(msg, msg.getAllRecipients());
    System.out.println("Réponse: " + t.getLastServerResponse());
    t.close();
  }
示例#23
0
  public void sendMail(String From, String To, String subject, String body) {

    Properties props = new Properties();

    props.put("mail.smtp.user", "*****@*****.**");
    props.put("mail.smtp.host", "smtp.gmail.com");

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", " javax.net.ssl.SSLSocketFactory");

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session =
        Session.getDefaultInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("*****@*****.**", "password");
              }
            });
    //
    // session.setDebug(true);

    try {

      Message message = new MimeMessage(session);

      message.setFrom(new InternetAddress(From));

      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
      message.setSubject(subject);
      message.setText(body);

      Transport transport = session.getTransport("smtps");
      transport.connect(
          "smtp.gmail.com", Integer.valueOf(465), "*****@*****.**", "password;");
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();

      // System.out.println("Done");

      // } catch (MessagingException e) {
    } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
  }
示例#24
0
 private static void sendSMTPMessage(
     Session session, String mailHost, String user, String password, Message msg)
     throws MessagingException {
   SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
   t.connect(mailHost, 25, user, password);
   t.sendMessage(msg, msg.getAllRecipients());
   t.close();
 }
示例#25
0
 public final Transport getTransport(final Session session) throws MessagingException {
   final Transport transport = session.getTransport("smtp"); // i18n lib
   transport.connect(
       cursorSMTP.getHost(),
       cursorSMTP.getPort(),
       cursorSMTP.getUser(),
       properties.getProperty(App.Settings.PASSWORD)); // i18n
   return transport;
 }
示例#26
0
 public void sendEmail(Session session, MimeMessage message) throws MessagingException {
   Transport transport = session.getTransport("smtp");
   try {
     transport.connect();
     transport.sendMessage(message, message.getAllRecipients());
   } finally {
     transport.close();
   }
 }
示例#27
0
  public void send() {

    Properties props = new Properties();

    props.setProperty("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    Session mailSession = Session.getDefaultInstance(props);
    // mailSession.setDebug(true);
    Transport transport;
    try {
      transport = mailSession.getTransport();

      MimeMessage message = new MimeMessage(mailSession);
      message.setFrom(new InternetAddress(userName));
      message.setSubject(subject);

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

      if (!cc.equals("")) {
        message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
      }
      if (!bcc.equals("")) {
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
      }

      // create the message part
      MimeBodyPart messageBodyPart = new MimeBodyPart();

      // fill message
      messageBodyPart.setText(content);

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);
      if (fList != null) {
        Iterator<File> i = fList.iterator();
        // part two is attachment
        while (i.hasNext()) {
          File file = (File) i.next();
          messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(file);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(file.getName());
          multipart.addBodyPart(messageBodyPart);
        }
      }
      // Put parts in message
      message.setContent(multipart);

      Transport.send(message);

      transport.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#28
0
 public static final Transport getMailTransport(StaticConfig staticConfig)
     throws MessagingException {
   Session mailSession = Session.getDefaultInstance(getMailInfo(staticConfig));
   Transport transport = mailSession.getTransport("smtp");
   transport.connect(
       staticConfig.getSMTPHost(),
       staticConfig.getSMTPUser(),
       staticConfig.getSMTPPasswordParam());
   return transport;
 }
  /**
   * @param MostrarInforme Abre una ventana con el informe del email
   * @param SERVIDOR
   * @param PUERTO
   */
  public boolean sendMailHTML(boolean MostrarInforme, String SERVIDOR, String PUERTO) {
    if (MostrarInforme) {
      Informe.setVisible(true);
      Informe.setAlwaysOnTop(true);
    }
    try {
      Informe.setTitulo("Destino: " + Destinatarios.get(0));
      Properties props = new Properties();
      props.put("mail.smtp.host", SERVIDOR); // "wo06.wiroos.com" o "smtp.gmail.com"
      props.setProperty("mail.smtp.starttls.enable", "true");
      //          props.setProperty("mail.smtp.port", "587");
      props.setProperty("mail.smtp.port", PUERTO); // "587");
      props.setProperty("mail.smtp.user", usuarioCorreo);
      props.setProperty("mail.smtp.auth", "true");

      Session session = Session.getDefaultInstance(props, null);
      BodyPart texto = new MimeBodyPart();
      texto.setText(mensaje);

      BodyPart adjunto = new MimeBodyPart();
      if (!rutaArchivo.equals("")) {
        adjunto.setDataHandler(new DataHandler(new FileDataSource(rutaArchivo)));
        adjunto.setFileName(nombreArchivo);
      }

      MimeMultipart multiParte = new MimeMultipart();
      multiParte.addBodyPart(texto);
      if (!rutaArchivo.equals("")) {
        multiParte.addBodyPart(adjunto);
      }

      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(usuarioCorreo));

      // Destinatarios varios
      for (int i = 0; i < Destinatarios.size(); i++) {
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(Destinatarios.get(i)));
        message.setSubject(asunto);
      }
      message.setContent(mensaje, "text/html");
      Informe.SetConectando();
      Transport t = session.getTransport("smtp");
      t.connect(usuarioCorreo, password);
      Informe.SetEnviando();
      t.sendMessage(message, message.getAllRecipients());
      t.close();
      Informe.SetEnviado(true);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      Informe.SetEnviado(false);
      return false;
    }
  }
示例#30
0
 public void sendEmailUsingJava(String to, String content, String subjectContext) {
   // int smtpPort = 465;
   // int smtpPort = 587;
   // String smtpHost = "smtp.gmail.com";
   // int smtpPort = 25;
   // String smtpHost = "exsmtp.wvu.edu";
   String subject = constants.getString(subjectContext);
   int smtpPort = Integer.parseInt(constants.getString("smtpPort"));
   String smtpHost = constants.getString("smtpHost");
   String urPassword = constants.getString("password");
   String from = constants.getString("emailaddress");
   // String content = contentText;
   // Create a mail session
   java.util.Properties props = new java.util.Properties();
   props = System.getProperties();
   props.put("mail.smtp.host", smtpHost);
   // props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtps.auth", "true");
   SmtpAuthenticator authentication = new SmtpAuthenticator();
   // Session session = Session.getInstance(props, null);
   Session session = Session.getInstance(props, authentication);
   session.setDebug(true);
   // Construct the message
   try {
     MimeMessage msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(from));
     msg.setRecipient(RecipientType.TO, new InternetAddress(to));
     msg.setSubject(subject);
     msg.setText(content);
     msg.setSentDate(new Date());
     Transport tr = null;
     tr = session.getTransport("smtp");
     tr.connect(smtpHost, smtpPort, from, urPassword);
     tr.sendMessage(msg, msg.getAllRecipients());
   } catch (AddressException e) {
     System.out.println(e.getClass().getName() + ": " + e.getMessage());
     Log.info(e.getClass().getName() + ": " + e.getMessage());
     StackTraceElement[] trace = e.getStackTrace();
     for (int i = 0; i < trace.length; i++) {
       System.out.println("\t" + trace[i].toString());
       Log.info("\n\t" + trace[i].toString());
     }
     e.printStackTrace();
   } catch (MessagingException e) {
     System.out.println(e.getClass().getName() + ": " + e.getMessage());
     Log.info(e.getClass().getName() + ": " + e.getMessage());
     StackTraceElement[] trace = e.getStackTrace();
     for (int i = 0; i < trace.length; i++) {
       System.out.println("\t" + trace[i].toString());
       Log.info("\n\t" + trace[i].toString());
     }
     e.printStackTrace();
   }
 }