//用 ASP.NET 实现邮件系统
System.Web.Mail名称空间 SmtpMail 是设定SMTP服务器 MailMessage最丰富
MailMessage m = new MailMessage(); m.Form = tbFrom.Text; m.To = tbTo.Text; m.Subject = tbSubject.Text; m.Body = tbBody.Text; //优先级 switch(ddlp.SelectedIndex) { case 0: m.Priority = MailPriority.High; break; case 1: m.Priority = MailPriority.Low; break; default: m.Priority = MailPriority.Normal; break; }
//格式 if(ddlp.SelectedIndex == 0) { m.BodyFormat = MailFormat.Text; } else { m.BodyFormat = MailFormat.Html; } //设置服务器 if(tbServer.Text!="") { SmtpMail.SmtpServer = tbServer.Text; } //处理附件 string strFileName = FileSelect.PostedFile.FileName; if(strFileName!="") { m.Attachments.Add(new MailAttachment(strFileName)); } SmtpMail.Send(m);
MailMessage的主要属性
Form:源地址 To:目的地址 Subject:邮件主题 priority:邮件优先级 Attachements:附件 Bcc:暗送地址 Cc:抄送地址 Body:邮件内容主体 Bodyformat:邮件格式(Html,Text) Bodyencoding:邮件编码(Base64,UUencode)
用Socket来实现Email系统
在Telnet下收发邮件 -POP3服务器 端口110 -SMTP服务器 端口25 常用命令: -发送: Mail From Rcpt To Data -收邮件: Stat:查询有几条新邮件 Retr n:查询指定邮件内容 Dele n:删除指定邮件
邮件收发系统
public class SMTP { public SMTP(){} public bool Send(string strSmtpServer,int nPort,string strSend,string strReceive,string strSubject,string strContent) { string smtpserer = strSmtpServer; TcpClient tcpc = new TcpClient(); try { tcpc.Connect(smtpserver,nPort); StreamReader sr; string strCmd; sr = new StreamReader(tcpc.GetStream(),Encoding.Default); //服务器连接成功以后,首先向服务器发送HELLO命令 strCmd = "Hello guo!"; SenSmtpCmd(tcpc,strCmd); //然后向服务器发送信件的成员的信箱 strCmd = "mail from:"+strSend; SenSmtpCmd(tcpc,strCmd); //向服务器发送收件人的信箱 SenSmtpCmd = "rcpt to:" +strReceive; SenSmtpCmd(tcpc,strCmd); //所有的准备工作都已经准备好了,下面开始进行邮件部分 strCmd = "data"; SenSmtpCmd(tcpc,strCmd); //邮件内容 strCmd = "Date:1234567\r\n"; strCmd = strCmd + "From:"+strSend +"\r\n"; strCmd = strCmd + "To:" + strReceive +"\r\n"; strCmd = strCmd +"Subject:"+strSubject +"\r\n\r\n"; strCmd = strCmd +strContent +"\r\n\r\n"; SenSmtpCmd(tcpc,strCmd); strCmd = "\r\n.\r\n"; SenSmtpCmd(tcpc,strCmd); tcpc.Close(); return true; } catch { return false; } } }
void SenSmtpCmd(TcpClient tcpc,String strCmd) { byte[] arrCmd; string strRet; StreamReader sr; Stream s; s = tcpc.GetStream(); strCmd = strCmd+"\r\n"; arrCmd = Encoding.Default.GetBytes(strCmd.ToCharArray()); s = tcpc.GetStream(): s.Write(arrCmd,0,strCmd.Length); //以下用于程序调试,显示服务器回应信息 sr = new StreamReader(tcpc.GetStream(),Encoding.Default); strRet = sr.ReadLine(); return; }
未完待续。。。。
|
请发表评论