三种使用SMTP协议发送邮件的方法
来源:互联网 发布:游泳教学软件 编辑:程序博客网 时间:2024/05/22 04:23
(in C#) using System.Web.Mail; public void sendMail() { try { System.Web.Mail.MailMessage myMail=new MailMessage(); myMail.From = "myaccount@test.com"; myMail.To = "myaccount@test.com"; myMail.Subject = "MailTest"; myMail.Priority = MailPriority.Low; myMail.BodyFormat = MailFormat.Text; myMail.Body = "Test"; SmtpMail.SmtpServer="smarthost"; //your smtp server here SmtpMail.Send(myMail); } catch(Exception e) { throw e; } }
(in C#) public void CDOsendMail() { try { CDO.Message oMsg = new CDO.Message(); oMsg.From = "myaccount@test.com"; oMsg.To = "myaccount@test.com"; oMsg.Subject = "MailTest"; oMsg.HTMLBody = "Test"; CDO.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 = "myaccount@test.com"; //sender mail oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "myaccount@test.com"; //email account oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value="username"; oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value="password"; 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="smtp.21cn.com"; oFields.Update(); oMsg.BodyPart.Charset="gb2312"; oMsg.HTMLBodyPart.Charset="gb2312"; oMsg.Send(); oMsg = null; } catch (Exception e) { throw e; } }
注意:由于Exchange2000的CDO组件cdoex.dll会更新原有的Windows2000的CDO组件cdosys.dll,所以如果您希望继续使用cdosys.dll,您必须先通过regsrv32.exe卸载掉cdoex.dll。public void SendMail(MailMessage msg) { NetworkStream nwstream = GetConnection(); WriteToStream(ref nwstream, "EHLO " + smtpHost + "/r/n"); string welcomeMsg = ReadFromStream(ref nwstream); // implement HELO command if EHLO is unrecognized. if (IsUnknownCommand(welcomeMsg)) { WriteToStream(ref nwstream, "HELO " + smtpHost + "/r/n"); } CheckForError(welcomeMsg, ReplyConstants.OK); // Authentication is used if the u/p are supplied AuthLogin(ref nwstream); WriteToStream(ref nwstream, "MAIL FROM: <" + msg.From.Address + ">/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); SendRecipientList(ref nwstream, msg.To); SendRecipientList(ref nwstream, msg.CC); SendRecipientList(ref nwstream, msg.BCC); WriteToStream(ref nwstream, "DATA/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.START_INPUT); if (msg.ReplyTo.Name != null && msg.ReplyTo.Name.Length != 0) { WriteToStream(ref nwstream, "Reply-To: /"" + msg.ReplyTo.Name + "/" <" + msg.ReplyTo.Address + ">/r/n"); } else { WriteToStream(ref nwstream, "Reply-To: <" + msg.ReplyTo.Address + ">/r/n"); } if (msg.From.Name != null && msg.From.Name.Length != 0) { WriteToStream(ref nwstream, "From: /"" + msg.From.Name + "/" <" + msg.From.Address + ">/r/n"); } else { WriteToStream(ref nwstream, "From: <" + msg.From.Address + ">/r/n"); } WriteToStream(ref nwstream, "To: " + CreateAddressList(msg.To) + "/r/n"); if (msg.CC.Count != 0) { WriteToStream(ref nwstream, "CC: " + CreateAddressList(msg.CC) + "/r/n"); } WriteToStream(ref nwstream, "Subject: " + msg.Subject + "/r/n"); if (msg.Priority != null) { WriteToStream(ref nwstream, "X-Priority: " + msg.Priority + "/r/n"); } if (msg.Headers.Count > 0) { SendHeaders(ref nwstream, msg); } if (msg.Attachments.Count > 0 ¦ ¦ msg.HtmlBody != null) { SendMessageBody(ref nwstream, msg); } else { WriteToStream(ref nwstream, msg.Body + "/r/n"); } WriteToStream(ref nwstream, "/r/n./r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); WriteToStream(ref nwstream, "QUIT/r/n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.QUIT); CloseConnection(); }
- 三种使用SMTP协议发送邮件的方法
- 使用SMTP协议发送邮件
- 使用smtp协议发送邮件
- C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
- C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
- C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
- C#发送邮件三种方法,Localhost,SMTP,SSL-SMTP
- ruby写的使用smtp协议发送邮件
- C++ 使用smtp协议发送邮件的简单实现
- SMTP协议发送邮件
- VBS使用SMTP协议发送邮件
- 使用JavaMail SMTP协议发送邮件
- 使用JavaMail SMTP协议发送邮件
- Python使用smtp协议发送邮件
- SMTP协议应用:使用Socket发送邮件
- 关于SMTP协议发送邮件的总结
- SMTP邮件格式、SMTP 协议,SMTP的MIME写法,SMTP发送HTML邮件
- SMTP邮件格式、SMTP 协议,SMTP的MIME写法,SMTP发送HTML邮件
- 结构体
- pku 1273 Drainage Ditches( Edmond-Karp最大流模板)
- Linux源代码查看工具
- 十年目睹之怪现状--考核:我眼中的城管
- vim的一个配置文件+详解
- 三种使用SMTP协议发送邮件的方法
- Java事件处理机制-事件监听器的四种实现方式
- sqlserver用户自定义函数if判断
- 网站优化中的标题优化
- 2008年度我国大陆83种SCI收录刊影响因子IF排序
- 字节序(大端和小端)
- Objectdock stacks docklet 无法启动程序快捷方式
- 坚持学习WF(3):WF框架概览
- linux read调用说明