当前位置: 代码迷 >> ASP.NET >> .net1.1发送邮件有关问题
  详细解决方案

.net1.1发送邮件有关问题

热度:605   发布时间:2013-02-25 00:00:00.0
.net1.1发送邮件问题
谁有.NET1发邮件的代码
只要输入邮件帐号密码 就可以给指定的邮箱发送邮件

------解决方案--------------------------------------------------------
using System;
using System.Web.Mail;

namespace SendMailTest
{
/// <summary>
/// EnhanceMailMessage 的摘要说明。
/// </summary>
public class EnhancedMailMessage : MailMessage
{
private string displayName;
private string smtpServerName;
private string smtpUserName;
private string smtpUserPassword;
private int smtpServerPort;
private bool smtpSSL;

public EnhancedMailMessage()
{
displayName = string.Empty;
smtpServerName = string.Empty;
smtpUserName = string.Empty;
smtpUserPassword = string.Empty;
smtpServerPort =465;
smtpSSL = true;
}

/// <summary>
/// The display name that will appear
/// in the recipient mail client
/// </summary>
public string DisplayName
{
set
{
displayName = value;
}
get
{
return displayName;
}
}
/// <summary>
/// SMTP server (name or IP address)
/// </summary>
public string SMTPServerName
{
set
{
smtpServerName = value;
}
get
{
return smtpServerName;
}
}

/// <summary>
/// Username needed for a SMTP server
/// that requires authentication
/// </summary>
public string SMTPUserName
{
set
{
smtpUserName = value;
}
get
{
return smtpUserName;
}
}

/// <summary>
/// Password needed for a SMTP server
/// that requires authentication
/// </summary>
public string SMTPUserPassword
{
set
{
smtpUserPassword = value;
}
get
{
return smtpUserPassword;
}
}

/// <summary>
/// SMTP server port (default 587)
/// </summary>
public int SMTPServerPort
{
set
{
smtpServerPort = value;
}
get
{
return smtpServerPort;
}
}

/// <summary>
/// If SMTP server requires SSL
/// </summary>
public bool SMTPSSL
{
set
{
smtpSSL = value;
}
get
{
return smtpSSL;
}
}

public void Send()
{
if (smtpServerName.Length == 0)
{
throw new Exception("SMTP Server not specified");
}
if (displayName.Length > 0)
{
this.Headers.Add("From",
string.Format("{0} <{1}>",
displayName, From));
}
// set SMTP server name
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpserver"] = smtpServerName;
// set SMTP server port
this.Fields["http://schemas.microsoft.com/cdo" +
"/configuration/smtpserverport"] = smtpServerPort;
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendusing"] = 2;

if (smtpUserName.Length > 0 && smtpUserPassword.Length > 0)
{
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpauthenticate"] = 1;

// set SMTP username
this.Fields["http://schemas.microsoft.com" +
"/cdo/configuration/sendusername"] = smtpUserName;
// set SMTP user password
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendpassword"] = smtpUserPassword;
}

// ssl if needed
  相关解决方案