最近开发中用了lion兄弟写的smtp邮件发送类,根据自己的需要适当做了点修改
邮件Subject和Body还有附件都以Base64编码
但接收到的邮件都是编码后的内容,一堆的垃圾字符,没有还原
求教高手指点迷津,感激不尽
付上一段代码(太长,放不完)
if(m_Charset=="")
{
SendBufferstr+="Subject:" + m_Subject + enter;
}
else
{
SendBufferstr+="Subject:" + "=?" + m_Charset.ToUpper() + "?B?" + Base64Encode(m_Subject) +"?=" +enter;
} SendBufferstr+="X-Priority:" + m_Priority + enter;
SendBufferstr+="X-MSMail-Priority:" + m_Priority + enter;
SendBufferstr+="Importance:" + m_Priority + enter;
SendBufferstr+="X-Mailer: Huolx.Pubclass" + enter;
SendBufferstr+="MIME-Version: 1.0" + enter; SendBufferstr += "Content-Type: multipart/mixed;"+enter;//内容格式和分隔符
SendBufferstr += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\""+enter;
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter; if(m_BodyFormat == MailFormat.Html)
{
SendBufferstr+="Content-Type: text/html;" + enter;
}
else
{
SendBufferstr+="Content-Type: text/plain;" + enter;
} if(m_Charset=="")
{
SendBufferstr+=" charset=\"iso-8859-1\"" + enter;
}
else
{
SendBufferstr+=" charset=\"" + m_Charset.ToLower() + "\"" + enter;
} SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter; SendBufferstr+= Base64Encode(m_Body) + enter;
//SendBufferstr+= "=?" + m_Charset.ToUpper() + "?B?" + Base64Encode(m_Body) +"?=" + enter;
if(m_Attachments.Count!=0)
{
foreach(string filepath in m_Attachments)
{
if( File.Exists(filepath ) )
{
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter;
SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += " name=\"=?"+m_Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter;
SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Disposition: attachment;"+enter;
SendBufferstr += " filename=\"=?"+m_Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter+enter;
SendBufferstr += GetByes(filepath)+enter+enter;
}
}
}
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--"+enter+enter; SendBufferstr += enter + "." + enter;
邮件Subject和Body还有附件都以Base64编码
但接收到的邮件都是编码后的内容,一堆的垃圾字符,没有还原
求教高手指点迷津,感激不尽
付上一段代码(太长,放不完)
if(m_Charset=="")
{
SendBufferstr+="Subject:" + m_Subject + enter;
}
else
{
SendBufferstr+="Subject:" + "=?" + m_Charset.ToUpper() + "?B?" + Base64Encode(m_Subject) +"?=" +enter;
} SendBufferstr+="X-Priority:" + m_Priority + enter;
SendBufferstr+="X-MSMail-Priority:" + m_Priority + enter;
SendBufferstr+="Importance:" + m_Priority + enter;
SendBufferstr+="X-Mailer: Huolx.Pubclass" + enter;
SendBufferstr+="MIME-Version: 1.0" + enter; SendBufferstr += "Content-Type: multipart/mixed;"+enter;//内容格式和分隔符
SendBufferstr += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\""+enter;
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter; if(m_BodyFormat == MailFormat.Html)
{
SendBufferstr+="Content-Type: text/html;" + enter;
}
else
{
SendBufferstr+="Content-Type: text/plain;" + enter;
} if(m_Charset=="")
{
SendBufferstr+=" charset=\"iso-8859-1\"" + enter;
}
else
{
SendBufferstr+=" charset=\"" + m_Charset.ToLower() + "\"" + enter;
} SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter; SendBufferstr+= Base64Encode(m_Body) + enter;
//SendBufferstr+= "=?" + m_Charset.ToUpper() + "?B?" + Base64Encode(m_Body) +"?=" + enter;
if(m_Attachments.Count!=0)
{
foreach(string filepath in m_Attachments)
{
if( File.Exists(filepath ) )
{
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter;
SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += " name=\"=?"+m_Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter;
SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Disposition: attachment;"+enter;
SendBufferstr += " filename=\"=?"+m_Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter+enter;
SendBufferstr += GetByes(filepath)+enter+enter;
}
}
}
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--"+enter+enter; SendBufferstr += enter + "." + enter;
{
if((source==null)||(source.Length ==0))
throw new ArgumentException("source is not valid"); ToBase64Transform tb64 = new ToBase64Transform();
MemoryStream stm = new MemoryStream();
int pos = 0;
byte[] buff; while (pos + 3 < source.Length)
{
buff = tb64.TransformFinalBlock (source, pos, 3);
stm.Write (buff, 0, buff.Length);
pos += 3;
} buff = tb64.TransformFinalBlock (source, pos, source.Length - pos);
stm.Write (buff, 0, buff.Length); return stm.ToArray();
}