1个回答
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Data;
using System.Threading;
namespace MailObj
{
public delegate void SendCompleted(string MailObj_ID);
public class MailObj
{
private SmtpClient Client;
private MailMessage Mail;
private Attachment Att;
public string ID;
public string fromMailDisplayName;
public event SendCompleted CallSendCompleted;
public MailObj(string ID)
{
this.ID = ID;
}
public MailObj(string ID, string fromMailDisplayName)
{
this.ID = ID;
this.fromMailDisplayName = fromMailDisplayName;
}
public bool CreateMail(string From, string To, string Subject, string Body, string FileName, string Smtp, string Uid, string Pwd, bool IsBodyHtml)
{
try
{
MailAddress f = null;
if ( this.fromMailDisplayName.Length > 0 )
f = new MailAddress(From, this.fromMailDisplayName);
else
f = new MailAddress(From);
MailAddress t = new MailAddress(To);
this.Mail = new MailMessage(f, t);
this.Mail.Subject = Subject;
this.Mail.Body = Body;
this.Mail.IsBodyHtml = IsBodyHtml;
if (FileName != "")
{
Att = new Attachment(FileName);
this.Mail.Attachments.Add(Att);
}
this.Client = new SmtpClient(Smtp);
this.Client.DeliveryMethod = SmtpDeliveryMethod.Network;
this.Client.Timeout = 10000;
this.Client.Credentials = new System.Net.NetworkCredential(Uid, Pwd);
}
catch
{
Dispose();
return false;
}
return true;
}
public void SendAsync()
{
//try
//{
if (this.Client != null && this.Mail != null)
{
Client.SendCompleted += new SendCompletedEventHandler(Client_SendCompleted);
Client.SendAsync(this.Mail, new object());
}
//}
//catch
//{
//}
//finally
//{
//}
}
private void Client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (this.CallSendCompleted != null && this.ID != "")
this.CallSendCompleted(this.ID);
}
public void Dispose()
{
this.Client = null;
if ( this.Mail != null )
this.Mail.Dispose();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Data;
using System.Threading;
namespace MailObj
{
public delegate void SendCompleted(string MailObj_ID);
public class MailObj
{
private SmtpClient Client;
private MailMessage Mail;
private Attachment Att;
public string ID;
public string fromMailDisplayName;
public event SendCompleted CallSendCompleted;
public MailObj(string ID)
{
this.ID = ID;
}
public MailObj(string ID, string fromMailDisplayName)
{
this.ID = ID;
this.fromMailDisplayName = fromMailDisplayName;
}
public bool CreateMail(string From, string To, string Subject, string Body, string FileName, string Smtp, string Uid, string Pwd, bool IsBodyHtml)
{
try
{
MailAddress f = null;
if ( this.fromMailDisplayName.Length > 0 )
f = new MailAddress(From, this.fromMailDisplayName);
else
f = new MailAddress(From);
MailAddress t = new MailAddress(To);
this.Mail = new MailMessage(f, t);
this.Mail.Subject = Subject;
this.Mail.Body = Body;
this.Mail.IsBodyHtml = IsBodyHtml;
if (FileName != "")
{
Att = new Attachment(FileName);
this.Mail.Attachments.Add(Att);
}
this.Client = new SmtpClient(Smtp);
this.Client.DeliveryMethod = SmtpDeliveryMethod.Network;
this.Client.Timeout = 10000;
this.Client.Credentials = new System.Net.NetworkCredential(Uid, Pwd);
}
catch
{
Dispose();
return false;
}
return true;
}
public void SendAsync()
{
//try
//{
if (this.Client != null && this.Mail != null)
{
Client.SendCompleted += new SendCompletedEventHandler(Client_SendCompleted);
Client.SendAsync(this.Mail, new object());
}
//}
//catch
//{
//}
//finally
//{
//}
}
private void Client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
if (this.CallSendCompleted != null && this.ID != "")
this.CallSendCompleted(this.ID);
}
public void Dispose()
{
this.Client = null;
if ( this.Mail != null )
this.Mail.Dispose();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |