c# ASP.NET 邮件发送
publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,Eve...
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SendMail(this.TextBox1.Text,this.TextBox2.Text,this.TextBox3.Text,this.FCKeditor1.Value,new ArrayList());
}
catch(Exception er)
{
throw new Exception("发送失败!!!",er);
}
}
private void SendMail(string FromEmako, string ToEmail, string Title, string Body, ArrayList aFileName)
{
MailMessage mymail = new MailMessage();
StringBuilder MailBody = new StringBuilder();
mymail.From = "<" + FromEmako + ">" + FromEmako;
mymail.To = ToEmail;
mymail.Subject = Title;
mymail.BodyFormat = MailFormat.Html;
MailBody.Append("<style>A:visited{TEXT-DECORATION:none}");
MailBody.Append("A:active {TEXT-DECORATION:none}");
MailBody.Append("A:hover {TEXT-DECORATION:underline}");
MailBody.Append("A:link {taxt-decoration:none}");
MailBody.Append("A:visited {taxt-decoration:none}");
MailBody.Append("BODY {FONT-FAMILY:Verdana,宋体;FONT-SIZE;9pt;}");
MailBody.Append("TD {FONT-FAMILY:Verdana,宋体;FONT-SIZE;9pt;}</style>");
MailBody.Append("<TABLE border=0 width='95%' align=center><TBODY><TR>");
MailBody.Append("<TD valign=middle align=top>");
MailBody.Append("</TD></TR>");
MailBody.Append("</TBODY></TABLE><br>");
mymail.Body = MailBody.ToString();
for (int i = 0; i < aFileName.Count; i++)
{
MailAttachment mailA = new MailAttachment(aFileName[i].ToString());
mymail.Attachments.Add(mailA);
}
SmtpMail.Send(mymail);
}
protected void Button2_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.FCKeditor1.Value = null;
Response.Write("你已经取消发送邮件!!!");
}
}
这是我比这书上敲的
发送邮件的程序,主要就是 mymail.Body = MailBody.ToString();
for (int i = 0; i < aFileName.Count; i++)
{
MailAttachment mailA = new MailAttachment(aFileName[i].ToString());
mymail.Attachments.Add(mailA);
}
SmtpMail.Send(mymail);
这一点不明白,请将一下,
还有书上说还要配置发送电子邮件的服务器之后才能发送,我没有接触过,请问在哪找这个服务器呀,如何配置!请把这一点详细的给我说说!急用,谢谢了!我没有积分了,不好意思呀! 展开
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SendMail(this.TextBox1.Text,this.TextBox2.Text,this.TextBox3.Text,this.FCKeditor1.Value,new ArrayList());
}
catch(Exception er)
{
throw new Exception("发送失败!!!",er);
}
}
private void SendMail(string FromEmako, string ToEmail, string Title, string Body, ArrayList aFileName)
{
MailMessage mymail = new MailMessage();
StringBuilder MailBody = new StringBuilder();
mymail.From = "<" + FromEmako + ">" + FromEmako;
mymail.To = ToEmail;
mymail.Subject = Title;
mymail.BodyFormat = MailFormat.Html;
MailBody.Append("<style>A:visited{TEXT-DECORATION:none}");
MailBody.Append("A:active {TEXT-DECORATION:none}");
MailBody.Append("A:hover {TEXT-DECORATION:underline}");
MailBody.Append("A:link {taxt-decoration:none}");
MailBody.Append("A:visited {taxt-decoration:none}");
MailBody.Append("BODY {FONT-FAMILY:Verdana,宋体;FONT-SIZE;9pt;}");
MailBody.Append("TD {FONT-FAMILY:Verdana,宋体;FONT-SIZE;9pt;}</style>");
MailBody.Append("<TABLE border=0 width='95%' align=center><TBODY><TR>");
MailBody.Append("<TD valign=middle align=top>");
MailBody.Append("</TD></TR>");
MailBody.Append("</TBODY></TABLE><br>");
mymail.Body = MailBody.ToString();
for (int i = 0; i < aFileName.Count; i++)
{
MailAttachment mailA = new MailAttachment(aFileName[i].ToString());
mymail.Attachments.Add(mailA);
}
SmtpMail.Send(mymail);
}
protected void Button2_Click(object sender, EventArgs e)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
this.FCKeditor1.Value = null;
Response.Write("你已经取消发送邮件!!!");
}
}
这是我比这书上敲的
发送邮件的程序,主要就是 mymail.Body = MailBody.ToString();
for (int i = 0; i < aFileName.Count; i++)
{
MailAttachment mailA = new MailAttachment(aFileName[i].ToString());
mymail.Attachments.Add(mailA);
}
SmtpMail.Send(mymail);
这一点不明白,请将一下,
还有书上说还要配置发送电子邮件的服务器之后才能发送,我没有接触过,请问在哪找这个服务器呀,如何配置!请把这一点详细的给我说说!急用,谢谢了!我没有积分了,不好意思呀! 展开
2个回答
展开全部
给你一个例子
public static void Send()
{
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.Host = "smtp.163.com"; //可以配置成其他邮件服务器
client.Port = 25;
client.Credentials = new NetworkCredential("发件人邮件帐号", "邮箱密码");
MailMessage mm = new MailMessage();
mm.Priority = MailPriority.High;
mm.From = new MailAddress("发件人邮件帐号");
mm.Sender = new MailAddress("发件人邮件帐号");
mm.To.Add(new MailAddress("收件人邮件帐号"));
mm.Subject = "到期提醒!";
StringBuilder sb = new StringBuilder();
sb.AppendLine("到期提醒!");
sb.AppendLine("请登录人力资源系统及时处理即将到期的合同");
sb.AppendLine(DateTime.Now.ToShortDateString());
sb.AppendLine("人力资源系统管理员");
mm.Body = sb.ToString();
mm.IsBodyHtml = true;
// client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(mm);
}
catch (Exception ex)
{
throw ex;
}
}
public static void Send()
{
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = false;
client.Host = "smtp.163.com"; //可以配置成其他邮件服务器
client.Port = 25;
client.Credentials = new NetworkCredential("发件人邮件帐号", "邮箱密码");
MailMessage mm = new MailMessage();
mm.Priority = MailPriority.High;
mm.From = new MailAddress("发件人邮件帐号");
mm.Sender = new MailAddress("发件人邮件帐号");
mm.To.Add(new MailAddress("收件人邮件帐号"));
mm.Subject = "到期提醒!";
StringBuilder sb = new StringBuilder();
sb.AppendLine("到期提醒!");
sb.AppendLine("请登录人力资源系统及时处理即将到期的合同");
sb.AppendLine(DateTime.Now.ToShortDateString());
sb.AppendLine("人力资源系统管理员");
mm.Body = sb.ToString();
mm.IsBodyHtml = true;
// client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(mm);
}
catch (Exception ex)
{
throw ex;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询