基于LumiSoft.Net.dll发、收、删邮件
|
admin
2017年9月30日 17:15
本文热度 6910
|
发邮件:
- using LumiSoft.Net.SMTP.Client;
-
-
- Mime m = new Mime();
- MimeEntity mainEntity = m.MainEntity;
-
- mainEntity.From = new AddressList();
- mainEntity.From.Add(new MailboxAddress(txtFrom.Text, txtFrom.Text));
-
- mainEntity.To = new AddressList();
- mainEntity.To.Add(new MailboxAddress(txtTo.Text, txtTo.Text));
- mainEntity.Subject = txtSubject.Text;
-
- mainEntity.ContentType = MediaType_enum.Multipart_mixed;
- MimeEntity textEntity = mainEntity.ChildEntities.Add();
- textEntity.ContentType = MediaType_enum.Text_html;
- textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
- textEntity.DataText = txtBody.Text;
-
- MimeEntity attachmentEntity = new MimeEntity();
- attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
- attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;
- attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
- attachmentEntity.ContentDisposition_FileName = "c:/test.jpg";
- attachmentEntity.DataFromFile("c:/test.jpg");
-
- mainEntity.ChildEntities.Add(attachmentEntity);
-
- SMTP_Client.QuickSend(m);
using LumiSoft.Net.SMTP.Client;
Mime m = new Mime();
MimeEntity mainEntity = m.MainEntity;
// Force to create From: header field
mainEntity.From = new AddressList();
mainEntity.From.Add(new MailboxAddress(txtFrom.Text, txtFrom.Text));
// Force to create To: header field
mainEntity.To = new AddressList();
mainEntity.To.Add(new MailboxAddress(txtTo.Text, txtTo.Text));
mainEntity.Subject = txtSubject.Text;
//添加正文
mainEntity.ContentType = MediaType_enum.Multipart_mixed;
MimeEntity textEntity = mainEntity.ChildEntities.Add();
textEntity.ContentType = MediaType_enum.Text_html;
textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
textEntity.DataText = txtBody.Text;
//发送附件
MimeEntity attachmentEntity = new MimeEntity();
attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;
attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
attachmentEntity.ContentDisposition_FileName = "c:/test.jpg";
attachmentEntity.DataFromFile("c:/test.jpg");
mainEntity.ChildEntities.Add(attachmentEntity);
SMTP_Client.QuickSend(m);
收邮件:
- using LumiSoft.Net.POP3.Client;
- using LumiSoft.Net;
- using LumiSoft.Net.Mime;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net;
using LumiSoft.Net.Mime;
- public List<Mime> GetEmails(string pop3Server, string pop3Port, string username, string password)
- {
- bool pop3UseSsl = false;
- List<string> gotEmailIds = new List<string>();
- List<Mime> result = new List<Mime>();
- using (POP3_Client pop3 = new POP3_Client())
- {
- try
- {
- pop3.Connect(pop3Server, Convert.ToInt32(pop3Port), pop3UseSsl);
- pop3.Authenticate(username, password, false);
- POP3_ClientMessageCollection infos = pop3.Messages;
- foreach (POP3_ClientMessage info in infos)
- {
- if (gotEmailIds.Contains(info.UID))
- continue;
- byte[] bytes = info.MessageToByte();
- gotEmailIds.Add(info.UID);
- Mime mime = Mime.Parse(bytes);
- result.Add(mime);
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- return result;
- }
public List<Mime> GetEmails(string pop3Server, string pop3Port, string username, string password)
{
bool pop3UseSsl = false;
List<string> gotEmailIds = new List<string>();
List<Mime> result = new List<Mime>();
using (POP3_Client pop3 = new POP3_Client())
{
try
{
pop3.Connect(pop3Server, Convert.ToInt32(pop3Port), pop3UseSsl);
pop3.Authenticate(username, password, false);
POP3_ClientMessageCollection infos = pop3.Messages;
foreach (POP3_ClientMessage info in infos)
{
if (gotEmailIds.Contains(info.UID))
continue;
byte[] bytes = info.MessageToByte();
gotEmailIds.Add(info.UID);
Mime mime = Mime.Parse(bytes);
result.Add(mime);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
return result;
}
删除邮件:
- using LumiSoft.Net.POP3.Client;
- using LumiSoft.Net;
- using LumiSoft.Net.Mime;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net;
using LumiSoft.Net.Mime;
- private void DeleteMail()
- {
- using (POP3_Client c = new POP3_Client())
- {
- c.Connect(pop3Server, Convert.ToInt32(pop3Port));
- c.Authenticate(username, password, false);
- if (c.Messages.Count > 0)
- {
- foreach (POP3_ClientMessage mail in c.Messages)
- {
- mail.MarkForDeletion();
- }
- }
- }
- }
该文章在 2017/9/30 17:15:20 编辑过