Basf_EProject/EProject/BLL/SendEmailService.cs

93 lines
3.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
///
/// </summary>
/// <param name="sender">发件人</param>
/// <param name="receiver">收件人</param>
/// <param name="title">标题</param>
/// <param name="content">内容</param>
/// <param name="FulPath">附件地址</param>
/// <param name="passWord">密码</param>
/// <returns></returns>
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;
}
}
}
}