C#发邮件的代码 (vs2005)
2007-04-11 12:29
218 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class sendemail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s_from = TextBox1.Text.Trim().ToString();
string s_to = TextBox3.Text.Trim().ToString();
string pwd=TextBox2.Text .Trim().ToString();
string s_body =TextBox5.Text.Trim().ToString();
s_body = s_body.Replace("/r/n","<br/>");
s_body = s_body.Replace("/n", "<br/>");
s_body = s_body.Replace(" ", " ");
int i = s_from.IndexOf("@");
string username = s_from.Substring(0,i);
MailAddress from = new MailAddress(s_from);
MailAddress to = new MailAddress(s_to);
MailMessage mailobj = new MailMessage(from ,to);
mailobj.Subject = TextBox4.Text.Trim().ToString();
mailobj.Body = s_body;
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailobj.Priority = MailPriority.High;
if (FileUpload1.PostedFile.ContentLength > 0)
{
mailobj.Attachments.Add(new Attachment( FileUpload1.PostedFile.FileName));
}
SmtpClient smtp = new SmtpClient("smtp.163.com");
// smtp.Host = "smtp.163.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(username,pwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try {
smtp.Send(mailobj);
Response.Write("ok!");
}
catch
{
Response.Write("sorry!");
}
}
}
////因为用的邮箱都是163的,所以就设置了smtp的地址是163的地址,如果用别的邮箱,只要smtp的地址改下就可以了.
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class sendemail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s_from = TextBox1.Text.Trim().ToString();
string s_to = TextBox3.Text.Trim().ToString();
string pwd=TextBox2.Text .Trim().ToString();
string s_body =TextBox5.Text.Trim().ToString();
s_body = s_body.Replace("/r/n","<br/>");
s_body = s_body.Replace("/n", "<br/>");
s_body = s_body.Replace(" ", " ");
int i = s_from.IndexOf("@");
string username = s_from.Substring(0,i);
MailAddress from = new MailAddress(s_from);
MailAddress to = new MailAddress(s_to);
MailMessage mailobj = new MailMessage(from ,to);
mailobj.Subject = TextBox4.Text.Trim().ToString();
mailobj.Body = s_body;
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailobj.Priority = MailPriority.High;
if (FileUpload1.PostedFile.ContentLength > 0)
{
mailobj.Attachments.Add(new Attachment( FileUpload1.PostedFile.FileName));
}
SmtpClient smtp = new SmtpClient("smtp.163.com");
// smtp.Host = "smtp.163.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(username,pwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try {
smtp.Send(mailobj);
Response.Write("ok!");
}
catch
{
Response.Write("sorry!");
}
}
}
////因为用的邮箱都是163的,所以就设置了smtp的地址是163的地址,如果用别的邮箱,只要smtp的地址改下就可以了.
相关文章推荐
- C#发邮件的代码 (vs2005)收藏
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题
- VS2005 Addin续--编写C#代码统计插件
- C#实现用免费smtp服务器(GMail)发邮件,贴段整理好的代码
- 在wince6.0用vs2005下远程调试c#代码断点空心问题解决方式
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题题
- asp.net中分页的代码(For Oracle and VS2005 C#)
- VS2005中的全角BUG(C#代码)[转]
- IT民工系列——c#模拟VS2005的代码编辑器,对关键字染色
- 使用VS2005 C#编写随机数Random算法的代码(适用于连续产生随机数,重置后不适用)
- VS2005中C#代码,转到定义时,总是显示从元数据
- VS2005中C#代码,转到定义时,总是显示从元数据[转]
- VS2005 c# 遇到“Lc.exe 已退出,代码 -1”问题
- c# 发邮件的代码
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题
- vs2005 下的发邮件代码
- 使用VS2005 C#编写随机数Random算法的代码
- C# ASP.net发邮件代码
- 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题?
- VS2005(C#)使用AjaxPro.2实现把前台页面的值传到后台代码中