I copy-pasted your code on CS50 IDE and used the debugger. The first 512-bytes block is not a JPEG so I go to line 51 (after the else) and test outfile==NULL. You can see that the pointer to the outfile is NULL so it makes sense that your program is entering the if statement and printing "No Output File 2".
You are using the correct functions but ...
Look at the following code:
if (outfile == NULL)
printf("%s\n", "No Output File 2");
fwrite(buffer, 512, 1, outfile);
Now, remember what the pset spec said. The data at the beginning of the input file is garbage and needs to be discarded. That ...