Example usage for javax.mail.internet MimeMessage setSentDate

List of usage examples for javax.mail.internet MimeMessage setSentDate

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage setSentDate.

Prototype

@Override
public void setSentDate(Date d) throws MessagingException 

Source Link

Document

Set the RFC 822 "Date" header field.

Usage

From source file:ru.org.linux.user.RegisterController.java

private static void sendEmail(Template tmpl, String nick, String email, boolean isNew)
        throws MessagingException {
    StringBuilder text = new StringBuilder();

    text.append("?!\n\n");
    if (isNew) {//w  ww.j  a v  a 2  s.c  o m
        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(tmpl.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:org.tizzit.util.mail.MailHelper.java

/**
 * Sends an email in text-format in iso-8859-1-encoding
 * //w w w  .j ava2  s .c o  m
 * @param subject the subject of the new mail
 * @param message the content of the mail
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendMail(String subject, String message, String from, String to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(message, "iso-8859-1");
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending mail: " + e.getLocalizedMessage());
    }
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Send an email in html-format in iso-8859-1-encoding
 * //from   w w w  .jav  a 2 s . co  m
 * @param subject the subject of the new mail
 * @param htmlMessage the content of the mail as html
 * @param alternativeTextMessage the content of the mail as text
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendHtmlMail(String subject, String htmlMessage, String alternativeTextMessage, String from,
        String to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());

        MimeMultipart multiPart = new MimeMultipart();
        BodyPart bodyPart = new MimeBodyPart();

        bodyPart.setText(alternativeTextMessage);
        multiPart.addBodyPart(bodyPart);

        bodyPart = new MimeBodyPart();
        bodyPart.setContent(htmlMessage, "text/html");
        multiPart.addBodyPart(bodyPart);

        multiPart.setSubType("alternative");

        msg.setContent(multiPart);
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending html-mail: " + e.getLocalizedMessage());
    }
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Send an email in html-format in iso-8859-1-encoding
 * //from  ww w.j  a  v  a2 s . com
 * @param subject the subject of the new mail
 * @param htmlMessage the content of the mail as html
 * @param alternativeTextMessage the content of the mail as text
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from,
        String to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        msg.addRecipients(Message.RecipientType.TO, to);
        msg.setSubject(subject, "ISO8859_1");
        msg.setSentDate(new Date());

        MimeMultipart multiPart = new MimeMultipart("alternative");
        MimeBodyPart htmlPart = new MimeBodyPart();
        MimeBodyPart textPart = new MimeBodyPart();

        textPart.setText(alternativeTextMessage, "ISO8859_1");
        textPart.setHeader("MIME-Version", "1.0");
        //textPart.setHeader("Content-Type", textPart.getContentType());
        textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\"");

        htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\"");
        htmlPart.setHeader("MIME-Version", "1.0");
        htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\"");
        //htmlPart.setHeader("Content-Type", htmlPart.getContentType());

        multiPart.addBodyPart(textPart);
        multiPart.addBodyPart(htmlPart);

        multiPart.setSubType("alternative");

        msg.setContent(multiPart);
        msg.setHeader("MIME-Version", "1.0");
        msg.setHeader("Content-Type", multiPart.getContentType());

        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending html-mail: " + e.getLocalizedMessage());
    }
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Sends an email in text-format in iso-8859-1-encoding
 * /*from w w w  .  ja v a  2  s.c  o  m*/
 * @param subject the subject of the new mail
 * @param message the content of the mail
 * @param from the sender-address
 * @param to the receiver-address(es)
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendMail(String subject, String message, String from, String[] to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        for (int i = to.length - 1; i >= 0; i--) {
            msg.addRecipients(Message.RecipientType.TO, to[i]);
        }
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(message, "iso-8859-1");
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending mail: " + e.getLocalizedMessage());
    }
}

From source file:jp.mamesoft.mailsocketchat.Mail.java

public static void Send(String to, int mode) {
    //mode 0 = ?, 1 = ?, 2 = ?, 3 = ??, 4 = , 5 = 
    System.out.println("???");
    String from = Mailsocketchat.mail_user;
    String host = "smtp.gmail.com";
    String port = "465";
    String text = "";
    String subject = "";
    if (mode == 0) {
        text = logprint(text);//from w w w  . ja v  a2s.c  om
        subject = " - MailSocketChat";
        if (Mailsocketchat.newver) {
            text = text + "\n\n??????????\n";
        }
    }
    if (mode == 1) {
        if (Mailsocketchat.subjectname) {
            subject = Mailsocketchat.logs.get(0).get("name");
        } else {
            subject = "?? - MailSocketChat";
        }
        text = logprint(text);
    }
    if (mode == 8) {
        text = logprint(text);
        subject = "?? - MailSocketChat";
    }
    if (mode == 2) {

        if (!Mailsocketchat.push) {
            subject = "????? - MailSocketChat";
            text = "???????????????????\n\n??????\n";
            text = logprint(text);
        } else {
            subject = "??? - MailSocketChat";
            text = "?????????????????\n(???????????)\n\n";
        }
        if (Mailsocketchat.newver) {
            text = text + "\n\n??????????\n";
        }
    }
    if (mode == 3) {

        if (!Mailsocketchat.push && !Mailsocketchat.repeat) {
            subject = "???? - MailSocketChat";
            text = "????\n\n??????\n";
            text = logprint(text);
        } else {
            subject = "?????? - MailSocketChat";
            text = "?????????????????\n\n";
            if (Mailsocketchat.repeat) {
                text = text + "??????\n";
                text = logprint(text);
            }
        }
        if (Mailsocketchat.newver) {
            text = text + "\n\n??????????\n";
        }
    }

    if (mode == 7) {

        if (!Mailsocketchat.repeat) {
            subject = "????? - MailSocketChat";
            text = "?????30????????????\n\n";
            if (!Mailsocketchat.push) {
                text = text + "??????\n";
                text = logprint(text);
            }
        } else {
            subject = "??? - MailSocketChat";
            text = "???\n\n";
        }
        if (Mailsocketchat.newver) {
            text = text + "\n\n??????????\n";
        }
    }

    if (mode == 4) {
        subject = " - MailSocketChat";

        int userint = Mailsocketchat.users.size();
        int romint = Mailsocketchat.roms.size();
        text = ": " + userint + " ROM: " + romint + "\n\n\n";

        for (Integer id : Mailsocketchat.users.keySet()) {
            HashMap<String, String> data = Mailsocketchat.users.get(id);
            text = text + data.get("name") + "\n";
            text = text + " (" + data.get("ip") + ")\n";
        }

        text = text + "\n\nROM\n";
        for (Integer id : Mailsocketchat.roms.keySet()) {
            HashMap<String, String> data = Mailsocketchat.roms.get(id);
            text = text + data.get("ip") + "\n";
        }
        if (Mailsocketchat.newver) {
            text = text + "\n\n??????????\n";
        }
    }
    if (mode == 5) {
        subject = " - MailSocketChat";
        if (Mailsocketchat.push) {
            text = "??: \n\n";
        } else if (Mailsocketchat.repeat) {
            text = "??: \n\n";
        } else {
            text = "??: ?\n\n";
        }
        text = text + "?\n";
        text = text + "?(fetch): ?????\n";
        text = text + "(push): ????\n";
        text = text + "(repeat): ????\n";
        text = text + "(list): ???\n";
        text = text + "#: ?????\n";
        text = text + "#hoge: ??hoge????\n";
        text = text + "(help): ?????\n\n";

        text = text + "\nMailSocketChat Ver." + Mailsocketchat.version + "\n";
        if (Mailsocketchat.newver) {
            text = text + "??????????\n";
        }
    }
    if (mode == 6) {
        subject = "????????? - MailSocketChat";
        text = text
                + "MailSocketChat??????????????????\n";
    }

    // create some properties and get the default Session
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    Session sendsession = Session.getInstance(props,
            new PlainAuthenticator(Mailsocketchat.mail_user, Mailsocketchat.mail_pass));

    try {
        // create a message
        MimeMessage msg = new MimeMessage(sendsession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] sendaddress = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, sendaddress);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        // If the desired charset is known, you can use
        // setText(text, charset)
        msg.setText(text);

        Transport.send(msg);
        System.out.println("?????");
    } catch (MessagingException mex) {
        System.out.println("??????");
    }
}

From source file:org.apache.lens.server.query.QueryEndNotifier.java

/** Send mail.
 *
 * @param host                      the host
 * @param port                      the port
 * @param email                     the email
 * @param mailSmtpTimeout           the mail smtp timeout
 * @param mailSmtpConnectionTimeout the mail smtp connection timeout */
public static void sendMail(String host, String port, Email email, int mailSmtpTimeout,
        int mailSmtpConnectionTimeout) throws Exception {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.timeout", mailSmtpTimeout);
    props.put("mail.smtp.connectiontimeout", mailSmtpConnectionTimeout);
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(email.getFrom()));
    for (String recipient : email.getTo().trim().split("\\s*,\\s*")) {
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
    }/*from  w ww.java 2 s .co  m*/
    if (email.getCc() != null && email.getCc().length() > 0) {
        for (String recipient : email.getCc().trim().split("\\s*,\\s*")) {
            message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(recipient));
        }
    }
    message.setSubject(email.getSubject());
    message.setSentDate(new Date());

    MimeBodyPart messagePart = new MimeBodyPart();
    messagePart.setText(email.getMessage());
    Multipart multipart = new MimeMultipart();

    multipart.addBodyPart(messagePart);
    message.setContent(multipart);
    Transport.send(message);
}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Send an email in html-format in iso-8859-1-encoding
 * /*ww w  .j  av  a 2  s  .  c o m*/
 * @param subject the subject of the new mail
 * @param htmlMessage the content of the mail as html
 * @param alternativeTextMessage the content of the mail as text
 * @param from the sender-address
 * @param to the receiver-address
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from,
        String[] to, String[] cc, String[] bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));

        if (cc != null && cc.length != 0) {
            for (int i = cc.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.CC, cc[i]);
            }
        }
        if (bcc != null && bcc.length != 0) {
            for (int i = bcc.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.BCC, bcc[i]);
            }
        }
        if (to != null && to.length != 0) {
            for (int i = to.length - 1; i >= 0; i--) {
                msg.addRecipients(Message.RecipientType.TO, to[i]);
            }
        }
        msg.setSubject(subject, "ISO8859_1");
        msg.setSentDate(new Date());

        MimeMultipart multiPart = new MimeMultipart("alternative");
        MimeBodyPart htmlPart = new MimeBodyPart();
        MimeBodyPart textPart = new MimeBodyPart();

        textPart.setText(alternativeTextMessage, "ISO8859_1");
        textPart.setHeader("MIME-Version", "1.0");
        //textPart.setHeader("Content-Type", textPart.getContentType());
        textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\"");

        htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\"");
        htmlPart.setHeader("MIME-Version", "1.0");
        htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\"");
        //htmlPart.setHeader("Content-Type", htmlPart.getContentType());

        multiPart.addBodyPart(textPart);
        multiPart.addBodyPart(htmlPart);

        multiPart.setSubType("alternative");

        msg.setContent(multiPart);
        msg.setHeader("MIME-Version", "1.0");
        msg.setHeader("Content-Type", multiPart.getContentType());

        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending html-mail: " + e.getLocalizedMessage());
    }
}

