using System;
using System.Data;
using System.Configuration;
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 jmail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string from = "gg@126.com";
string to = this.TextBox1.Text.ToString();
string subject = "就不信发不出去";
string body = "个斑斑的,发出去了吧";
if (SendMail(from, to, subject, body))
{
this.Label2.Text = "发送成功";
}
else
{
this.Label2.Text = "发送失败";
}
}
public static bool SendMail(string from, string to, string subject, string body)
{
jmail.Message message = new Message();
//Jmail创建的日志,前提loging属性设置为true 可无
message.Logging = true;
message.Charset = "gb2312";
message.From = from;
message.Subject = subject;//添加标题
//这是添加收件人
message.AddRecipient(to, null, null);
//这里使用的是我的邮箱帐号,你自己重新申请。注意在邮箱设置-账户-POP/SMTP设置中,勾选开启。不然无法使用。
message.MailServerUserName = "....";//帐号
message.MailServerPassWord = "....";//密码
// message.ContentType = "html/text";
DateTime t=DateTime.Now;
//还有点问题没有解决,那就是如果设置 message.ContentType = "html/text" 那么收件中无法看到正常的正文,却莫名其妙多了一个附件。
message.Body = body+t.ToString();
try
{
message.Send("smtp.sina.com", false);
return true;
}
catch (Exception e)
{
return false;
}
}
}