日期:2014-05-20  浏览次数:20489 次

发邮件
private   void   btnSend_Click(object   sender,   System.EventArgs   e)
{
MailMessage   mailMsg=new   MailMessage();
mailMsg.To=txtTo.Text;
mailMsg.From=txtFrom.Text;
mailMsg.From=this.txtsubject.Text;
mailMsg.Body=txtMsg.Text;
mailMsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1 ");
mailMsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername ", "soundblas@163.com ");
mailMsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword ", "6311455 ");
try
{
SmtpMail.SmtpServer= "smtp.163.com ";
SmtpMail.Send(mailMsg);
Response.Write( "ok ");
}
catch(Exception   ex)
{
Response.Write( "false; ");
}

}
}
请问我跟书上写的一模一样,书上说可以发邮件为什么我运行后都是FALSE,希望能指导我应该怎么去写

------解决方案--------------------
1.看看你的IIS里的SMTP服务器安装了没有!
2.书上的有错误正常,他们都不负责任。上网搜一下代码吧!
3.using System;
using System.Web;

namespace EMailCommon
{
/// <summary>
/// Summary description for CDOSendMail.
/// </summary>
public class CDOSendMail
{
public CDOSendMail()
{
//
// TODO: Add constructor logic here
//
}

public static void sendMail(string mailFrom, string mailTo, string mailSubject, string mailBody)
{
try
{
CDO.Message oMsg = new CDO.Message();

oMsg.From = mailFrom;
oMsg.To = mailTo;
oMsg.Subject = mailSubject; // "MailTest ";

oMsg.HTMLBody = mailBody;


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= mailFrom; // "dhou@sagatechnologies.com "; //sender mail
oFields[ "http://schemas.microsoft.com/cdo/configuration/smtpaccountname "].Value= mailTo; // "dhou@sagatechnologies.com "; //email account
//oFields[ "http://schemas.microsoft.com/cdo/configuration/sendusername "].Value= "XXXXX@XXXXXX.com ";
oFields[ "http://schemas.microsoft.com/cdo/configuration/sendusername "].Value = System.Configuration.ConfigurationSettings.AppSettings[ "sendusername "];
//oFields[ "http://schemas.microsoft.com/cdo/configuration/sendpassword "].Value= "3308576636 ";
oFields[ "http://schemas.microsoft.com/cdo/configuration/sendpassword "].Value = System.Configuration.ConfigurationSettings.AppSettings[ "sendpassword "];
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.fabby.com ";
oFields[ "http://schemas.microsoft.com/cdo/configuration/smtpserver "].Value= System.Configuration.ConfigurationSettings.AppSettings[ "smtpserver "];