using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
namespace BLL
{
public class SendEmailService
{
///
///
///
/// 发件人
/// 收件人
/// 标题
/// 内容
/// 附件地址
/// 密码
///
public static bool SendEmail(string sender, string receiver, string title, string content, string FulPath, string passWord)
{
bool IsSend = true;
MailMessage mailObject = new MailMessage();
string fromAddress = sender; //发件人
MailAddress mayAddress = new MailAddress(fromAddress);
mailObject.SubjectEncoding = System.Text.Encoding.UTF8;
mailObject.BodyEncoding = System.Text.Encoding.UTF8;
//设置邮件的收件人
string address = "";
string[] mailNames = (receiver + ";").Split(';');
foreach (string name in mailNames)
{
if (name != string.Empty)
{
if (name.IndexOf('<') > 0)
{
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
else
{
address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
}
mailObject.To.Add(new MailAddress(address));//收件人
}
}
mailObject.From = mayAddress;
mailObject.Subject = title;// "标题";
mailObject.Body = content;//"内容";
mailObject.IsBodyHtml = false;
//设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
//设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
if (!String.IsNullOrEmpty(FulPath))
{
mailObject.Attachments.Add(new Attachment(FulPath));
mailObject.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
}
SmtpClient client;
client = new SmtpClient("smtp." + fromAddress.Substring(fromAddress.LastIndexOf("@") + 1));
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
client.Timeout = 60000;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(fromAddress, passWord);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(mailObject);
return IsSend;
}
catch (Exception ex)
{
ErrLogInfo.WriteLog(ex);
return !IsSend;
}
}
}
}