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.

143 lines
4.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using MailAnalyzeTools.Common;
using System.Data;
using MailAnalyzeTools.Models;
namespace MailAnalyzeTools
{
class SendmaillTool
{
/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">收件人邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>est
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param>
/// <returns></returns>
public static bool sendmail(string sfrom, string sfromer, string sto, string cc, bool isbodyHtml, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPPort, string sSMTPuser, string sSMTPpass, out string errinfo)
{
errinfo = "";
////设置from和to地址
var user_addresses = sto.Split(',').ToList<string>();
MailAddress from = new MailAddress(sfrom, sfromer);
//MailAddress to = new MailAddress(sto, stoer);
////创建一个MailMessage对象
MailMessage oMail = new MailMessage( );
oMail.From = from;
//支持多邮件地址:逗号分割
for (int i = 0; i < user_addresses.Count; i++)
{
oMail.To.Add(user_addresses[i]);
}
//抄送
if (!string.IsNullOrEmpty(cc))
{
var cc_addresses = cc.Split(',').ToList<string>();
for (int i = 0; i < cc_addresses.Count; i++)
{
oMail.CC.Add(cc_addresses[i]);
}
}
//密送
// oMail.Bcc.Add("admin@dongshengsoft.com");
//多附件:半角分号分割
if (!string.IsNullOrEmpty(sfile))
{
var sfiles = sfile.Split(';').ToList() ;
foreach (var itemFile in sfiles)
{
if (System.IO.File.Exists(itemFile))
{
oMail.Attachments.Add(new Attachment(itemFile));
}
else
{
errinfo += string.Format("附件{0}不存在或者不可访问\n", itemFile);
}
}
}
//oMail.To.Add();
////邮件标题,避免中文乱码 2018-02-22
oMail.SubjectEncoding = System.Text.Encoding.UTF8;
oMail.Subject = sSubject;
////邮件内容
oMail.Body = sBody;
////邮件格式
oMail.IsBodyHtml = isbodyHtml;
oMail.BodyEncoding = System.Text.Encoding.UTF8;
////设置邮件的优先级为高:0正常,1低,2高
//oMail.Priority = MailPriority.High;
////发送邮件
if (sSMTPPort == "")
{
sSMTPPort = "25";
}
SmtpClient client = new SmtpClient(sSMTPHost, int.Parse(sSMTPPort));
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(sSMTPuser, sSMTPpass);
client.Credentials = nc;
////client.UseDefaultCredentials = false;
//client.Host = sSMTPHost;
// client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
//client.DeliveryMethod = SmtpDeliveryMethod.Network;
//如果 System.Net.Mail.SmtpClient 使用 SSL则为 true否则为 false。默认值为 false。
//client.EnableSsl=true;?
//if (sSMTPPort == "")
//{
// sSMTPPort = "25";
//}
//client.Port = int.Parse( sSMTPPort);
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
LogHelper.WriteLog(sSubject+":自动转发失败", err.Message);
errinfo += err.Message;
return false;
}
finally
{
////释放资源
oMail.Dispose();
}
}
}
}