在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
MailAddress from = new MailAddress("邮件发送地址"); MailAddress to = new MailAddress("邮件接收地址"); MailMessage message = new MailMessage(from, to); message.Subject = "邮件标题"; message.IsBodyHtml = true;//邮件是否是HTML格式 message.Body = URLEncode.BackString(getvalues[2]); message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312"); SmtpClient client = new SmtpClient(getvalues[3]); client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential("发送邮箱地址", "发送邮箱密码"); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Send(message); 1.SMTP 方式: public void SendMailLog(string sourceName, string message) { MailMessage newMail = new MailMessage(); newMail.Subject = sourceName; newMail.From="**@sina.com"; newMail.To = "**@sina.com"; newMail.Body = message; newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "******"); //set your username here newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*****"); //set your password here newMail.Priority=MailPriority.High; SmtpMail.SmtpServer = "smtp.sina.com.cn"; SmtpMail.Send(newMail); } 附:在SMTP下使用线程发送邮件: http://www.dotnetspider.com/kb/Article1565.aspx 2.使用Jmail组件 public string SendPassword() { jmail.Message MailObj=new jmail.MessageClass(); MailObj.From="[email protected]"; //发件人的地址 MailObj.Logging=true; MailObj.MailServerUserName="xxxx"; //发件人用户名 MailObj.MailServerPassWord="xxxxx"; //服务器验证 MailObj.HTMLBody=" Test"; MailObj.Charset="gb2312"; MailObj.Subject="HELLO"; MailObj.FromName="Hey"; MailObj.AddRecipient([email protected],"User","A"); //添加接收人 MailObj.Priority=3; //发送 if(MailObj.Send("smtp.163.com",false)) { return "Success"; } else { return "Fail"; } 3.ASP.NET结合COM组件发送Email 在系统目录(如c:\winnt或c:\windows)的system32子目录中可以找到一个名称为cdosys.dll的文件,我们可以通过ASP.NET调用此COM组件来实现Email的发送。cdosys构建在SMTP协议和NNTP协议之上,并且作为Windows2000 Server的组件被安装,当然我们也可以使用Exchange2000中cdoex.dll来实现发送邮件的机制,由于cdosys.dll内嵌到了操作系统中,所以不用再去注册相应的其他邮件发送程序比如jmail等。 1、新建一个项目文件 2、添加引用系统目录下的cdosys.dll文件,在引用中会发现添加了两个要用到的接口:CDO,ADODB 3、添加新项文件SendMail.aspx,在其页面上放置三个Label,三个Textbox,作用分别为收件人地址、主题、内容,放置一个Button按钮。 4、切换到代码页,创建一下内容 使用CDO发送(Microsoft CDO) namespace GoldWeb.WebUI { using System; using System.Text; using CDO; using GoldWeb.Model; using GoldWeb.SystemFramework; /// <summary> /// CDO Email操作类 /// </summary> public class CdoMail { /// <summary> /// 构造函数 /// </summary> public CdoMail() { } public static int Send(AccountModel accountModel) { try { string email = ApplicationString.HtmlEncodeString(accountModel.Email); string userName = ApplicationString.HtmlEncodeString(accountModel.UserName); string password = ApplicationString.HtmlEncodeString(accountModel.Password); Message oMsg = new MessageClass(); oMsg.From = ""; // 邮件来源 oMsg.To = email; oMsg.Subject = "确认函"; StringBuilder sb = new StringBuilder(); sb.Append("你的邮件内容"); oMsg.HTMLBody = sb.ToString(); IConfiguration iConfg = oMsg.Configuration; ADODB.Fields oFields = iConfg.Fields; oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = email; //sender mail oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "[email protected]"; //email account oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = ""; // 邮箱用户名 oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = ""; // 邮箱密码 oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; //value=0 代表Anonymous验证方式(不需要验证) //value=1 代表Basic验证方式(使用basic (clear-text) authentication. //The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.) //Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express) oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804; oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = ""; // 邮件服务器地址 oFields.Update(); oMsg.BodyPart.Charset="gb2312"; oMsg.HTMLBodyPart.Charset="gb2312"; oMsg.Send(); oMsg = null; return 0; } catch { return 1; } } |
请发表评论