From source file:net.sourceforge.vulcan.mailer.MessageAssembler.java

public MimeMessage constructMessage(String subscribers, ConfigDto config, ProjectStatusDto status, String html)
        throws MessagingException, AddressException {

    final MimeMessage message = new MimeMessage(mailSession);

    message.setSentDate(new Date());
    message.setFrom(new InternetAddress(config.getSenderAddress()));

    if (isNotBlank(config.getReplyToAddress())) {
        message.setReplyTo(InternetAddress.parse(config.getReplyToAddress()));
    }/*from   w  ww.  j  a va 2  s  .  c  o m*/
    message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(subscribers));

    message.setSubject(status.getName() + ": " + status.getStatus());

    final Multipart multipart = new MimeMultipart();

    html = html.replaceAll("\\r", "");

    addMultipartBody(multipart, "text/html; charset=UTF-8", html);

    message.setContent(multipart);

    return message;
}

From source file:no.kantega.publishing.modules.mailsender.MailSender.java

/**
 * Sends a mail message. The content must be provided as MimeBodyPart objects.
 *
 * @param from      Sender's email address.
 * @param to        Recipient's email address.
 * @param cc        Email address for CC.
 * @param bcc       Email address for BCC.
 * @param subject   Subject text for the email.
 * @param bodyParts The body parts to insert into the message.
 * @throws SystemException        if an unexpected error occurs.
 * @throws ConfigurationException if a configuration error occurs.
 *///  www.  ja v a2s. com
