PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label jakarta-mail. Show all posts
Showing posts with label jakarta-mail. Show all posts

Wednesday, November 9, 2022

[FIXED] How to Sign Javamail with DKIM

 November 09, 2022     dkim, email, jakarta-mail, james, java     No comments   

Issue

Is there a library or a way to do this without an external library? I am using apache james as my mail server and currently send email like this:

public void sendMessage(String to, String subject, String content) {
    MimeMessage message = new MimeMessage(session);
    try {
        message.addRecipients(Message.RecipientType.TO, to);
        message.setFrom(new InternetAddress(from));
        message.setSubject(subject);
        message.setContent(content, "text/html; charset=utf-8");
        Transport.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }       
}

But i'd like to sign the email with DKIM before hand. I understand I need to implement DKIM signing into the james server and plan on use jDKIM to do this, I also understand I need to create the keys using something like www.port25.com, but how do I actually sign the email in java before I send it out?


Solution

I ended up using DKIM for Javamail which can be downloaded at: DKIM For Javamail

Here is an example (Its pretty well documented in the examples in the download):

public void sendMessage(String to, String subject, String content) {
    //Create DKIM Signer
    DKIMSigner dkimSigner = null;
    try {
        dkimSigner = new DKIMSigner(properties.getProperty("mail.smtp.dkim.signingdomain"), properties.getProperty("mail.smtp.dkim.selector"), properties.getProperty("mail.smtp.dkim.privatekey"));
        dkimSigner.setIdentity(properties.getProperty("mail.user") + "@" + properties.getProperty("mail.smtp.dkim.signingdomain"));
        dkimSigner.setHeaderCanonicalization(Canonicalization.SIMPLE);
        dkimSigner.setBodyCanonicalization(Canonicalization.RELAXED);
        dkimSigner.setLengthParam(true);
        dkimSigner.setSigningAlgorithm(SigningAlgorithm.SHA1withRSA);
        dkimSigner.setZParam(true);
    } catch (Exception e) {
    e.printStackTrace();
        }
    if(dkimSigner != null) {
        //Create message
        Message message = new SMTPDKIMMessage(session, dkimSigner);
        try {
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            message.setFrom(new InternetAddress(from));
            message.setSubject(subject);
            message.setContent(content, "text/html; charset=utf-8");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }   
    }           
}


Answered By - ryandlf
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, July 12, 2022

[FIXED] How to skip emails in copy operation for INBOX folder when emails already exist in java ?

 July 12, 2022     imap, jakarta-mail, java, message     No comments   

Issue

I want to copy message from source mail server to destination mail server. i used imap protocol and javamail. For first stage, i copy message and there is not any problem. Again i copy message the already message dose not overwrite or skip copy. I want to skip message in inbox folder in the destination folder. I do not like delete source message. Thanks.


Solution

The most efficient way is to keep track of the UIDs of the messages from the source folder that you've already copied, so you don't copy them again. Depending on how you're doing the copying, you may only need to keep track of the last UID you copied, and then only copy messages with larger UIDs (newer messages). See the UIDFolder interface.



Answered By - Bill Shannon
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, July 11, 2022

[FIXED] How to remove CC and BCC from Recipients address : JavaMail

 July 11, 2022     jakarta-mail, java, message     No comments   

Issue

I am able to add CC,BCC and TO successfully to message or Mimemessage, But my requirement is I need to send the constructed email to some third party for backup purpose. I Tried this:

message.setRecipient(Message.RecipientType.CC, null);
message.setRecipient(Message.RecipientType.CC, "");

Please help!!!


Solution

Use setRecipients instead of setRecipient, with a null address.



Answered By - Bill Shannon
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing