对于我们这些寄居于别人服务器的,一般都没有装,就要用到第三方免费邮件提供商
因为没有验证用户信息,发出的信件会被很多服务器所拒收,还是用JMAIL发吧
安装JMAIL组件,在VS里添加对组件的引用
using System;
using System.Web.Util;
using System.Web.Mail;
using jmail;//引用JMAIL
namespace shop.web
{
/// <summary>
/// mail 的摘要说明。
/// </summary>
///
//属性
public class mail_item
{
public string _server_user;
public string _server_pass;
public string _from;
public string _from_name;
public string _to;
public string _subject;
public string _body;
public string _smtp_server;
public string server_user
{
get
{
return _server_user;
}
set
{
_server_user=value;
}
}
public string server_pass
{
get
{
return _server_pass;
}
set
{
_server_pass=value;
}
}
public string from
{
get
{
return _from;
}
set
{
_from=value;
}
}
public string from_name
{
get
{
return _from_name;
}
set
{
_from_name=value;
}
}
public string to
{
get
{
return _to;
}
set
{
_to=value;
}
}
public string subject
{
get
{
return _subject;
}
set
{
_subject=value;
}
}
public string body
{
get
{
return _body;
}
set
{
_body=value;
}
}
public string smtp_server
{
get
{
return _smtp_server;
}
set
{
_smtp_server=value;
}
}
}
public class mail
{
public bool send(mail_item nn)
{
bool sed=true;
//POP3 pop3=new POP3();
Message msg=new Message();
msg.Silent = true;
msg.Logging = true;
msg.Charset = "gb2312";
//jmail.IMessage.MailServerUserName
msg.MailServerUserName =nn.server_user;
msg.MailServerPassWord =nn.server_pass;
msg.From =nn.from;
msg.FromName =nn.from_name;
msg.AddRecipient(nn.to,"","");
msg.Subject =nn.subject;
msg.Body =nn.body;
msg.AppendHTML(nn.body);//设置为HTML
if(!msg.Send(nn.smtp_server,false))
sed=false;
msg.Close();
return sed;
}
}
}
实例
mail mm=new mail();
item.server_user="ggy";//邮件服务器你的用户名
item.server_pass="fft";//邮件服务器你的密码
item.from="ggy@163.com";//你的邮箱
item.from_name="hubro";
item.to=email.Text;
item.subject="test";
item.body="<b>this is a text</b>";
item.smtp_server="smtp.163.com";//smtp邮件服务器
if(!mm.send(item))
{
msg="邮件发送失败,请联系管理员";
}
OK,可以发邮件了