asp.net如何把信息发到邮件,急用谢谢!!
解决方案 »
- 在datalist控件中commandname为select对应的事件吗?
- 急啊!!!ASP.NET数据库连接问题
- asp.net
- 在vs里面新建一个cs类文件,为什么输入"Response."不会智能弹出选择菜单
- 關于在SQL語句中﹐怎么格式化日期時間
- ###··简单问题,mailto,Button
- 如何取到DataGrid中的值?很急
- 如何将excel文件资料导入数据库
- 如何上传一个zip文件,然后将其解压缩?
- help:oledbConnection链接错误,无法使用web.config
- JavaScriptConverter 怎么用
- 请问datagrid根据前台已有table设置多行表头的问题!
using System;
using System.Collections.Generic;
using System.Text;namespace Shopping.Business
{
public class SysMailMessage
{
private string _subject;
private string _body;
private string _from;
private string _fromName;
private string _recipientName;
private string _mailDomain;
private int _mailserverport;
private string _username;
private string _password;
private bool _html;
private string _recipient; public SysMailMessage()
{ } /// <summary>
/// 邮件主题
/// </summary>
public string Subject
{
get
{
return this._subject;
}
set
{
this._subject = value;
}
} /// <summary>
/// 邮件正文
/// </summary>
public string Body
{
get
{
return this._body;
}
set
{
this._body = value;
}
}
/// <summary>
/// 发件人地址
/// </summary>
public string From
{
get
{
return _from;
}
set
{
this._from = value;
}
}
/// <summary>
/// 发件人姓名
/// </summary>
public string FromName
{
get
{
return this._fromName;
}
set
{
this._fromName = value;
}
}
/// <summary>
/// 收件人姓名
/// </summary>
public string RecipientName
{
get
{
return this._recipientName;
}
set
{
this._recipientName = value;
}
} /// <summary>
/// 邮箱域
/// </summary>
public string MailDomain
{
get
{
return this._mailDomain;
}
set
{
this._mailDomain = value;
}
} /// <summary>
/// 邮件服务器端口号
/// </summary>
public int MailDomainPort
{
set
{
this._mailserverport = value;
}
get
{
return this._mailserverport;
}
}
/// <summary>
/// SMTP认证时使用的用户名
/// </summary>
public string MailServerUserName
{
set
{
if (value.Trim() != "")
{
this._username = value.Trim();
}
else
{
this._username = "";
}
}
get
{
return _username;
}
} /// <summary>
/// SMTP认证时使用的密码
/// </summary>
public string MailServerPassWord
{
set
{
this._password = value;
}
get
{
return _password;
}
} /// <summary>
/// 是否Html邮件
/// </summary>
public bool Html
{
get
{
return this._html;
}
set
{
this._html = value;
}
} //收件人的邮箱地址
public bool AddRecipient(params string[] username)
{
//this._recipient= null;
this._recipient = username[0].Trim(); return true;
} /// <summary>
/// 发送
/// </summary>
/// <returns></returns>
public bool Send()
{ System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
Encoding eEncod = Encoding.GetEncoding("utf-8");
myEmail.From = new System.Net.Mail.MailAddress(this.From, this.From, eEncod);
myEmail.To.Add(this._recipient);
myEmail.Subject = this.Subject;
myEmail.Body = this.Body;
myEmail.Priority = System.Net.Mail.MailPriority.Normal;
//myEmail.BodyFormat = this.Html?MailFormat.Html:MailFormat.Text; //邮件形式,.Text、.Html
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = this.MailDomain;
smtp.Port = this.MailDomainPort;
smtp.Credentials = new System.Net.NetworkCredential(this.MailServerUserName, this.MailServerPassWord);
//smtp.UseDefaultCredentials = true;
//smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //当不是25端口(gmail:587)
if (this.MailDomainPort != 25)
{
smtp.EnableSsl = true;
}
//System.Web.Mail.SmtpMail.SmtpServer = this.MailDomain; try
{
smtp.Send(myEmail);
}
catch (System.Net.Mail.SmtpException e)
{
string result = e.Message;
return false;
} return true;
}
}
}
需要注意的是.smtp.163.com的东西需要匹配下面的邮箱. public void sendmail(string senduser, string recuser, string sub, string str)
{
if (senduser != null && recuser != null) //Send User & Receive User must be not null
{
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.163.com");
smtp.Credentials = new System.Net.NetworkCredential("***@163.com", "****");
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(senduser, recuser, sub, str);
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true; try
{
smtp.Send(msg); //send mail
}
catch (Exception err)
{
return; //return to page
} }
else
return;
}