asp.net中进行电子邮件的发送

aris0519 2008-09-20 02:10:37
本人最近在学习asp.net2.0,麻烦哪位大哥有此类的源码,方便否,发到我的电子邮箱来.
Email:ithqm@163.com
...全文
112 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chouto 2008-09-22
  • 打赏
  • 举报
回复

using System;
using System.Web.Mail;

namespace SendMail
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
try
{
MailMessage Message = new MailMessage();
Message.To = args[0]; //收件人
Message.From = args[1]; //发件人
Message.Subject = args[2];//主题
Message.Body = args[3];//正文

//...............其它信息.................

try
{
SmtpMail.SmtpServer = "这里邮件服务器地址"; //例如:smtp.126.com
SmtpMail.Send(Message);
}
catch(System.Web.HttpException ehttp)
{
Console.WriteLine("{0}", ehttp.Message);
Console.WriteLine("Here is the full error message output");
Console.Write("{0}", ehttp.ToString());
}
}
catch(IndexOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
}
catch(System.Exception e)
{
Console.WriteLine("Unknown Exception occurred {0}", e.Message);
Console.WriteLine("Here is the Full Message output");
Console.WriteLine("{0}", e.ToString());
}
}
}
}
hs1983 2008-09-22
  • 打赏
  • 举报
回复
public void GetPwd(string userEmail)
{
string userPwd;
DataBase db = new DataBase();
string sql = "select * from [User] where user_Email='" + userEmail + "'";
DataRow dr = db.GetDataRow(sql);
if (dr != null)
{//获取用户密码
userPwd = dr["user_Pwd"].ToString();

MailMessage mail = new MailMessage("***@163.com", userEmail);
mail.Subject = "***用户找回密码";
mail.Body = "***用户您好: <br> <br>      您的用户名为:" + userEmail + " <br>      您的密码为:" + userPwd + " <br>      请妥善保管!点击: <a href=http://www.***.com >http://www.****.com </a> 返回!";
mail.IsBodyHtml = true; // 是否是HTML格式
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.163.com";//SMPT服务器IP
smtp.Port = 25;//SMPT端口号,一般为25
System.Net.NetworkCredential credential = new System.Net.NetworkCredential();
credential.UserName = "userName";//你的用户名
credential.Password = "Password";//你的密码
smtp.Credentials = credential;
smtp.Send(mail);
}
}
对了不要忘记:using System.Net.Mail;
  • 打赏
  • 举报
回复
Frame Work 2.0 版本下的发送邮件





/// <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) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。

client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

//建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject,strBody);

//定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");
message.SubjectEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");

//获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = false;

//指定邮件优先级

message.Priority = System.Net.Mail.MailPriority.Normal;

//添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");
if (strFileName != " " && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}


//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password);

//发送
client.Send(message);
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