日期:2014-05-19  浏览次数:20538 次

ASP.Net发送邮件如何配置
代码:
MailMessage   mailObj   =   new   MailMessage();

mailObj.From   =   this.txtfrom.Text;
mailObj.To   =   this.txtto.Text;
mailObj.Subject   =   "标题 ";
mailObj.Body   =   "邮件内容 ";

mailObj.BodyFormat   =   MailFormat.Html;
mailObj.Priority   =   MailPriority.High;
mailObj.Attachments.Add(new   MailAttachment( "D:\\新建   文本文档.txt "));

SmtpMail.Send(mailObj);

运行后出现:
“SendUsing”配置值无效。
这个错误,请问该如何配置呢?

------解决方案--------------------
//给你个邮件发送类,看看有用吗
using System;
using System.Web.Mail;


namespace midlink_healthcare
{

public class TestMail
{
string tb_from;
string tb_to;
string tb_mailBody;
string tb_smtpserver;

string tb_body;
string tb_sub;
public TestMail(string to ,string body,string sub)
{


tb_to=to.Trim();
//tb_name=name.Trim();
tb_body=body.Trim();
tb_sub=sub.Trim();


}

public void sengmill_net()
{


tb_from= "xxx@xxxx.com ";//你使用的邮箱名字

tb_mailBody=tb_body;
tb_smtpserver= "xxx.xx.xxx.xxx ";//你使用的邮件服务器的ip地址
System.Web.Mail.MailMessage mailmsg = new System.Web.Mail.MailMessage();
mailmsg.From = tb_from;
mailmsg.To = tb_to;
mailmsg.Body = tb_mailBody;
mailmsg.Subject =tb_sub;


mailmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1 ");
//是否需要验证,一般是要的
mailmsg.Fields.Add ( "http://schemas.microsoft.com/cdo/configuration/sendusername ", "xxxxx ");
//自己邮箱的用户名
mailmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword ", "xxxxxxx ");
//自己邮箱的密码
System.Web.Mail.SmtpMail.SmtpServer = tb_smtpserver;
System.Web.Mail.SmtpMail.Send(mailmsg);
}

}
}

------解决方案--------------------
/// <summary>
/// 发送邮件
/// </summary>
/// <param name= "strSmtpServer "> smtp地址 </param>
/// <param name= "UserName "> 用户名 </param>
/// <param name= "Password "> 密码 </param>
/// <param name= "strFrom "> 发信人地址 </param>
/// <param name= "strto "> 收信人地址 </param>
/// <param name= "strSubject "> 邮件标题 </param>
/// <param name= "strBody "> 邮件正文 </param>
public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);

//表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true;



//包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password);

//指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。