Asp.Net(C#)发送带有附件及显示图片的邮件
Asp.Net(C#)发送带有附件及显示图片的邮件Asp.Net(C#)发送带有附件及显示图片的邮件截图:
源代码:
源代码:
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.Mail;
using System.Text;
public partial class VJSDN_MAIL : System.Web.UI.Page
{
/*
发送带有附件及显示图片的邮件。
http://www.csframework.com copyrights reserved
*/
protected void Page_Load(object sender, EventArgs e)
{
//
}
protected void Button1_Click(object sender, EventArgs e)
{
//必须是域名内的地址
string logoFile = Server.MapPath("~/images/vjsdn.png");
this.SendEmail(logoFile);
}
public bool SendEmail(string logoFile)
{
//增加图片附件
Attachment imgLogo = new Attachment(logoFile);
imgLogo.Name = "vjsdn.png"; //定个名字
imgLogo.NameEncoding = Encoding.UTF8;
imgLogo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
imgLogo.ContentDisposition.Inline = true;
imgLogo.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = imgLogo.ContentId;
bool ret = this.Send("webadmin@vjsdn.com", "请激活帐号.(by www.vjsdn.com)", "请激活帐号.(by www.vjsdn.com)", imgLogo, true);
return ret;
}
public bool Send(string address, string subject, string body, Attachment attachment, bool isBodyHtml)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("yourmail@vjsdn.com"); //你的邮箱,用于发邮件
mail.To.Clear();
mail.To.Add(new MailAddress(address));
mail.Body = body;
mail.BodyEncoding = Encoding.UTF8;
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.IsBodyHtml = isBodyHtml;
mail.Priority = System.Net.Mail.MailPriority.Normal;
if (attachment != null) mail.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("mail.vjsdn.com", 25); //SMTP
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//邮件帐号,及密码.注意增的邮件帐号和邮件密码
smtp.Credentials = new System.Net.NetworkCredential("accountxxx", "pwdxxxx");
smtp.Send(mail);
return true;
}
catch
{
return false;
}
}
}
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.Mail;
using System.Text;
public partial class VJSDN_MAIL : System.Web.UI.Page
{
/*
发送带有附件及显示图片的邮件。
http://www.csframework.com copyrights reserved
*/
protected void Page_Load(object sender, EventArgs e)
{
//
}
protected void Button1_Click(object sender, EventArgs e)
{
//必须是域名内的地址
string logoFile = Server.MapPath("~/images/vjsdn.png");
this.SendEmail(logoFile);
}
public bool SendEmail(string logoFile)
{
//增加图片附件
Attachment imgLogo = new Attachment(logoFile);
imgLogo.Name = "vjsdn.png"; //定个名字
imgLogo.NameEncoding = Encoding.UTF8;
imgLogo.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
imgLogo.ContentDisposition.Inline = true;
imgLogo.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = imgLogo.ContentId;
bool ret = this.Send("webadmin@vjsdn.com", "请激活帐号.(by www.vjsdn.com)", "请激活帐号.(by www.vjsdn.com)", imgLogo, true);
return ret;
}
public bool Send(string address, string subject, string body, Attachment attachment, bool isBodyHtml)
{
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("yourmail@vjsdn.com"); //你的邮箱,用于发邮件
mail.To.Clear();
mail.To.Add(new MailAddress(address));
mail.Body = body;
mail.BodyEncoding = Encoding.UTF8;
mail.SubjectEncoding = Encoding.UTF8;
mail.Subject = subject;
mail.IsBodyHtml = isBodyHtml;
mail.Priority = System.Net.Mail.MailPriority.Normal;
if (attachment != null) mail.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("mail.vjsdn.com", 25); //SMTP
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//邮件帐号,及密码.注意增的邮件帐号和邮件密码
smtp.Credentials = new System.Net.NetworkCredential("accountxxx", "pwdxxxx");
smtp.Send(mail);
return true;
}
catch
{
return false;
}
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网