分享乙個非同步傳送郵件的類

2021-08-26 16:25:10 字數 3521 閱讀 7953

首先要定義乙個郵件資訊的基類,如下所示:

///

/// base message class used for emails

///

public class message

#endregion

#region properties

///

/// whom the message is to

///

public virtual string to

///

/// the subject of the email

///

public virtual string subject

///

/// whom the message is from

///

public virtual string from

///

/// body of the text

///

public virtual string body

#endregion

}然後定義乙個郵件的傳送類,使用netmail的方式傳送郵件,傳送郵件時採用了.net中自帶的執行緒池,

通過多執行緒來實現非同步的傳送,**如下:

///

/// utility for sending an email

///

public class emailsender : message

#endregion

#region public functions

///

/// sends an email

///

/// the body of the message

public void sendmail(string message)

///

/// sends a piece of mail asynchronous

///

/// message to be sent

public void sendmailasync(string message));}

///

/// sends an email

///

public void sendmail()

;string addresscollection = to.split(splitter);

for (int x = 0; x < addresscollection.length; ++x)

if (!string.isnullorempty(cc))

}if (!string.isnullorempty(bcc))

}message.subject = subject;

message.from = new system.net.mail.mailaddress((from));

alternateview bodyview = alternateview.createalternateviewfromstring(body, null, mediatypenames.text.html);

foreach (linkedresource resource in embeddedresources)

message.alternateviews.add(bodyview);

= body;

message.priority = priority;

message.subjectencoding = system.text.encoding.getencoding("iso-8859-1");

message.bodyencoding = system.text.encoding.getencoding("iso-8859-1");

message.isbodyhtml = true;

foreach (attachment tempattachment in attachments)

system.net.mail.smtpclient smtp = new system.net.mail.smtpclient(server, port);

if (!string.isnullorempty(username) && !string.isnullorempty(password))

if (usessl)

smtp.enablessl = true;

else

smtp.enablessl = false;

smtp.send(message);}}

///

/// sends a piece of mail asynchronous

///

public void sendmailasync());}

#endregion

#region properties

///

/// any attachments that are included with this

/// message.

///

public listattachments

///

/// any attachment (usually images) that need to be embedded in the message

///

public listembeddedresources

///

/// the priority of this message

///

public mailpriority priority

///

/// server location

///

public string server

///

/// user name for the server

///

public string username

///

/// password for the server

///

public string password

///

/// port to send the information on

///

public int port

///

/// decides whether we are using starttls (ssl) or not

///

public bool usessl

///

/// carbon copy send (seperate email addresses with a comma)

///

public string cc

///

/// blind carbon copy send (seperate email addresses with a comma)

///

public string bcc

#endregion

}

C ,乙個郵件傳送的類

以下為郵件傳送使用 tp協議,在使用過程中曾經遇到郵件不能傳送的問題,如果你也遇到了,可以檢查一下windows的防火牆,windows自帶的防火牆如有啟用,可加入例外埠 25。如有安裝金山病毒防火牆,也需要將25埠的過濾禁用。郵件實體類 using system using system.coll...

乙個通過web Mail傳送郵件的類

using system using system.web using system.web.mail using dottext.framework using dottext.framework.configuration namespace yournamespace.email region...

乙個通過web Mail傳送郵件的類

using system using system.web using system.web.mail using dottext.framework using dottext.framework.configuration namespace yournamespace.email region...