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.
108 lines
3.9 KiB
108 lines
3.9 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
|
|
namespace Stone.WinService
|
|
{
|
|
public class NewMail
|
|
{
|
|
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 NewMail(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();
|
|
|
|
smtpClient.UseDefaultCredentials = false;
|
|
//if (!credentials)
|
|
{
|
|
|
|
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
|
|
|
|
|
|
}
|
|
|
|
//是否使用默认凭据,若为false,则使用自定义的证书,就是下面的networkCredential实例对象
|
|
|
|
|
|
|
|
|
|
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.Host = "smtp.office365.com";
|
|
smtpClient.SendCompleted += new SendCompletedEventHandler
|
|
(CompletedMethod);//注册[zhu ce]异步[yi bu]发送邮件[you jian]完成时的事件[shi jian]
|
|
smtpClient.SendAsync(mailMessage, mailMessage.Body);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|