VBNET] 繞路解決 MailMessage 在 Header 亂編碼的 bug
相信很多人在MailMessage中的HEAD中塞入中文
會遇到標頭值有無效的字元
網路上有人把文字先編碼後再傳給MailMessage
也是個好的解決方法,但還是希望MS可以由底層解決
http://tlcheng.spaces.live.com/Blog/cns!145419920BFD55A7!4978.entry
C# 版本
internal string EncodeInnerString(string strSource, string strEncoding = null, bool bAllEncode = true)
{
try {
if ((strEncoding != null) && strEncoding.Length > 0)
Encoding = System.Text.Encoding.GetEncoding(strEncoding);
if (Encoding.CodePage != Encoding.Default.CodePage) {
string encName = Encoding.HeaderName;
string fmtEncode = "=?{0}?B?{1}?=";
string strHeaderItem = strSource;
System.Text.StringBuilder encHeader = new System.Text.StringBuilder();
string ascHeader = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(strHeaderItem));
string strBase64 = null;
if (bAllEncode) {
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
} else {
int ibl = 0;
int ubl = 0;
int sbl = 0;
int ebl = 0;
ubl = strHeaderItem.Length - 1;
ebl = -1;
ibl = -1;
do {
ibl += 1;
if (strHeaderItem.Substring(ibl, 1) != ascHeader.Substring(ibl, 1)) {
sbl = ibl;
encHeader.Append(strHeaderItem.Substring(ebl + 1, sbl - ebl - 1));
do {
ibl += 1;
if (strHeaderItem.Substring(ibl, 1) == ascHeader.Substring(ibl, 1)) {
ebl = ibl - 1;
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem.Substring(sbl, ebl - sbl + 1)));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
break; // TODO: might not be correct. Was : Exit Do
} else if (ibl == ubl) {
ebl = ibl;
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem.Substring(sbl, ebl - sbl + 1)));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
break; // TODO: might not be correct. Was : Exit Do
}
} while (!(ibl >= ubl));
}
} while (!(ibl >= ubl));
encHeader.Append(strHeaderItem.Substring(ebl + 1));
}
return encHeader.ToString();
} else {
return strSource;
}
} catch (Exception ex) {
throw ex;
}
}
會遇到標頭值有無效的字元
網路上有人把文字先編碼後再傳給MailMessage
也是個好的解決方法,但還是希望MS可以由底層解決
http://tlcheng.spaces.live.com/Blog/cns!145419920BFD55A7!4978.entry
C# 版本
internal string EncodeInnerString(string strSource, string strEncoding = null, bool bAllEncode = true)
{
try {
if ((strEncoding != null) && strEncoding.Length > 0)
Encoding = System.Text.Encoding.GetEncoding(strEncoding);
if (Encoding.CodePage != Encoding.Default.CodePage) {
string encName = Encoding.HeaderName;
string fmtEncode = "=?{0}?B?{1}?=";
string strHeaderItem = strSource;
System.Text.StringBuilder encHeader = new System.Text.StringBuilder();
string ascHeader = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(strHeaderItem));
string strBase64 = null;
if (bAllEncode) {
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
} else {
int ibl = 0;
int ubl = 0;
int sbl = 0;
int ebl = 0;
ubl = strHeaderItem.Length - 1;
ebl = -1;
ibl = -1;
do {
ibl += 1;
if (strHeaderItem.Substring(ibl, 1) != ascHeader.Substring(ibl, 1)) {
sbl = ibl;
encHeader.Append(strHeaderItem.Substring(ebl + 1, sbl - ebl - 1));
do {
ibl += 1;
if (strHeaderItem.Substring(ibl, 1) == ascHeader.Substring(ibl, 1)) {
ebl = ibl - 1;
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem.Substring(sbl, ebl - sbl + 1)));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
break; // TODO: might not be correct. Was : Exit Do
} else if (ibl == ubl) {
ebl = ibl;
strBase64 = System.Convert.ToBase64String(Encoding.GetBytes(strHeaderItem.Substring(sbl, ebl - sbl + 1)));
encHeader.AppendFormat(fmtEncode, encName, strBase64);
break; // TODO: might not be correct. Was : Exit Do
}
} while (!(ibl >= ubl));
}
} while (!(ibl >= ubl));
encHeader.Append(strHeaderItem.Substring(ebl + 1));
}
return encHeader.ToString();
} else {
return strSource;
}
} catch (Exception ex) {
throw ex;
}
}
留言