You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

3 years ago
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
namespace Stone.Common
{
public class MySmtpMail
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string server;
private string username;
private string password;
private bool enablessl;
private bool credentials;
private int port;
public MySmtpMail(string To, string From, string Body, string Title, string Server, string Username, string Password, bool EnableSsl, int Port, bool Credentials, string AttachmentFile)
{
mailMessage = new MailMessage();
mailMessage.To.Add(To);
mailMessage.From = new System.Net.Mail.MailAddress(From);
mailMessage.Subject = Title;
mailMessage.Body = Body;
if(AttachmentFile != "")
{
Attachment attachment = new Attachment(AttachmentFile);
mailMessage.Attachments.Add(attachment);
}
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
this.server = Server;
this.password = Password;
this.username = Username;
this.enablessl = EnableSsl;
this.port = Port;
this.credentials = Credentials;
}
public void Send()
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
if (!credentials)
{
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
}
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = server;
smtpClient.Port = port;
smtpClient.EnableSsl = enablessl;
smtpClient.Send(mailMessage);
}
}
#region ��ʱ���õ�
public void Attachments(string Path)
{
string[] path = Path.Split(',');
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);//ʵ����[shi li hua]
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//��ȡ
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//��ȡ��
mailMessage.Attachments.Add(data);//���ӵ�������
}
}
public void SendAsync(SendCompletedEventHandler CompletedMethod)
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential
(mailMessage.From.Address, password);//����[she zhi]����������[shen fen]��Ʊ��
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = "smtp." + mailMessage.From.Host;
smtpClient.SendCompleted += new SendCompletedEventHandler
(CompletedMethod);//ע��[zhu ce]�첽[yi bu]�����ʼ�[you jian]����ʱ���¼�[shi jian]
smtpClient.SendAsync(mailMessage, mailMessage.Body);
}
}
#endregion
}
}