webservice SMTP

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Services;

namespace webserviceTest1
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {        
        [WebMethod(Description ="SMTP Service PMC Suzhou")]
        public bool SendMail(string frommail,string Nickname_1, string sendTo, string copyTo, string subject, string body, byte[] attachment, string attachmentName, byte[] attachment_2, string attachmentName_2)
        {
            bool res = true;
            //string Host = ConfigurationManager.AppSettings["SmtpHost"];

            //string Port = ConfigurationManager.AppSettings["SmtpPort"];
            //string FromMail = ConfigurationManager.AppSettings["SendFrom"];
            //string Password = ConfigurationManager.AppSettings["Password"];
            //string EnableSSL = ConfigurationManager.AppSettings["EnableSSL"];
            //string IsAnonymous = ConfigurationManager.AppSettings["IsAnonymous"];

            string Host = "smtp.molex.com";
            string Port = "25";
            string FromMail = frommail;
            string Password = "";
            string EnableSSL = "False";
            string IsAnonymous = "True";

            string ToMail = sendTo == null ? "" : sendTo;
            string CCMail = copyTo == null ? "" : copyTo;

            Host = String.IsNullOrEmpty(Host) ? "localhost" : Host;
            Port = String.IsNullOrEmpty(Port) ? "25" : Port;
            FromMail = String.IsNullOrEmpty(FromMail) ? "PMC_Suzhou_Digital@molex.com" : FromMail;
            Password = String.IsNullOrEmpty(Password) ? "" : Password;
            EnableSSL = String.IsNullOrEmpty(EnableSSL) ? "False" : EnableSSL;
            IsAnonymous = String.IsNullOrEmpty(IsAnonymous) ? "True" : IsAnonymous;

            if (ToMail == "" && CCMail == "")
            {
                return false;
            }
            try
            {
                //实例化一个发送邮件类。
                MailMessage mailMessage = new MailMessage();
                //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
                mailMessage.Priority = MailPriority.Normal;
                //发件人邮箱地址。
                mailMessage.From = new MailAddress(FromMail,Nickname_1, Encoding.UTF8);
                //收件人邮箱地址。需要群发就写多个
                //拆分邮箱地址
                List<string> ToMailList = ToMail.Split(';').ToList();
                for (int i = 0; i < ToMailList.Count; i++)
                {
                    mailMessage.To.Add(new MailAddress(ToMailList[i]));  //收件人邮箱地址。
                }

                if (CCMail != null && CCMail != "")
                {
                    List<string> CCMailList = CCMail.Split(';').ToList();
                    for (int i = 0; i < CCMailList.Count; i++)
                    {
                        邮件的抄送者,支持群发
                        mailMessage.CC.Add(new MailAddress(CCMailList[i]));
                    }
                }
                //如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
                mailMessage.SubjectEncoding = Encoding.GetEncoding(936);
                //邮件正文是否是HTML格式
                mailMessage.IsBodyHtml = true;
                //邮件标题。
                mailMessage.Subject = subject;
                //邮件内容。
                mailMessage.Body = body;

                //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中  
                if (attachment != null)
                {
                    //将附件添加到邮件
                    //Stream stream = new MemoryStream(attachment);
                    mailMessage.Attachments.Add(new Attachment(new MemoryStream(attachment), attachmentName));
                }
                if (attachment_2 != null)
                {
                    //将附件添加到邮件
                    //Stream stream = new MemoryStream(attachment);
                    mailMessage.Attachments.Add(new Attachment(new MemoryStream(attachment_2), attachmentName_2));
                }
                //获取或设置此电子邮件的发送通知。
                mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;

                //实例化一个SmtpClient类。
                SmtpClient client = new SmtpClient();
                //设置邮件服务器地址{
                client.Host = Host;
                client.Port = int.Parse(Port);
                //安全加密连接。
                client.EnableSsl = bool.Parse(EnableSSL);
                if (Convert.ToBoolean(IsAnonymous))
                {
                    //不和请求一块发送。
                    client.UseDefaultCredentials = false;
                }
                else
                {
                    //不和请求一块发送。
                    client.UseDefaultCredentials = false;
                    //验证发件人身份(发件人的邮箱,邮箱里的生成授权码);
                    client.Credentials = new NetworkCredential(FromMail, Password);
                }
                //如果发送失败,SMTP 服务器将发送 失败邮件告诉我  
                mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                //发送
                client.Send(mailMessage);
                //MessageBox.Show("SUCCESS");

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

    }
}

public ActionResult CreateInterface(QueryListInt result)
        {
            try
            {
                DataTable dt;
            string ID = "";
            dt = sql_1.getdataset("select top 1 ID from [loTDBWrite_1].[dbo].[IoT_Interface] order by ID desc").Tables[0];

            if (dt.Rows.Count > 0)
            {
                ID = dt.Rows[0][0].ToString();
                
            }
            else
            {
                ID = "0";
            }

            int res = 0;
            QueryListInt re = result;
            string RequiedBy = re.RequiedBy;
            List<DateTime> valueDate = re.valueDate;
            string InterfaceName = re.InterfaceName;
            string InterfaceAcronym = re.InterfaceAcronym;
            string InterfaceOwner = re.InterfaceOwner;
            string InterfaceType = re.InterfaceType;
            string DeptProject = re.DeptProject;
            string RequestType = re.RequestType;
            string ProjectClone = re.ProjectClone;
            string DeptProjectClone = re.DeptProjectClone;
            string Comments = re.Comments;
            DateTime dtnow = DateTime.Now;

            DateTime start;
            DateTime end;
            if (valueDate.Count > 1)
            {
                start = valueDate[0];
                end = valueDate[1];
            }
            else
            {
                start = DateTime.Now.AddHours(0.5);
                end = DateTime.Now;
            }
                
            string MailBody = "<html><body><div style=\"text-align:center;color:#dc3545;font-family:'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans','Liberation Sans',sans-serif,'Apple Color Emoji';\"><h2> MIA - Manufacturing Intelligence Analysis</h2></div><div style=\"text-align:center;font:bold;font-family:'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans','Liberation Sans',sans - serif,'Apple Color Emoji';\">New DataInterface Request</div><hr/><div>Frank Li,</div><br/><div> "+ RequiedBy+" has requested a new datainterface request </div><br/><div><span style = \"font:bold;\"> Request ID </span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span> "+ (Convert.ToInt32(ID) + 1)+" </span></div><div><span style = \"font:bold;\"> Start Date </span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span> "+ start+" </span></div><div><span style = \"font:bold;\"> End Date </span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span> "+end+" </span></div><div><span style = \"font:bold;\"> Request Type </span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span> "+ InterfaceType + " </span></div><div><span style = \"font:bold;\"> Remark </span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span> "+ Comments+ " </span></div><br/><div><a href =\"http://mlxsztvwpapp01/PMTCProduction/interfaceDetails?ID="+(Convert.ToInt32(ID) + 1)+"\"> Please use IoT DataInterface Application to approve/deny this request </a></div><br/><hr/><div>© "+dtnow.Year.ToString()+" - MIA-Manufacturing Intelligence Analysis</ div ></body></html>";

string sendto = "";
            string copyto = ADUser.Email + ";" + "" + ";" + "" + ";" ;
            string subject = "IoT DataInterface Application";
            //string body = "<span style=""color: red"">HTML</span>";
            byte[] attachment = null;
            string attachmentname = "";
            bool issuccess = myWebservice.SendMail("PMC-SZT-DigitalService@molex.com", "", sendto, copyto, subject, MailBody, attachment, attachmentname);
            
            string sqltest = "insert into [loTDBWrite_1].[dbo].[IoT_Interface] ([RequiedBy] ,[StartTime] ,[EndTime] ,[InterfaceName] ,[InterfaceAcronym],[InterfaceOwner] ,[InterfaceType] ,[DeptProject] ,[RequestType] ,[ProjectClone] ,[DeptProjectClone] ,[Comments]) values ('" + RequiedBy + "','" + start + "','" + end + "','" + InterfaceName + "','" + InterfaceAcronym + "','" + InterfaceOwner + "','" + InterfaceType + "','" + DeptProject + "','" + RequestType + "','" + ProjectClone + "', '" + DeptProjectClone + "', '" + Comments + "')";
            
                res = sql.ExecuteNonQuery("insert into [loTDBWrite_1].[dbo].[IoT_Interface] ([RequiedBy] ,[StartTime] ,[EndTime] ,[InterfaceName] ,[InterfaceAcronym],[InterfaceOwner] ,[InterfaceType] ,[DeptProject] ,[RequestType] ,[ProjectClone] ,[DeptProjectClone] ,[Comments],[Status]) values ('" + RequiedBy + "','" + start + "','" + end + "','" + InterfaceName + "','" + InterfaceAcronym + "','" + InterfaceOwner + "','" + InterfaceType + "','" + DeptProject + "','" + RequestType + "','" + ProjectClone + "', '" + DeptProjectClone + "', '" + Comments + "','Waiting on admin')");
                if (res > 0)
                {
                    return Json(new { isSuccess = true }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { isSuccess = false }, JsonRequestBehavior.AllowGet);
                }
            }
            catch
            {
                return Json(new { isSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值