/// <summary> /// 完成的初始 /// </summary> void Init() { this.size = this.sm.Length; this.mimeType = "application/octet-stream"; tempname = MailCommon.MakeTempFileName(); this.fin = null; this.contentid = ""; }
/// <summary> /// 初始化新实例,并指定邮件发件人 /// </summary> /// <param name="From">邮件发送人</param> public MailMessage(MailAddress From) { this.from = From; this.boundary = MailCommon.MakeBoundary(); this.messageid = MailCommon.MakeMessageID(); this.subject = ""; this.enc = MailEncodeType.BASE64; this.charset = Encoding.GetEncoding("gb2312"); this.priority = MailPriority.Normal; this.text = ""; this.html = ""; }
/// <summary> /// 获取该邮件的QP编码方式编码结果,当未设置姓名时,姓名即为邮件地址 /// </summary> /// <param name="enc">要进行编码的编码类型</param> /// <param name="Charset">文件编码字符集</param> internal protected virtual string QPEncoding(Encoding enc, string Charset) { //判断姓名是否为空 if (string.IsNullOrEmpty(this.Name)) { return(string.Format("\"{0}\" <{0}>", this.Address)); } //判断姓名是否为键盘可打印,如果是,则不进行编码,如果不是,则进行编码 if (MailCommon.IsAscii(this.Name)) { return(string.Format("\"{0}\" <{1}>", this.Name, this.Address)); } //进行编码输出 return(Coding.EncodQPHead(this.Name, enc, Charset) + string.Format(" <{0}>", this.Address)); }
/// <summary> /// 获取附件的Mime头 /// </summary> /// <param name="NextPart">邮件分隔符</param> /// <param name="enc">字符编码类型</param> /// <param name="Charset">文件编码字符集</param> public string MimeHead(string NextPart, Encoding enc, string Charset) { StringBuilder sb = new StringBuilder(); //加入分隔 sb.Append("--"); sb.Append(NextPart + MailCommon.NewLine); if (!string.IsNullOrEmpty(this.ContentID)) { sb.AppendFormat("Content-ID: <{0}>", this.ContentID); sb.Append(MailCommon.NewLine); } sb.AppendFormat("Content-Type: {0};", this.MimeType); sb.Append(MailCommon.NewLine); string temp = ""; //判断文件名称是否须要编码 if (MailCommon.IsAscii(this.Name)) { temp = string.Format("=\"{0}\"", this.Name); } else { temp = string.Format("=\"{0}\"", Coding.EncodBase64Head(this.Name, enc, Charset)); } sb.Append(" name" + temp + MailCommon.NewLine); sb.Append("Content-Transfer-Encoding: base64" + MailCommon.NewLine); sb.Append("Content-Disposition: attachment;" + MailCommon.NewLine); sb.Append(" filename" + temp + MailCommon.NewLine); sb.Append(MailCommon.NewLine + MailCommon.NewLine); return(sb.ToString()); }
/// <summary> /// 进行邮件头的读取 /// </summary> /// <returns>返回所生成的邮件头值</returns> internal protected virtual string ReadHead() { if (this.From == null || string.IsNullOrEmpty(this.From.Address) || this.To == null || this.To.Count == 0) { throw new InvalidOperationException("Message Not Set From Or To !"); } if (string.IsNullOrEmpty(this.Subject)) { throw new InvalidOperationException("Not Set Subject!"); } StringBuilder sb = new StringBuilder(); //写入邮件ID sb.Append(this.MessageID + MailCommon.NewLine); //from sb.Append("From: "); sb.Append(this.EncodeType == MailEncodeType.BASE64 ? this.From.Base64Encoding(this.Charset, this.SetCharset) : this.From.QPEncoding(this.Charset, this.SetCharset)); sb.Append(MailCommon.NewLine); //replay if (this.replyto != null) { sb.Append("Replay-To: "); sb.Append(this.EncodeType == MailEncodeType.BASE64 ? this.ReplyTo.Base64Encoding(this.Charset, this.SetCharset) : this.ReplyTo.QPEncoding(this.Charset, this.SetCharset)); sb.Append(MailCommon.NewLine); } //to sb.Append("To: "); sb.Append(this.CreateAddressList(this.To)); sb.Append(MailCommon.NewLine); //cc if (this.Cc != null && this.Cc.Count > 0) { sb.Append("CC: "); sb.Append(this.CreateAddressList(this.Cc)); sb.Append(MailCommon.NewLine); } //Subject sb.Append("Subject: "); if (MailCommon.IsAscii(this.Subject)) { sb.Append(this.Subject); } else { sb.Append(this.EncodeType == MailEncodeType.BASE64 ? Coding.EncodBase64Head(this.Subject, this.Charset, this.SetCharset) : Coding.EncodQPHead(this.Subject, this.Charset, this.SetCharset)); } sb.Append(MailCommon.NewLine); //Data sb.Append("Date: "); sb.Append(DateTime.Now.ToString("R")); sb.Append(MailCommon.NewLine); //Mime sb.Append(MailCommon.MimeVersion); sb.Append(MailCommon.NewLine); //Notification if (this.Notification != null) { sb.Append("Disposition-Notification-To: "); sb.Append(this.EncodeType == MailEncodeType.BASE64 ? this.Notification.Base64Encoding(this.Charset, this.SetCharset) : this.Notification.QPEncoding(this.Charset, this.SetCharset)); sb.Append(MailCommon.NewLine); } //priority sb.Append("X-Priority: "); sb.Append((int)this.Priority); sb.Append(MailCommon.NewLine); sb.Append("X-MSMail-Priority: "); sb.Append(this.Priority.ToString()); sb.Append(MailCommon.NewLine); //Mailer sb.Append(MailCommon.XMailer); sb.Append(MailCommon.NewLine); //Content-Type sb.Append(CreateContentType()); sb.Append(MailCommon.NewLine); //自定Headers if (this.headers != null) { for (int i = 0; i < this.headers.Count; i++) { sb.Append(this.headers[i].ToString()); sb.Append(MailCommon.NewLine); } } sb.Append(MailCommon.NewLine); return(sb.ToString()); }