110,546
社区成员
发帖
与我相关
我的任务
分享
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.163.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@163.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@163.com", "zzmdegm@yahoo.com.cn");
message.Subject = "测试";
message.Body = "用自己写的软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
try
{
client.Send(message);
}
catch (Exception ex)
{}
using System.Net.Mail;
/// <summary>
/// 使用Gmail邮箱发送邮件
/// </summary>
/// <param name="Sender">发信人</param>
/// <param name="SenderPassword">发信人密码</param>
/// <param name="Addressee">收信人</param>
/// <param name="maildisplayName">邮件名</param>
/// <param name="mailSubject">邮件主题</param>
/// <param name="mailBody">邮件内容</param>
/// <returns>返回空为发送成功,不为空是错误描述</returns>
public static string SendMailByGmail(string Sender, string SenderPassword, string Addressee, string maildisplayName, string mailSubject, string mailBody)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential(Sender, SenderPassword);
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = Addressee.Split(',');
try
{
mail.From = new MailAddress(Sender, maildisplayName, System.Text.Encoding.UTF8);
Byte i;
for (i = 0; i < addr.Length; i++)
mail.To.Add(addr[i]);
mail.Subject = mailSubject;
mail.Body = mailBody;
//if(ListBox1.Items.Count != 0)
//{
// for(i = 0;i<ListBox1.Items.Count;i++)
// mail.Attachments.Add(new Attachment(ListBox1.Items[i].ToString()));
//}
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(Addressee);
SmtpServer.Send(mail);
//Response.Write("<script>alert('发送成功!');</script>");
return "";
}
catch (Exception ex)
{
//Response.Write(ex.ToString());
return ex.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//SendMail _sm = new SendMail();
string sendExstr = SendMail.SendMailByGmail("zzmdegm@gmail.com", "******", this.TxtB_Addressee.Text.Trim(), "XX公司", "XX公司-用户密码找回!", "616818!");
if (sendExstr == "")
{ Response.Write("<script>alert('发送成功!');</script>"); }
else
{ Response.Write(sendExstr); }
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@sina.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@sina.com", "zzmdegm@163.com");
message.Subject = "测试aa";
message.Body = "用自己写的asdfasdf软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
try
{
client.Send(message);
}
catch (Exception ex)
{ }
#region 2、电子邮件
/// <summary>
/// 发送电子邮件方法
/// </summary>
/// <param name="from">发件人地址</param>
/// <param name="password">密码</param>
/// <param name="DisPalyName">对方收到时显示的名称</param>
/// <param name="to">收件人</param>
/// <param name="cc">抄送:默认为空</param>
/// <param name="subject">主题</param>
/// <param name="ssl">经过ssl加密</param>
/// <param name="priority">邮件优先级:MailPriority.High</param>
/// <param name="isbodyhtml">是否HTML邮件</param>
/// <param name="attachment">附件:没有则为null</param>
/// <param name="body">正文</param>
/// <returns>返回值:如果为空则成功,否则失败</returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, bool ssl, MailPriority priority, bool isbodyhtml, Attachment attachment, string body)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from, DisPalyName, Encoding.UTF8);
mm.To.Add(to);
mm.SubjectEncoding = Encoding.UTF8;
mm.Subject = subject;
if (attachment != null)
mm.Attachments.Add(attachment);
if (cc != null && cc != "")
mm.CC.Add(cc); //抄送
mm.IsBodyHtml = isbodyhtml; //是否是HTML邮件
mm.BodyEncoding = Encoding.UTF8; //编码
mm.Priority = priority; // MailPriority.High; //优先级
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Credentials = new System.Net.NetworkCredential(from, password);
//sc.Port = 25;
sc.Host = "smtp." + from.Substring(from.IndexOf('@') + 1);
try
{
sc.Send(mm);
return "";
}
catch (Exception ee)
{
return ee.ToString();
}
}
/// <summary>
/// 发送电子邮件方法
/// </summary>
/// <param name="from">发件人地址</param>
/// <param name="password">密码</param>
/// <param name="DisPalyName">对方收到时显示的名称</param>
/// <param name="to">收件人</param>
/// <param name="cc">抄送:默认为空</param>
/// <param name="subject">主题</param>
/// <param name="attachment">附件</param>
/// <param name="body">正文</param>
/// <returns>返回值:如果为空则成功,否则失败</returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, Attachment attachment, string body)
{
return SendEMail(from, password, DisPalyName, to, cc, subject, false, MailPriority.High, false, attachment, body);
}
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址</param>
/// /// <param name="MaxSize">最大容量,单位:M</param>
/// <returns></returns>
public static Attachment GetAttachment(string file, int MaxSize)
{
if (!File.Exists(file))
{
System.Web.HttpContext.Current.Response.Write("<script>alert('" + file + "文件不存在!')</script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
int size = 0;
FileStream fs = new FileStream(file, FileMode.Open);
string name = fs.Name;
size = (int)(fs.Length / 1024);
fs.Close();
//控制文件大小不大于10M
if (size > 1024 * MaxSize)
{
System.Web.HttpContext.Current.Response.Write("<script>alert('附件容量过大!')</script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
Attachment att = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = att.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
return att;
}
/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址</param>
/// <returns></returns>
public static Attachment GetAttachment(string file)
{
return GetAttachment(file, 10);
}
#endregion
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@sina.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@sina.com", "zzmdegm@yahoo.com.cn");
message.Subject = "测试";
message.Body = "用自己写的软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);
try
{
client.Send(message);
}
catch (Exception ex)
{ }
//群发邮件
private void SendMyMail()
{
string Accessory = ""; //附件
string SendTos = ""; //发送人
string CcTos = ""; //多个抄送人用逗号隔开,实现群发
string[] Receiver = System.Text.RegularExpressions.Regex.Split(CcTos, ","); //总收件人
string FromMail = ""; //发件人
string Title = ""; //邮件主题
string Body = ""; //邮件主体
string ShowName=""; //显示名字
string Host = "smtp.163.com"; //邮件服务器 ,如果使用其他邮件服务器一定要改这地方,根据发送邮箱而不同
string EmailPwd = ""; //发件人邮箱密码
if (SendTos!=""||CcTos!="")
{
//用户名(取发送邮箱的@前面的部分,即ustbwuyi1
string MailUserName = FromMail.Substring(0, FromMail.IndexOf("@"));
//密码
string MailPassword = EmailPwd;//发件人邮箱密码
MailMessage mailmessage = new MailMessage();
MailAddress n = new MailAddress(FromMail);
mailmessage.From = n;
mailmessage.Subject = Title;
mailmessage.IsBodyHtml = true;
mailmessage.Body = Body;
//添加附件
ArrayList FileNames = new ArrayList();
FileNames.Add(@Accessory);//附件地址
//如果存在附件
if (FileNames.Count>= 1)
{
//添加附件
string Filename;
for (int k = 0; k < FileNames.Count; k++)
{
Filename = FileNames[k].ToString();
mailmessage.Attachments.Add(new Attachment(Filename));
}
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = Host;
client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
//向收件人和抄送人循环发送
for (int j = 0; j < Receiver.Length; j++)
{
mailmessage.To.Add(Receiver[j].ToString());
try
{
client.Send(mailmessage);
Response.Write(" <script language='javascript'> alert('用户:" + Receiver[j] + "邮件发送成功') </script> ");
//System.Web.Mail.SmtpMail.Send(mailmessage);
}
catch (Exception ex)
{
Response.Write(" <script language='javascript'> alert('用户:"+Receiver[j]+"邮件发送失败:" + ex.Message + "') </script> ");
}
}
}
}