欢迎光临
我们一直在努力

c#发送需要smtp认证的邮件-.NET教程,C#语言

建站超值云服务器,限时71元/月

————————-调用类 ———————————–

private void sendmaill(string username,string superpassword,string email) {

string body="尊敬的用户:<br><br>您好!<br><br>&nbsp;&nbsp;你的登录用户名是:"+username+"这是你的超级密码:"+superpassword

+"为了您的账号安全,我们建议您在收到邮件的72小时之内,到官方网站进行修改,并把该邮件删除。"

+"<br><br>"

+" 如有任何疑问,欢迎致电×××××发展有限责任公司客服热线:028-×××××××,我们将热情为您解答。"

+" 感谢您的支持!<br><br>"

+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;××××××发展有限责任公司<br><br>"

+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+system.datetime.now.year+"年"+system.datetime.now.month+"月"+system.datetime.now.day+"日<br><br>"

+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;官网:http://www.×××××.com<br><br>"

+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;××××:http://www.××××.com";

lion_office.data.library.mailclass.esmtpmail sm=new lion_office.data.library.mailclass.esmtpmail();

sm.send("mail.××××.com",

"×××@××××.com",

"××客服",

email,

username,

true,

"×××官方网站密码找回邮件",

body

);

session.remove("dd");

response.redirect("findspassok.aspx");

}

————————-基类———————

using system;

using system.text;

using system.io;

using system.net;

using system.net.sockets;

using system.collections;

namespace lion_office.data.library.mailclass

