OpenSmtp is a good email sending component on the .Net platform, but there are some bugs in it that affect our use. During use, I encountered garbled messages when the email subject length was large.
After checking the source code, it was found that there were problems with the original processing process: first, the theme was encoded through ASCII, and secondly, QP encoding was used, but the difference between theme and content was not considered.
When coding within themes, each row must be coded individually and the entire theme cannot be coded.
After comparing with Outlook Express, a method is added to the MailEncoding class to perform Base64 encoding specifically for the email subject.
public static string ConvertHeaderToBase64(string s, string charset)
{
int lineLength = 40; // Each line processes 40 bytes
Encoding encoding = Encoding.GetEncoding( charset ); // Get the specified encoding
byte[] buffer = encoding.GetBytes( s ); // Convert to bytecode
StringBuilder sb = new StringBuilder(); // Save the final result
string linebase64;
int block = buffer.Length%lineLength==0?buffer.Length/lineLength:buffer.Length/lineLength + 1;
for(int i=0; i< block; i++)
{
if( buffer.Length - i*lineLength >=lineLength )
linebase64 = Convert.ToBase64String( buffer, i*lineLength, lineLength );
else
linebase64 = Convert.ToBase64String( buffer, i*lineLength, buffer.Length - i*lineLength);
sb.Append( "=?" );
sb.Append(charset);
sb.Append( "?B?" );
sb.Append(linebase64);
sb.Append( "?=rnt" );
}
sb.Remove( sb.Length-3, 3); // Remove the last newline character
return sb.ToString();
}
Then, modify the processing of the email subject in the ToString method in the MailMessage class to call a custom method
// sb.Append("Subject: " + MailEncoder.ConvertHeaderToQP(cleanSubject.ToString(), charset) + "rn");
sb.Append("Subject: " + MailEncoder.ConvertHeaderToBase64( cleanSubject.ToString(), charset) + "rn");
Just recompile
Source: haogj