📄 mailinfo.cs
字号:
// $Id: MailInfo.cs,v 1.2 2006/08/07 11:00:27 ethem Exp $
namespace Erle.DnsMail
{
using System;
using System.Text;
using System.Collections;
using HttpContext = System.Web.HttpContext;
using HttpRequest = System.Web.HttpRequest;
public class MailInfo
{
private const string LOCALHOST = "localhost";
internal static readonly string DefaultCharset = System.Text.Encoding.Default.BodyName;
private static readonly Encoding DefaultEncoder = Encoding.GetEncoding(DefaultCharset);
private string mailserver;
private string mailserveruser;
private string mailserverpass;
internal Encoding encoder = DefaultEncoder;
private MailInfo(bool useMailServer)
{
recipients = useMailServer ? new RecipientCollection(true) : new RecipientCollection(1);
}
public MailInfo() : this(false) { }
public MailInfo(string from, string to, string subject, string body)
: this(false)
{
From = from;
Recipients.Add(to);
Subject = subject;
Body = body;
}
public MailInfo(string mailServer)
: this(true)
{
SetUpMailServer(mailServer);
}
public MailInfo(string mailServer, string msUsername, string msPassword)
: this(true)
{
SetUpMailServer(mailServer);
this.mailserveruser = msUsername;
this.mailserverpass = msPassword;
}
private void SetUpMailServer(string server)
{
if (server == null || server == string.Empty)
this.mailserver = LOCALHOST;
else
this.mailserver = server.Trim();
}
private RecipientCollection recipients;
public RecipientCollection Recipients
{
get { return recipients; }
set { recipients = value; }
}
private ArrayList headers;
public ArrayList Headers
{
get
{
if (headers == null)
headers = new ArrayList(2);
return headers;
}
}
private ContentType ct = Erle.DnsMail.ContentType.Text;
public ContentType ContentType
{
get { return ct; }
set { ct = value; }
}
private string charset = DefaultCharset;
public String Charset
{
get { return charset; }
set
{
if (value != null && value != string.Empty)
{
value = value.Trim();
if (value != string.Empty)
{
try
{
Encoding enc = Encoding.GetEncoding(value);
charset = value;
encoder = enc;
}
catch
{
}
}
}
}
}
private string _mailFrom = string.Empty;
public String From
{
get { return _mailFrom; }
set { _mailFrom = value.Trim(); }
}
private string _mailFromName;
public String FromName
{
get { return _mailFromName; }
set { _mailFromName = value.Trim(); }
}
private string _mailReplyTo;
public String ReplyTo
{
get { return _mailReplyTo; }
set { _mailReplyTo = value.Trim(); }
}
private string _mailSubject;
public String Subject
{
get { return _mailSubject; }
set { _mailSubject = value.Trim(); }
}
private bool _senddata = true;
public bool SendMail
{
get { return _senddata; }
set { _senddata = value; }
}
internal bool sent;
public bool Sent
{
get { return sent; }
}
private string _mailBody = string.Empty;
public String Body
{
get { return _mailBody; }
set { _mailBody = value; }
}
public string MailServer
{
get { return mailserver; }
}
public string MailServerUserName
{
get { return mailserveruser; }
}
public string MailServerPassword
{
get { return mailserverpass; }
}
public override string ToString()
{
return ToString(From, 1);
}
public string ToString(string to, int rcptcount)
{
StringBuilder header = new StringBuilder();
String __sd = DateTime.Now.ToUniversalTime().ToString("R", System.Globalization.DateTimeFormatInfo.InvariantInfo);
// Received, Date, X-Mailer, Standard Headers
if (HttpContext.Current != null)
{
HttpRequest req = HttpContext.Current.Request;
Object o = req.Headers["X-Forwarded-For"];
String userip = (o == null) ? req.UserHostAddress : o.ToString();
header.AppendFormat("Received: from [{0}]{1}\t by {2}{3}\t with HTTP",
userip, "\r\n", req.Url.Host, "\r\n");
if (rcptcount == 1)
header.AppendFormat("\r\n" + "\t for <{0}>", to);
header.AppendFormat("; {0}" + "\r\n", __sd);
}
header.AppendFormat("Date: {0}" + "\r\n", __sd);
header.Append("X-Mailer: Erle.DnsMail.v0.4.0.0" + "\r\n");
// Subject:
if ((Subject != null) && (Subject != string.Empty))
header.Append("Subject: " +
MIME.BEncode(encoder.GetBytes(Subject), Charset)
+ "\r\n");
// From:
if (FromName == null || FromName == string.Empty)
header.AppendFormat("From: <{0}>" + "\r\n", From);
else
header.AppendFormat("From: \"{0}\" <{1}>" + "\r\n",
MIME.BEncode(encoder.GetBytes(FromName), Charset),
From);
// To:
if (rcptcount == 1)
header.AppendFormat("To: <{0}>" + "\r\n", to);
else
header.Append("To: <Undisclosed-Recipient:;>" + "\r\n");
// Reply-To:
if (ReplyTo != null && ReplyTo != string.Empty)
header.Append("Reply-To: <" + ReplyTo + ">" + "\r\n");
// Custom headers:
foreach (String val in Headers)
{
if (val != null && val != String.Empty &&
val.IndexOf(": ", 0, val.Length) != -1 &&
val.IndexOf('\n', 0, val.Length) == -1
) header.Append(val.Trim() + "\r\n");
}
// Body
if (Body != null && Body.Length > 0)
{
header.Append("MIME-Version: 1.0" + "\r\n");
header.AppendFormat("Content-Type: text/{0}; charset={1}" + "\r\n",
ContentType == ContentType.Html ? "html" : "plain", Charset);
header.Append("Content-Transfer-Encoding: quoted-printable" + "\r\n");
}
header.Append("\r\n");
return header.ToString();
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -