62,047
社区成员
发帖
与我相关
我的任务
分享
public delegate bool SendEmailDelegate(string Title, string DisplayName, string Content, string Attachment, string FromAdress);
public static bool SendEmailFunct(string Title, string DisplayName, string Content, string Attachment,string FromAdress)
{
string EmailSMTP = ConfigurationManager.AppSettings["EmailSMTP"].ToString();
string HostAdd = ConfigurationManager.AppSettings["HostAdd"].ToString();
string HostPwd = ConfigurationManager.AppSettings["HostPwd"].ToString();
string PortNum = "25";
string EnableSSL = "true";
bool isSend = true;
MailMessage msg = new MailMessage();
msg.To.Add(FromAdress);
msg.From = new MailAddress(HostAdd, DisplayName, System.Text.Encoding.UTF8);
msg.Subject = Title;//标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = Content;//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
//msg.Attachments = Attachment;
if (!string.IsNullOrEmpty(Attachment))
{
msg.Attachments.Add(new Attachment(Attachment));
}
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(HostAdd, HostPwd);
client.Port = Convert.ToInt32(PortNum);
client.Host = EmailSMTP;
client.EnableSsl = Convert.ToBoolean(EnableSSL);//必须
object userState = msg;
try
{
client.Send(msg);
}
catch (Exception ex)
{
isSend = false;
}
return isSend;
}
public static void AsyncSendEmail(string Title, string DisplayName, string Content, string Attachment, string FromAdress)
{
SendEmailDelegate dele = new SendEmailDelegate(SendEmailFunct);
IAsyncResult ar = dele.BeginInvoke(Title, DisplayName, Content, Attachment, FromAdress, null, null);
}