Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C# email
Recently,I try to write a program about receiving email.First of all,I received a email subject(wrote with English)
is very successful,but if the email subject wrote with Chinese,I only get some messy code(I think it's messy code,because I don't know how to convert),please help me!
 

There is some mainly code:
public class Imap
{
TcpClient _TcpClient = new TcpClient();
NetworkStream _NetWorkStream=null;
string Server = "imap.***.com";
private string ReceiveFromServer_3(ref NetworkStream NS)
        {
            StringBuilder SB = new StringBuilder();
            StreamReader sr = new StreamReader(NS);
 
            string strLine = sr.ReadLine();
 
            while (strLine == null || strLine.Length == 0)
                strLine = sr.ReadLine();
 
            SB.Append(strLine);
            if (sr.Peek() != -1)
            {
 
                while ((strLine = sr.ReadLine()) != "A2 OK [READ-WRITE] SELECT completed") 
                SB.Append(strLine);
            }
            return SB.ToString();
        }
public string FETCH_Subject(string num)//get email subject
        {
            SendToServe(ref _NetWorkStream, "A4 FETCH " + num +" BODY.PEEK[HEADER.FIELDS (SUBJECT)]");
            string strMsg = ReceiveFromServer_2(ref _NetWorkStream);
            string[] strSub = strMsg.Split(new char[] {':'});
 
            return strSub[1].Substring(1,strSub[1].Length-2);
       }
 public string SELECT(string strName)//select a box
        {
            SendToServe(ref _NetWorkStream, "A2 SELECT " + strName);
            string strMsg = ReceiveFromServer_3(ref _NetWorkStream);
            strMsg += " " + ReceiveFromServer_3(ref _NetWorkStream);
            string[] strInt=strMsg.Split(new char[] {' '});
            return strInt[1]+" "+strInt[4];
 
        }
 public bool Login(string user, string password)
        {
            _TcpClient = new TcpClient();
            try
            {
                _TcpClient.Connect(Server, Port);
            }
            catch
            {
                MessageBox.Show("correct!");
            }
            _NetWorkStream = _TcpClient.GetStream();
            if (_NetWorkStream == null)
            {
                MessageBox.Show("correct!");
            }
            string CMessage = ReceiveFromServer(ref _NetWorkStream);
            CheckCorrect(CMessage);
            try
            {
                SendToServe(ref _NetWorkStream, "A01 LOGIN " + user + " " + password);
                string strMsg = ReceiveFromServer(ref _NetWorkStream);
                CheckCorrect(strMsg);
                return true;
            }
            catch
            {
                return false;
            }
        }
}
 

 private void button1_Click(object sender, EventArgs e)
        {
            Imap im = new Imap();
            im.Login("******", "******");
           string[] strnum= im.SELECT("INBOX").Split(new char[] {' '});
           label2.Text = strnum[0]; label4.Text = strnum[1];
           im.FETCH_TEXT("1","1500");
        }
 
 
for example I want to get a Email Subject "田里的庄稼 土生土长",
I press the button and I got the text is "* OK [UIDVALIDITY 1] UIDs valid* FLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)* OK [PERMANENTFLAGS (\\Answered \\Seen \\Deleted \\Draft \\Flagged)] LimitedA2 OK [READ-WRITE] SELECT completed* 1 FETCH (BODY[HEADER.FIELDS (SUBJECT)] {35}Subject: =?gb18030?B?uf65/g==?=)"
 
thank you for read this problem!
Posted 29-Aug-13 5:14am
Comments
H.Brydon at 29-Aug-13 11:41am
   
Interesting... Most of the questions I see like this are people who mix up ansi and unicode and get Chinese when they are expecting text. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 29 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100