public static void send(String from, String to, String cc, String bcc, String subject, MimeBodyPart[] bodyParts)
        throws ConfigurationException, SystemException {

    initEventLog();

    try {
        Properties props = new Properties();

        Configuration config = Aksess.getConfiguration();
        String host = config.getString("mail.host");
        if (host == null) {
            throw new ConfigurationException("mail.host");
        }

        // I noen tilfeller nsker vi at all epost skal g til en testadresse
        String catchAllTo = config.getString("mail.catchall.to");
        boolean catchallExists = catchAllTo != null && catchAllTo.contains("@");
        if (catchallExists) {
            StringBuilder prefix = new StringBuilder(" (original recipient: " + to);
            if (cc != null) {
                prefix.append(", cc: ").append(cc);
            }
            if (bcc != null) {
                prefix.append(", bcc: ").append(bcc);
            }
            prefix.append(") ");
            subject = prefix + subject;
            to = catchAllTo;
            cc = null;
            bcc = null;
        }

        props.setProperty("mail.smtp.host", host);

        Session session = Session.getDefaultInstance(props);

        boolean debug = config.getBoolean("mail.debug", false);
        if (debug) {
            session.setDebug(true);
        }

        // Opprett message, sett attributter
        MimeMessage message = new MimeMessage(session);
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress[] = InternetAddress.parse(to);

        message.setFrom(fromAddress);

        if (toAddress.length > 1) {
            message.setRecipients(Message.RecipientType.BCC, toAddress);
        } else {
            message.setRecipients(Message.RecipientType.TO, toAddress);
        }
        if (cc != null) {
            message.addRecipients(Message.RecipientType.CC, cc);
        }
        if (bcc != null) {
            message.addRecipients(Message.RecipientType.BCC, bcc);
        }

        message.setSubject(subject, "ISO-8859-1");
        message.setSentDate(new Date());

        Multipart mp = new MimeMultipart();
        for (MimeBodyPart bodyPart : bodyParts) {
            mp.addBodyPart(bodyPart);
        }
        message.setContent(mp);

        // Send meldingen
        Transport.send(message);

        eventLog.log("System", null, Event.EMAIL_SENT, to + ":" + subject, null);

        // Logg sending
        log.info("Sending email to " + to + " with subject " + subject);
    } catch (MessagingException e) {
        String errormessage = "Subject: " + subject + " | Error: " + e.getMessage();
        eventLog.log("System", null, Event.FAILED_EMAIL_SUBMISSION, errormessage + " | to: " + to, null);
        log.error("Error sending mail", e);
        throw new SystemException("Error sending email to : " + to + " with subject " + subject, e);
    }
}