开发者

.NET or MSSMTP adding a line-break in the From header

I might be just blind (very possible!) but i can't find any information on here/Google anywhere that helps me out at all :(

First up, below is the code i'm using to send the email message:

MailMessage newMM = new MailMessage();

newMM.To.Add(new MailAddress(userEmail));
newMM.From = new MailAddress(getFrom, getFromName);
newMM.IsBodyHtml = true;
newMM.Subject = userSubject;
newMM.Body = userHTML;
if (chkEncoding.Checked)
{
    newMM.BodyEncoding = System.Text.Encoding.UTF8;
    newMM.SubjectEncoding = System.Text.Encoding.UTF8;
}

NetworkCredential basicAuthenticationInfo = new NetworkCredential(mUsername, mPassword);
SmtpClient smtp = new SmtpClient(mServer);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicAuthenticationInfo;
smtp.Send(newMM);

Which sends fine, and does UTF8 fine when enabled etc... however in the resulting email i get this (UTF8 or not):

...
MIME-Version: 1.0
From: "The name i want"
 <fromname@fromdomain.com>
To: someone@atadomain.com
...

And this is from checking the files in MSSMTP Queue folder, so it seems to me its .NET adding the line break in or MSSMTP doing it when it receives the email.

Anyone come across this before? or have ideas? :)

The reason this is important to fix is because Declude is saying that the from address doesn't match and adds spam weight which i can see via the headers:

X-RBL-Warning: FROMNOMATCH: Env sender (fromname@fromdomain.com) From: ("The name i want") mismatch.
...
X-Declude-Tests: ... FROMNOMATCH [2] ...

So i imagine other spam filters will also have a whinge about it.

EDIT:

For reference; All emails look perfectly fine in all email clients, this is purely a header rendering/spam processor problem.

If someone has a little spare time, could they possibly send a message using .NET and check the raw headers (unedited and unparsed by email client) and let me know? i'll keep on trucking for now :)

EDIT2:

I have created a basic SMTP server in .NET using sockets, basic replies (220's, 250's, 354's etc) and used the code to connect to it and send... and the problem occurs, so this is definitely at the code/.NET side of things and not MS-SMTP.

I have also created a brand new .NET 4.0 windows application, added a button and put in this code (note i added the using System.Net.Mail; to the top as well but nothing else from a blank new windows application):

private void button1_Click(object sender, EventArgs e)
{
    M开发者_运维技巧ailMessage newMM = new MailMessage();
    newMM.To.Add(new MailAddress("toaddress@domain.com"));
    newMM.From = new MailAddress("fromaddress@domain.com", "Happy As'Larry");
    newMM.IsBodyHtml = true;
    newMM.Subject = "My Subject";
    newMM.Body = "My HTML";
    SmtpClient smtp = new SmtpClient("localhost");
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Send(newMM);
}

Please note this is NOT edited in any way... as its local to a testing SMTP output application there is no need for verification so its exactly as it is above.

My SMTP application uses a TcpListener and Socket objects from System.Net.Sockets and just outputs all remote data to a simple text box, it parses a string for the command buffer so it can actually reply and get to the DATA command to check what .NET is sending however :)

Output is here:

EHLO WhiteDragon-PC
MAIL FROM:<fromaddress@domain.com>
RCPT TO:<toaddress@domain.com>
DATA
MIME-Version: 1.0
From: "Happy As'Larry"
 <fromaddress@domain.com>
To: toaddress@domain.com
Date: 28 Feb 2011 19:00:19 +1100
Subject: My Subject
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

My HTML

.

So with 2 fully new applications, with very basic code in each i still get the problem!

Is this a .NET 4.0 bug? if anyone wants the SMTP code i'll paste that too if you want to check this thing out.


Ok!

The last part of my edit turns up the results.

Switch the test client application to .NET 3.5 and the from header becomes:

From: "Happy As'Larry" <fromaddress@domain.com>

Switch the test client application to .NET 4.0 and the from header becomes:

From: "Happy As'Larry"
  <fromaddress@domain.com>

So this a problem in/change to the .NET 4.0 libraries and now i have a solution... change the profile to .NET 3.5!

Thanks to all who helped out :)

EDIT: Alignment and encoding of tags... heh

EDIT2: Additionally, this has fixed Declude's spam check headers, the NOFROMMATCH[2] weighting is gone so my emails are less 'spammy'

My suggestion is if you use .NET 4.0 and send emails... get this checked!


Comparing your source code to similar source code in one of my projects, the only possibility I come up with is:

Your getFrom variable contains the linefeed.


The problem is that earlier versions of MailMessage have tons of other bugs; take a look at this topic: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/879f13d7-24e6-4a0f-b396-627e9da25fc1/

So, if somebody could suggest a better class that doesn't rely on MailMessage - I would be really thankful.

And for your enjoyment, here is Encode method in .NET 4.0 that messes up your email address:

// Encodes the full email address, folding as needed
internal string Encode(int charsConsumed)
{ 
    string encodedAddress = String.Empty;
    IEncodableStream encoder; 
    byte[] buffer; 

    Debug.Assert(this.Address != null, "address was null"); 

    //do we need to take into account the Display name?  If so, encode it
    if (!String.IsNullOrEmpty(this.displayName))
    { 
        //figure out the encoding type.  If it's all ASCII and contains no CRLF then
        //it does not need to be encoded for parity with other email clients.  We will 
        //however fold at the end of the display name so that the email address itself can 
        //be appended.
        if (MimeBasePart.IsAscii(this.displayName, false)) 
        {
            encodedAddress = String.Format("\"{0}\"", this.displayName);
        }
        else 
        {
            //encode the displayname since it's non-ascii 
            encoder = encoderFactory.GetEncoderForHeader(this.displayNameEncoding, false, charsConsumed); 
            buffer = displayNameEncoding.GetBytes(this.displayName);
            encoder.EncodeBytes(buffer, 0, buffer.Length); 
            encodedAddress = encoder.GetEncodedString();
        }

        //the extra space is because there should be a non-printable whitespace char 
        //following the fold (CRLF) and there is supposed to be a space between the display name and
        //the address so this is necessary. 
        encodedAddress += "\r\n "; 
    }

    //now we have the encoded display name (if present), we need to append the address to it.
    if (!String.IsNullOrEmpty(encodedAddress))
    {
        //address should be enclosed in <> when a display name is present 
        encodedAddress += SmtpAddress;
    } 
    else 
    {
        //no display name, just return the address 
        encodedAddress = this.Address;
    }

    return encodedAddress; 

} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