javax.mail.internet.MailDateFormat Java Examples

The following examples show how to use javax.mail.internet.MailDateFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MimeMailExample.java    From DKIM-for-JavaMail with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {
	
	// read test configuration from test.properties in your classpath
	Properties testProps = TestUtil.readProperties();
	
	// generate string buffered test mail
	StringBuffer mimeMail = new StringBuffer();
	mimeMail.append("Date: ").append(new MailDateFormat().format(new Date())).append("\r\n");
	mimeMail.append("From: ").append(testProps.getProperty("mail.smtp.from")).append("\r\n");
	if (testProps.getProperty("mail.smtp.to") != null) {
		mimeMail.append("To: ").append(testProps.getProperty("mail.smtp.to")).append("\r\n");
	}
	if (testProps.getProperty("mail.smtp.cc") != null) {
		mimeMail.append("Cc: ").append(testProps.getProperty("mail.smtp.cc")).append("\r\n");
	}
	mimeMail.append("Subject: ").append("DKIM for JavaMail: MimeMailExample Testmessage").append("\r\n");
	mimeMail.append("\r\n");
	mimeMail.append(TestUtil.bodyText);

	// get a JavaMail Session object 
	Session session = Session.getDefaultInstance(testProps, null);

	
	
	///////// beginning of DKIM FOR JAVAMAIL stuff
	
	// get DKIMSigner object
	DKIMSigner dkimSigner = new DKIMSigner(
			testProps.getProperty("mail.smtp.dkim.signingdomain"),
			testProps.getProperty("mail.smtp.dkim.selector"),
			testProps.getProperty("mail.smtp.dkim.privatekey"));

	/* set an address or user-id of the user on behalf this message was signed;
	 * this identity is up to you, except the domain part must be the signing domain
	 * or a subdomain of the signing domain.
	 */ 
	dkimSigner.setIdentity("mimemailexample@"+testProps.getProperty("mail.smtp.dkim.signingdomain"));

	// construct the JavaMail message using the DKIM message type from DKIM for JavaMail
	Message msg = new SMTPDKIMMessage(session, new ByteArrayInputStream(mimeMail.toString().getBytes()), dkimSigner);

	///////// end of DKIM FOR JAVAMAIL stuff

	// send the message by JavaMail
	Transport transport = session.getTransport("smtp");
	transport.connect(testProps.getProperty("mail.smtp.host"),
			testProps.getProperty("mail.smtp.auth.user"),
			testProps.getProperty("mail.smtp.auth.password"));
	transport.sendMessage(msg, msg.getAllRecipients());
	transport.close();
}
 
Example #2
Source File: UpdateThread.java    From ImapNote2 with GNU General Public License v3.0 4 votes vote down vote up
public void WriteMailToNew(OneNote note, String usesticky, String noteBody) throws MessagingException, IOException {
  String body = null;

  // Here we add the new note to the "new" folder
  //Log.d(TAG,"Add new note");
  Properties props = new Properties();
  Session session = Session.getDefaultInstance(props, null);
  MimeMessage message = new MimeMessage(session);
  if (usesticky.equals("true")) {
    body = "BEGIN:STICKYNOTE\nCOLOR:" + this.color + "\nTEXT:" + noteBody +
           "\nPOSITION:0 0 0 0\nEND:STICKYNOTE";
    message.setText(body);
    message.setHeader("Content-Transfer-Encoding", "8bit");
    message.setHeader("Content-Type","text/x-stickynote; charset=\"utf-8\"");
  } else {
    message.setHeader("X-Uniform-Type-Identifier","com.apple.mail-note");
    UUID uuid = UUID.randomUUID();
    message.setHeader("X-Universally-Unique-Identifier", uuid.toString());
    body = noteBody;
    body = body.replaceFirst("<p dir=ltr>", "<div>");
    body = body.replaceFirst("<p dir=\"ltr\">", "<div>");
    body = body.replaceAll("<p dir=ltr>", "<div><br></div><div>");
    body = body.replaceAll("<p dir=\"ltr\">", "<div><br></div><div>");
    body = body.replaceAll("</p>", "</div>");
    body = body.replaceAll("<br>\n", "</div><div>");
    message.setText(body, "utf-8", "html");
    message.setFlag(Flags.Flag.SEEN,true);
  }
  message.setSubject(note.GetTitle());
  MailDateFormat mailDateFormat = new MailDateFormat();
  // Remove (CET) or (GMT+1) part as asked in github issue #13
  String headerDate = (mailDateFormat.format(new Date())).replaceAll("\\(.*$", "");
  message.addHeader("Date", headerDate);
  //déterminer l'uid temporaire
  String uid = Integer.toString(Math.abs(Integer.parseInt(note.GetUid())));
  File directory = new File ((ImapNotes2.getAppContext()).getFilesDir() + "/" +
          Listactivity.imapNotes2Account.GetAccountname() + "/new");
  //message.setFrom(new InternetAddress("ImapNotes2", Listactivity.imapNotes2Account.GetAccountname()));
  message.setFrom(Listactivity.imapNotes2Account.GetAccountname());
  File outfile = new File (directory, uid);
  OutputStream str = new FileOutputStream(outfile);
  message.writeTo(str);

}