localization for <%Password%> and <%Username%> RRS feed

  • Question

  • User-330731364 posted

     I posted the following question under "security- membership controls" but did not receive a reply.  Perhaps I should post it here under localization and will get the right people to read and hopefully have the answer (Moderator, if I am not allowed to post the same posting twice, please forgive me and simply delete this.  Thanks)


    I use the <%Password%> and <%Username%> to insert the password and username to a html to create my own email.  However, my problem is the password and usename can only be read properly using unicode encoding.  Instead, I like them to be displayed properly when the GB2312 encoding is used (since most of my site's users uses mail clients that are set with GB2312 and will always read emails using the GB2321 encoding.  Some web based email clients will not even allow the user to switch to the unicode encoding.)  

     I have tried everything I can think of to make it work- saving the file in GB2312 encoding, use GB2312 meta tags, use the "<%@ Page RequestEncoding="gb2312" ResponseEncoding="gb2312" %>", etc.   Nothing works.  Can someone help?  Thanks


    Friday, December 8, 2006 3:54 PM

All replies

  • User-186692512 posted
    Maybe you can convert your values directly: 
    static byte[] ConvertUTF82OtherEncoding(string utf8String, string codePageForTargetEncoding)
        byte[] utf8AsBytes = Encoding.UTF8.GetBytes(utf8String);
        return Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding( codePageForTargetEncoding), utf8AsBytes);
     and ConvertUTF82OtherEncoding( Password, "gb2312") would return array of bytes which represents Password encoded in gb2312
    Saturday, December 9, 2006 5:39 PM
  • User-330731364 posted

    Thanks for your help and as soon as I found some time, I will try out your solution.  However, off the top of my head, I think I will encounter this problem with your solution...

     Even if the text is in gb2312, by the time the email gets to the user, it becomes gibberish.  The reason I know is because I tried sending gb2312 encoded text, it is gibberish by the time asp.net converts it to an email and send it to the user.  So by converting the username and password first to gb2312, it will simply repeat the above scenerio.  ASP.net is doing something behind the scenes that is causing this problem. 

     But in any case, I appreciate your suggestion and as soon as I find some time, I will still give it a try- again thank you.

    Sunday, December 10, 2006 11:42 PM
  • User-186692512 posted

    Ok, I am sorry if I am asking obvious, but did you try something like:

    // send the mail
    MailMessage msg = new MailMessage();
    msg.IsBodyHtml = false;
    msg.BodyEncoding = Encoding.GetEncoding("gb2312");
    msg.SubjectEncoding = Encoding.GetEncoding("gb2312");
    msg.From = new MailAddress(txtEmail.Text, txtName.Text);
    msg.To.Add(new MailAddress(Globals.Settings.ContactForm.MailTo));
    msg.Body = txtMessage.Text;
    new SmtpClient().Send(msg);


    No conversion at all - just compose message, specify encoding and send - what are results?

    Monday, December 11, 2006 3:13 AM
  • User-330731364 posted

    Thank you for your suggestion.  What you posted makes perfect sense to try... but sorry... I should have stated in the original post that I am looking for a solution specifically for "Visual Web Developer 2005 Express Edition".

     I use their PasswordRecovery control...

           <asp:PasswordRecovery ID="PasswordRecovery1" runat="server">
                <MailDefinition BodyFileName="~/EmailBody_gb2312.txt" Priority="High" Subject="This is your password">

    Do you know if it is at all possible to include Encoding.GetEncoding("gb2312") somehow into this.  If yes, how?

    If it is not possible, I just may have to completed drop this passwordrecovery control and write the entire code (as you have provided above) for this password recovery page myself   Thank you.



    Monday, December 11, 2006 11:14 AM
  • User-186692512 posted

    Of course, try something like that:

    - add additional attribute to to PasswordRecovery:


    - than do little programming:

      void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
        e.Message.IsBodyHtml = false;
        e.Message.BodyEncoding = Encoding.GetEncoding("gb2312");


    Hope this helps.
    Monday, December 11, 2006 11:33 AM
  • User-330731364 posted

    Thanks for leading me in the right direction.  Add attributes?   hmm...I hate programming but I guess if I must... I must. 

    When I come up wiht the exact code that works, I will come back and post it (hope it will  help others.)

    Again, thank you so much.

    (This .net and visual web studio thing really isn't a "one stop shopping" like I hope it will... but atleast microsoft is smart enough to create a forum like this to help out.)  [:P]


    Monday, December 11, 2006 1:55 PM
  • User-186692512 posted

    No problem.

    But, again, your request is rather unorthodox [:)]  

    Monday, December 11, 2006 2:00 PM
  • User-168896048 posted

    The solution turned out to be rather simple and not so obvious.

     When you save the e-mail template file, make sure you save the file with UTF8 encoding (or other encoding).  In Visual Studio, File->Advanced Save Options... and select the desired encoding. Then, there is no need to set the encoding programatically as the input file will be correct.

    Once I changed the template file encoding and saved it (yes, don't forget to save it-- doh!), the e-mails came through correctly encoded and all characters displayed correctly.



    Friday, September 28, 2007 5:53 PM