{

/*

create by lion

2002-11-20 01:44

copyright (c) 2001,2002 www.lionsky.net. all rights reserved.

web: http://www.lionsky.net ;;

email: lion-a@sohu.com

support .net framework beta 2

*/

public class esmtpmail

{

private string enter="\r\n";

/// <summary>

/// 设定语言代码,默认设定为gb2312,如不需要可设置为""

/// </summary>

public string charset="gb2312";

/// <summary>

/// 发件人地址

/// </summary>

public string from="";

/// <summary>

/// 发件人姓名

/// </summary>

public string fromname="";

/// <summary>

/// 回复邮件地址

/// </summary>

//public string replyto="";

/// <summary>

/// 收件人姓名

/// </summary>

public string recipientname="";

/// <summary>

/// 收件人列表

/// </summary>

private hashtable recipient=new hashtable();

/// <summary>

/// 邮件服务器域名

/// </summary>

private string mailserver="";

/// <summary>

/// 邮件服务器端口号

/// </summary>

private int mailserverport=25;

/// <summary>

/// smtp认证时使用的用户名

/// </summary>

private string username="";

/// <summary>

/// smtp认证时使用的密码

/// </summary>

private string password="";

/// <summary>

/// 是否需要smtp验证

/// </summary>

private bool esmtp=false;

/// <summary>

/// 是否html邮件

/// </summary>

public bool html=false;

/// <summary>

/// 邮件附件列表

/// </summary>

private system.collections.arraylist attachments;

/// <summary>

/// 邮件发送优先级,可设置为"high","normal","low"或"1","3","5"

/// </summary>

private string priority="normal";

/// <summary>

/// 邮件主题

/// </summary>

public string subject="";

/// <summary>

/// 邮件正文

/// </summary>

public string body="";

/// <summary>

/// 收件人数量

/// </summary>

private int recipientnum=0;

/// <summary>

/// 最多收件人数量

/// </summary>

private int recipientmaxnum=1;

/// <summary>

/// 密件收件人数量

/// </summary>

//private int recipientbccnum=0;

/// <summary>

/// 错误消息反馈

/// </summary>

private string errmsg;

/// <summary>

/// tcpclient对象,用于连接服务器

/// </summary>

private tcpclient tc;

/// <summary>

/// networkstream对象

/// </summary>

private networkstream ns;

/// <summary>

/// smtp错误代码哈希表

/// </summary>

private hashtable errcodeht = new hashtable();

/// <summary>

/// smtp正确代码哈希表

/// </summary>

private hashtable rightcodeht = new hashtable();

public esmtpmail()

{

attachments = new system.collections.arraylist();

}

/// <summary>

/// 邮件服务器域名和验证信息

/// 形如:"user:paswww.server.com:25",也可省略次要信息。如"user:paswww.server.com"www.server.com"

/// </summary>

public string maildomain

{

set

{

string maidomain=value.trim();

int tempint;

if(maidomain!="")

{

tempint=maidomain.indexof("@");

if(tempint!=-1)

{

string str=maidomain.substring(0,tempint);

mailserverusername=str.substring(0,str.indexof(":"));

mailserverpassword=str.substring(str.indexof(":")+1,str.length-str.indexof(":")-1);

maidomain=maidomain.substring(tempint+1,maidomain.length-tempint-1);

}

tempint=maidomain.indexof(":");

if(tempint!=-1)

{

mailserver=maidomain.substring(0,tempint);

mailserverport=system.convert.toint32(maidomain.substring(tempint+1,maidomain.length-tempint-1));

}

else

{

mailserver=maidomain;

}

}

}

}

/// <summary>

/// 邮件服务器端口号

/// </summary>

public int maildomainport

{

set

{

mailserverport=value;

}

}

/// <summary>

/// smtp认证时使用的用户名

/// </summary>

public string mailserverusername

{

set

{

if(value.trim()!="")

{

username=value.trim();

esmtp=true;

}

else

{

username="";

esmtp=false;

}

}

}

/// <summary>

/// smtp认证时使用的密码

/// </summary>

public string mailserverpassword

{

set

{

password=value;

}

}

/// <summary>

/// 邮件发送优先级,可设置为"high","normal","low"或"1","3","5"

/// </summary>

public string priority

{

set

{

switch(value.tolower())

{

case "high":

priority="high";

break;

case "1":

priority="high";

break;

case "normal":

priority="normal";

break;

case "3":

priority="normal";

break;

case "low":

priority="low";

break;

case "5":

priority="low";

break;

default:

priority="normal";

break;

}

}

}

/// <summary>

/// 错误消息反馈

/// </summary>

public string errormessage

{

get

{

return errmsg;

}

}

/// <summary>

/// 服务器交互记录

/// </summary>

private string logs="";

/// <summary>

/// 服务器交互记录,如发现本组件不能使用的smtp服务器,请将出错时的logs发给我(lion-a@sohu.com),我将尽快查明原因。

/// </summary>

public string logs

{

get

{

return logs;

}

}

/// <summary>

/// smtp回应代码哈希表

/// </summary>

private void smtpcodeadd()

{

errcodeht.add("500","邮箱地址错误");

errcodeht.add("501","参数格式错误");

errcodeht.add("502","命令不可实现");

errcodeht.add("503","服务器需要smtp验证");

errcodeht.add("504","命令参数不可实现");

errcodeht.add("421","服务未就绪,关闭传输信道");

errcodeht.add("450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)");

errcodeht.add("550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)");

errcodeht.add("451","放弃要求的操作;处理过程中出错");

errcodeht.add("551","用户非本地,请尝试<forward-path>");

errcodeht.add("452","系统存储不足,要求的操作未执行");

errcodeht.add("552","过量的存储分配,要求的操作未执行");

errcodeht.add("553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)");

errcodeht.add("432","需要一个密码转换");

errcodeht.add("534","认证机制过于简单");

errcodeht.add("538","当前请求的认证机制需要加密");

errcodeht.add("454","临时认证失败");

errcodeht.add("530","需要认证");

rightcodeht.add("220","服务就绪");

rightcodeht.add("250","要求的邮件操作完成");

rightcodeht.add("251","用户非本地,将转发向<forward-path>");

rightcodeht.add("354","开始邮件输入,以<enter>.<enter>结束");

rightcodeht.add("221","服务关闭传输信道");

rightcodeht.add("334","服务器响应验证base64字符串");

rightcodeht.add("235","验证成功");

}

/// <summary>

/// 将字符串编码为base64字符串

/// </summary>

/// <param name="estr">要编码的字符串</param>

private string base64encode(string str)

{

byte[] barray;

barray=encoding.default.getbytes(str);

return convert.tobase64string(barray);

}

/// <summary>

/// 将base64字符串解码为普通字符串

/// </summary>

/// <param name="dstr">要解码的字符串</param>

private string base64decode(string str)

{

byte[] barray;

barray=convert.frombase64string(str);

return encoding.default.getstring(barray);

}

/// <summary>

/// 得到上传附件的文件流

/// </summary>

/// <param name="filepath">附件的绝对路径</param>

private string getstream(string filepath)

{

//建立文件流对象

system.io.filestream filestr=new system.io.filestream(filepath,system.io.filemode.open);

byte[] by=new byte[system.convert.toint32(filestr.length)];

filestr.read(by,0,by.length);

filestr.close();

return(system.convert.tobase64string(by));

}

/// <summary>

/// 添加邮件附件

/// </summary>

/// <param name="path">附件绝对路径</param>

public void addattachment(string path)

{

attachments.add(path);

}

/// <summary>

/// 添加一个收件人

/// </summary>

/// <param name="str">收件人地址</param>

public bool addrecipient(string str)

{

str=str.trim();

if(str==null||str==""||str.indexof("@")==-1)

return true;

if(recipientnum<recipientmaxnum)

{

recipient.add(recipientnum,str);

recipientnum++;

return true;

}

else

{

errmsg+="收件人过多";

return false;

}

}

/// <summary>

/// 最多收件人数量

/// </summary>

public int recipientmaxnum

{

set

{

recipientmaxnum = value;

}

}

/// <summary>

/// 添加一组收件人(不超过recipientmaxnum个),参数为字符串数组

/// </summary>

/// <param name="str">保存有收件人地址的字符串数组(不超过recipientmaxnum个)</param>

public bool addrecipient(string[] str)

{

for(int i=0;i<str.length;i++)

{

if(!addrecipient(str[i]))

{

return false;

}

}

return true;

}

/// <summary>

/// 发送smtp命令

/// </summary>

private bool sendcommand(string str)

{

byte[] writebuffer;

if(str==null||str.trim()=="")

{

return true;

}

logs+=str;

writebuffer = encoding.default.getbytes(str);

try

{

ns.write(writebuffer,0,writebuffer.length);

}

catch

{

errmsg="网络连接错误";

return false;

}

return true;

}

/// <summary>

/// 接收smtp服务器回应

/// </summary>

private string recvresponse()

{

int streamsize;

string returnvalue = "";

byte[] readbuffer = new byte[1024] ;

try

{

streamsize=ns.read(readbuffer,0,readbuffer.length);

}

catch

{

errmsg="网络连接错误";

return "false";

}

if (streamsize==0)

{

return returnvalue ;

}

else

{

returnvalue = encoding.default.getstring(readbuffer).substring(0,streamsize);

logs+=returnvalue;

return returnvalue;

}

}

/// <summary>

/// 与服务器交互,发送一条命令并接收回应。

/// </summary>

/// <param name="command">一个要发送的命令</param>

/// <param name="errstr">如果错误,要反馈的信息</param>

private bool dialog(string str,string errstr)

{

if(str==null||str.trim()=="")

{

return true;

}

if(sendcommand(str))

{

string rr=recvresponse();

if(rr=="false")

{

return false;

}

string rrcode=rr.substring(0,3);

if(rightcodeht[rrcode]!=null)

{

return true;

}

else

{

if(errcodeht[rrcode]!=null)

{

errmsg+=(rrcode+errcodeht[rrcode].tostring());

errmsg+=enter;

}

else

{

errmsg+=rr;

}

errmsg+=errstr;

return false;

}

}

else

{

return false;

}

}

/// <summary>

/// 与服务器交互,发送一组命令并接收回应。

/// </summary>

private bool dialog(string[] str,string errstr)

{

for(int i=0;i<str.length;i++)

{

if(!dialog(str[i],""))

{

errmsg+=enter;

errmsg+=errstr;

return false;

}

}

return true;

}

private bool sendemail()

{

//连接网络

try

{

tc=new tcpclient(mailserver,mailserverport);

}

catch(exception e)

{

errmsg=e.tostring();

return false;

}

ns = tc.getstream();

smtpcodeadd();

//验证网络连接是否正确

if(rightcodeht[recvresponse().substring(0,3)]==null)

{

errmsg="网络连接失败";

return false;

}

string[] sendbuffer;

string sendbufferstr;

//进行smtp验证

if(esmtp)

{

sendbuffer=new string[4];

sendbuffer[0]="ehlo " + mailserver + enter;

sendbuffer[1]="auth login" + enter;

sendbuffer[2]=base64encode(username) + enter;

sendbuffer[3]=base64encode(password) + enter;

if(!dialog(sendbuffer,"smtp服务器验证失败,请核对用户名和密码。"))

return false;

}

else

{

sendbufferstr="helo " + mailserver + enter;

if(!dialog(sendbufferstr,""))

return false;

}

//

sendbufferstr="mail from:<" + from + ">" + enter;

if(!dialog(sendbufferstr,"发件人地址错误,或不能为空"))

return false;

//

sendbuffer=new string[recipientmaxnum];

for(int i=0;i<recipient.count;i++)

{

sendbuffer[i]="rcpt to:<" + recipient[i].tostring() +">" + enter;

}

if(!dialog(sendbuffer,"收件人地址有误"))

return false;

sendbufferstr="data" + enter;

if(!dialog(sendbufferstr,""))

return false;

sendbufferstr="from:" + fromname + "<" + from +">" +enter;

sendbufferstr += "to:=?"+charset.toupper()+"?b?"+base64encode(recipientname)+"?="+"<"+recipient[0]+">"+enter;

sendbufferstr+="cc:";

for(int i=0;i<recipient.count;i++)

{

sendbufferstr+=recipient[i].tostring() + "<" + recipient[i].tostring() +">,";

}

sendbufferstr+=enter;

if(charset=="")

{

sendbufferstr+="subject:" + subject + enter;

}

else

{

sendbufferstr+="subject:" + "=?" + charset.toupper() + "?b?" + base64encode(subject) +"?=" +enter;

}

sendbufferstr+="x-priority:" + priority + enter;

sendbufferstr+="x-msmail-priority:" + priority + enter;

sendbufferstr+="importance:" + 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(html)

{

sendbufferstr+="content-type: text/html;" + enter;

}

else

{

sendbufferstr+="content-type: text/plain;" + enter;

}

if(charset=="")

{

sendbufferstr+=" charset=\"iso-8859-1\"" + enter;

}

else

{

sendbufferstr+=" charset=\"" + charset.tolower() + "\"" + enter;

}

//sendbufferstr += "content-transfer-encoding: base64"+enter;

sendbufferstr+="content-transfer-encoding: base64" + enter + enter;

sendbufferstr+= base64encode(body) + enter;

if(attachments.count!=0)

{

foreach(string filepath in attachments)

{

sendbufferstr += "——=_nextpart_000_00d6_01c29593.aab31770"+enter;

sendbufferstr += "content-type: application/octet-stream"+enter;

sendbufferstr += " name=\"=?"+charset.toupper()+"?b?"+base64encode(filepath.substring(filepath.lastindexof("\\")+1))+"?=\""+enter;

sendbufferstr += "content-transfer-encoding: base64"+enter;

sendbufferstr += "content-disposition: attachment;"+enter;

sendbufferstr += " filename=\"=?"+charset.toupper()+"?b?"+base64encode(filepath.substring(filepath.lastindexof("\\")+1))+"?=\""+enter+enter;

sendbufferstr += getstream(filepath)+enter+enter;

}

}

sendbufferstr += "——=_nextpart_000_00d6_01c29593.aab31770–"+enter+enter;

sendbufferstr += enter + "." + enter;

if(!dialog(sendbufferstr,"错误信件信息"))

return false;

sendbufferstr="quit" + enter;

if(!dialog(sendbufferstr,"断开连接时错误"))

return false;

ns.close();

tc.close();

return true;

}

/// <summary>

/// 发送邮件方法,所有参数均通过属性设置。

/// </summary>

public bool send()

{

if(recipient.count==0)

{

errmsg="收件人列表不能为空";

return false;

}

if(mailserver.trim()=="")

{

errmsg="必须指定smtp服务器";

return false;

}

return sendemail();

}

/// <summary>

/// 发送邮件方法

/// </summary>

/// <param name="smtpserver">smtp服务器信息,如"username:passwordwww.smtpserver.com:25",也可去掉部分次要信息,如www.smtpserver.com"</param>

public bool send(string smtpserver)

{

maildomain=smtpserver;

return send();

}

/// <summary>

/// 发送邮件方法

/// </summary>

/// <param name="smtpserver">smtp服务器信息,如"username:passwordwww.smtpserver.com:25",也可去掉部分次要信息,如www.smtpserver.com"</param>

/// <param name="from">发件人mail地址</param>

/// <param name="fromname">发件人姓名</param>

/// <param name="to">收件人地址</param>

/// <param name="toname">收件人姓名</param>

/// <param name="html">是否html邮件</param>

/// <param name="subject">邮件主题</param>

/// <param name="body">邮件正文</param>

public bool send(string smtpserver,string from,string fromname,string to,string toname,bool html,string subject,string body)

{

maildomain=smtpserver;

from=from;

fromname=fromname;

addrecipient(to);

recipientname=toname;

html=html;

subject=subject;

body=body;

return send();

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#发送需要smtp认证的邮件-.NET教程,C#语言
分享到: 更多 (0)