Jeremiah was about 17 years of age when the Lord commissioned him to be a prophet: “I appointed you as a prophet to the nations” (Jeremiah 1:5). This commission was to last for 40 years. The purpose was for God to demonstrate his patience with a nation that had rejected him and turned to idolatry. God knew his warnings would be rejected, but nonetheless, it was Jeremiah’s job to deliver his message. Here is a partial quote from an article about it:
“God sent Jeremiah to give Judah the last warning before He cast them out of the land, decimating the nation and sending them into captivity in the pagan kingdom of Babylon. Jeremiah, a faithful, God-fearing man, was called to tell Judah that, because of their unrepentant sin, their God had turned against them and was now prepared to remove them from the land at the hands of a pagan king. Jeremiah is known as “the weeping prophet,” because he cried tears of sadness, not only because he knew what was about to happen but because, no matter how hard he tried, the people would not listen. Furthermore, he found no human comfort. God had forbidden him to marry or have children (Jeremiah 16:2), and his friends had turned their backs on him. So, along with the burden of the knowledge of impending judgment, he also must have felt very lonely. The people of Israel had become so hardened by the numbing effects of sin that they no longer believed God, nor did they fear Him. Jeremiah preached for 40 years, and not once did he see any real success in changing or softening the hearts and minds of his stubborn, idolatrous people.” https://www.gotquestions.org/life-Jeremiah.html
The situation when Jesus delivered his sermon on the mount and told his disciples not to waste their breath on those who mock, scoff and ridicule is entirely different. Christ’s followers have also been given a commission, to preach the gospel throughout the inhabited world. Jesus knew many would reject the message, so he simply instructed his followers to handle rejection by simply going elsewhere. There are plenty people out there who will appreciate the good news and turn to God.
“A dual reference to swine and dogs is also found in 2 Peter 2:22, “Of [false teachers] the proverbs are true: ‘A dog returns to its vomit,’ and, ‘A sow that is washed goes back to her wallowing in the mud.’” In His sermon, Jesus uses dogs and pigs as representative of those who would ridicule, reject, and blaspheme the gospel once it is presented to them. We are not to expose the gospel of Jesus Christ to those who have no other purpose than to trample it and return to their own evil ways. Repeatedly sharing the gospel with someone who continually scoffs and ridicules Christ is like casting pearls before swine. We can identify such people through discernment, which is given in some measure to all Christians (1 Corinthians 2:15–16). The command not to cast your pearls before swine does not mean we refrain from preaching the gospel. Jesus Himself ate with and taught sinners and tax collectors (Matthew 9:10). In essence, the instruction in Matthew 7:6 is the same that Jesus gave to His apostles when He said, “If anyone will not welcome you or listen to your words, shake the dust off your feet when you leave that home or town” (Matthew 10:14). We are to share the gospel, but, when it becomes apparent that the gospel is not welcome, we are to move on. We are responsible to share the good news; we are not responsible for people’s response to the good news. Pigs don’t appreciate pearls, and some people don’t appreciate what Christ has done for them. Our job is not to force conversions or cram the gospel down people’s throats; there’s no sense in preaching the value of pearls to swine. Jesus’ instruction to His apostles on how to handle rejection was to simply go elsewhere. There are other people who need to hear the gospel, and they are ready to hear it.” https://www.gotquestions.org/pearls-before-swine.html
If God tells you to prophesy a message of doom, gloom and destruction for 40 years to an unrepentant nation, then you do it. Otherwise, pay heed to the advice Jesus has given to those who try to share the good news of the kingdom with those who mock, scoff and ridicule. Shake the dust off your feet and move on.
Solomon’s Temple in Jerusalem was destroyed and the people taken into captivity in Babylon. The prophecy was fulfilled. Now we’ve moved on and what Jesus said applies to here and now. There is no contradiction because Jeremiah executed his assignment as prophet.