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

359 lines
14 KiB
C#

2 years ago
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
1 year ago
using System.Reflection;
using System.IO;
using System.Globalization;
using System.Linq;
2 years ago
namespace JobSendAgentMail
{
/// <summary>
/// 邮件发送类
/// </summary>
public class SendEmail
{
private MailMessage mailMessage = new MailMessage(); //实例化一个邮件类
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toAddresses">收件人地址(多个以,号分开)</param>
/// <param name="fromAddress">发件人地址</param>
/// <param name="title">主题</param>
/// <param name="body">正文</param>
public SendEmail(string toAddresses, string fromAddress, string title, string body)
: this(toAddresses, fromAddress, "", "", title, body, false)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toAddress">收件人地址</param>
/// <param name="fromAddress">发件人地址</param>
/// <param name="toName">收件人名字</param>
/// <param name="fromName">发件人姓名</param>
/// <param name="title">主题</param>
/// <param name="body">正文</param>
/// <param name="isBodyHtml">正文是否为html格式</param>
public SendEmail(string toAddress, string fromAddress, string toName, string fromName, string title, string body, bool isBodyHtml)
{
1 year ago
2 years ago
mailMessage.From = new MailAddress(fromAddress, fromName, Encoding.GetEncoding(936));
if (toName.Equals(""))
mailMessage.To.Add(toAddress);
else
mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));
mailMessage.Subject = title;
mailMessage.SubjectEncoding = Encoding.GetEncoding(936);
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.BodyEncoding = Encoding.GetEncoding(936);
}
#endregion
/// <summary>
/// 设置SMTP并且将邮件发送出去
/// 所有参数都设置完成后再调用该方法
/// </summary>
/// <param name="password">发件人密码</param>
/// <param name="smtpHost">SMTP服务器地址</param>
public void SetSmtp(string password, string smtpHost)
{
SetSmtp(mailMessage.From.Address, password, smtpHost, 25, false, MailPriority.Normal);
}
/// <summary>
/// 设置SMTP并且将邮件发送出去
/// 所有参数都设置完成后再调用该方法
/// </summary>
/// <param name="address">发件人地址必须为真实有效的email地址</param>
/// <param name="password">发件人密码</param>
/// <param name="smtpHost">SMTP服务器地址</param>
/// <param name="smtpPort">SMTP服务器的端口</param>
/// <param name="isEnableSsl">SMTP服务器是否启用SSL加密</param>
/// <param name="priority">邮件的优先级</param>
public void SetSmtp(string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority)
{
SmtpClient smtp = new SmtpClient(smtpHost,smtpPort);
// smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
// smtp.UseDefaultCredentials = true;
//if (isEnableSsl)
//{
// smtp.UseDefaultCredentials = true;
// System.Net.NetworkCredential nc = new System.Net.NetworkCredential(address, password);
// smtp.Credentials = nc.GetCredential(smtpHost, smtpPort, "NTLM");
//}
//else
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(address, password);
smtp.Credentials = nc;
//smtp.Credentials = new NetworkCredential(address, password);
//smtp.Host = smtpHost;
//smtp.Port = smtpPort;
smtp.EnableSsl = isEnableSsl;
// smtp.UseDefaultCredentials = true;
// mailMessage.Priority = priority;
try
{
smtp.Send(mailMessage); //发送邮件
}
catch (Exception e)
{
smtp.Dispose();
}
finally
{
smtp.Dispose();
}
smtp.Dispose();
}
1 year ago
2 years ago
class Mailinfo {
public SendEmail se { get; set; }
public string address { get; set; }
public string password { get; set; }
public string smtpHost { get; set; }
public int smtpPort { get; set; }
public bool isEnableSsl { get; set; }
public MailPriority priority { get; set; }
public Mailinfo(SendEmail se, string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority)
{
this.se = se;
this.address = address;
this.password = password;
this.smtpHost = smtpHost;
this.smtpPort = smtpPort;
this.isEnableSsl = isEnableSsl;
this.priority = priority;
}
}
class ThreadMail
{
public void FuncSend(object o)
{
Mailinfo Mailinfo = o as Mailinfo;
Mailinfo.se.SetSmtp(Mailinfo.address, Mailinfo.password, Mailinfo.smtpHost, Mailinfo.smtpPort, Mailinfo.isEnableSsl, Mailinfo.priority);
}
}
public void SetSmtp_thread( string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority) {
ThreadMail tmail = new ThreadMail();
//有参调用实例方法ParameterizedThreadStart是一个委托input为object,返回值为void
Thread thread1 = new Thread(new ParameterizedThreadStart(tmail.FuncSend));
Mailinfo mi = new Mailinfo(this, address, password, smtpHost, smtpPort, isEnableSsl, priority);
thread1.Start(mi);
}
#region //设置邮件地址
/// <summary>
/// 设置更多收件人
/// </summary>
/// <param name="toAddresses">收件人地址</param>
public void SetMoreToAddress(string toAddresses)
{
mailMessage.To.Add(toAddresses);
}
/// <summary>
/// 设置更多收件人
/// </summary>
/// <param name="toAddresses">收件人地址</param>
/// <param name="toName">收件人名字</param>
public void SetMoreToAddress(string toAddress, string toName)
{
mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936)));
}
/// <summary>
/// 设置抄送者(多个以,号分开)
/// </summary>
/// <param name="ccAddresses">抄送者地址</param>
public void SetCarbonCopyFor(string ccAddresses)
{
mailMessage.CC.Add(ccAddresses);
}
/// <summary>
/// 设置抄送者
/// </summary>
/// <param name="ccAddress">抄送者地址</param>
/// <param name="ccName">抄送者名字</param>
public void SetCarbonCopyFor(string ccAddress, string ccName)
{
mailMessage.Bcc.Add(new MailAddress(ccAddress, ccName, Encoding.GetEncoding(936)));
}
/// <summary>
/// 设置密送者(多个以,号分开)
/// </summary>
/// <param name="bccAddresses">密送者</param>
public void SetBlindCarbonCopyFor(string bccAddresses)
{
mailMessage.Bcc.Add(bccAddresses);
}
/// <summary>
/// 设置密送者
/// </summary>
/// <param name="bccAddress">密送者</param>
/// <param name="bccName">密送者名字</param>
public void SetBlindCarbonCopyFor(string bccAddress, string bccName)
{
mailMessage.Bcc.Add(new MailAddress(bccAddress, bccName, Encoding.GetEncoding(936)));
}
#endregion
#region 添加附件
/// <summary>
/// 添加附件(自动识别文件类型)
/// </summary>
/// <param name="fileName">单个文件的路径</param>
public void Attachments(string fileName)
{
mailMessage.Attachments.Add(new Attachment(fileName));
}
/// <summary>
/// 添加附件默认为富文本RTF格式
/// </summary>
/// <param name="fileName">单个文件的路径</param>
public void AttachmentsForRTF(string fileName)
{
mailMessage.Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Rtf));
}
1 year ago
//public void SaveToEml(string emlFileAbsolutePath)
//{
// const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
// using (MemoryStream ms = new MemoryStream())
// {
//     Assembly assembly = typeof(System.Net.Mail.SmtpClient).Assembly;
//     Type tMailWriter = assembly.GetType("System.Net.Mail.MailWriter");
//     object mailWriter = Activator.CreateInstance(tMailWriter, flags, null, new object[] { ms }, CultureInfo.InvariantCulture);
// mailMessage.GetType().GetMethod("Send", flags).Invoke(mailMessage, new object[] { mailWriter, true });
//     File.WriteAllText(emlFileAbsolutePath, System.Text.Encoding.Default.GetString(ms.ToArray()), System.Text.Encoding.Default);
// }
//}
public struct MailBillType
{
public const string = "EDI";
public const string VGM = "VGM";
}
public struct MailInfo {
public const string _ = "DSFTP_EDI_FTP_EDIPATH";
public const string VGM_ = "DSFTP_EDI_FTP_VGMPATH";
public const string __ = "申请发送 ";
public const string __ = "舱单发送-";
public const string VGM__ = "申请发送VGM-";
public const string VGM__ = "VGM发送-";
}
public void SaveToEml(string emlFileAbsolutePath, string billtype)
{
var assembly = typeof(SmtpClient).Assembly;
var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
using (var memoryStream = new MemoryStream())
{
// Get reflection info for MailWriter contructor
var mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
// Construct MailWriter object with our FileStream
var mailWriter = mailWriterContructor.Invoke(new object[] { memoryStream });
// Get reflection info for Send() method on MailMessage
var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
//20210622 标题 按GB2312转base64
var mailhead = "";
var ftphead = "";
if(billtype== MailBillType.)
{
mailhead = MailInfo.__;
ftphead = MailInfo.__;
}
if (billtype == MailBillType.VGM)
{
mailhead = MailInfo.VGM__;
ftphead = MailInfo.VGM__;
}
string title = Convert.ToBase64String(System.Text.Encoding.GetEncoding(936).GetBytes(mailMessage.Subject.Replace(mailhead, ftphead)));
var codedtitle= "=?GB2312?B?"+title+"?=";
// Call method passing in MailWriter
sendMethod.Invoke(mailMessage, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);
// Finally get reflection info for Close() method on our MailWriter
var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
var _str = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()).Replace(mailMessage.Subject, codedtitle);
File.WriteAllText(emlFileAbsolutePath, _str, System.Text.Encoding.Default);
//return Encoding.ASCII.GetString(memoryStream.ToArray());
}
}
public MailMessage getMailMessage() {
return mailMessage;
}
2 years ago
#endregion
1 year ago
/// <summary>
/// 将MailMessage保存为eml文件
/// </summary>
/// <param name="msg">待保存的具有内容的MailMessage</param>
/// <param name="emlFileAbsolutePath">保存后的eml文件的路径</param>
public void SaveToEml(string emlFileAbsolutePath)
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
using (MemoryStream ms = new MemoryStream())
{
Assembly assembly = typeof(System.Net.Mail.SmtpClient).Assembly;
Type tMailWriter = assembly.GetType("System.Net.Mail.MailWriter");
object mailWriter = Activator.CreateInstance(tMailWriter, flags, null, new object[] { ms }, CultureInfo.InvariantCulture);
mailMessage.GetType().GetMethod("Send", flags).Invoke(mailMessage, new object[] { mailWriter, true });
File.WriteAllText(emlFileAbsolutePath, System.Text.Encoding.Default.GetString(ms.ToArray()), System.Text.Encoding.Default);
}
}
2 years ago
}
}