ASP.NET 发送邮件



#region SendMails
        /// <summary> ///根据传入的内容发送邮件。MessageTo接收者。若多个可用";"隔开.MessageSubject邮件主题。MessageBody邮件内容 /// </summary>
        public static bool SendMails(string MessageTo, string MessageCC, string MessageSubject, string MessageBody)
        {  
//from@vdis.cn servicetransfer@vdis.cn
            string sFrom = "jihao.pan@soyisoft.com"; //邮箱
            string spass = "123456"; //密码
            try
            {

                if (MessageTo != "")
                {
                    string[] mailtos = MessageTo.Split(';');
                    string MessageToMain = MessageTo;
                    if (mailtos.Length > 1)
                    {
                        MessageToMain = mailtos[0];
                    }
                    MailAddress from = new MailAddress(sFrom);
                    MailAddress to = new MailAddress(MessageToMain);
                    MailMessage message = new MailMessage(from, to);
                    //多个主送
                    for (int i = 1; i < mailtos.Length; i++)
                    {
                        message.To.Add(new MailAddress(mailtos[i]));
                    }
                    //多个抄送
                    if (MessageCC != "")
                    {
                        string[] mailccs = MessageCC.Split(';');
                        foreach (string mailcc in mailccs)
                        {
                            message.CC.Add(new MailAddress(mailcc));
                        }
                    }
                    message.Subject = MessageSubject; //发送邮件的标题 
                    message.Body = MessageBody; //发送邮件的内容
                    if (MessageSubject.IndexOf("重要") > -1) //只有重要的才加上优先级别。
                    {
                        message.Priority = MailPriority.High; //发送邮件的优先等级
                    }

                    message.IsBodyHtml = true;

                    //string strSC = "smtp." + sFrom.Substring(sFrom.IndexOf("@") + 1);
                    string strSC = "smtp.ee-post.com";
                    SmtpClient client = new SmtpClient(strSC);
                    client.UseDefaultCredentials = false;
                    client.Port = 25;
                    client.Credentials = new NetworkCredential(sFrom, spass);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;

                    client.Send(message);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (InvalidOperationException io)
            {
                throw io;
            }
            catch (SmtpException se)
            {

                throw se;
            }
        }
        /// <summary> ///根据传入的内容发送邮件。MessageTo接收者。若多个可用";"隔开.MessageSubject邮件主题。MessageBody邮件内容 /// </summary>
        public static bool SendMails(string sFrom, string spass, string MessageTo, string MessageCC, string MessageSubject, string MessageBody)
        {
            try
            {

                if (MessageTo != "")
                {
                    string[] mailtos = MessageTo.Split(';');
                    string MessageToMain = MessageTo;
                    if (mailtos.Length > 1)
                    {
                        MessageToMain = mailtos[0];
                    }
                    MailAddress from = new MailAddress(sFrom);
                    MailAddress to = new MailAddress(MessageToMain);
                    MailMessage message = new MailMessage(from, to);
                    //多个主送
                    for (int i = 1; i < mailtos.Length; i++)
                    {
                        message.To.Add(new MailAddress(mailtos[i]));
                    }
                    //多个抄送
                    if (MessageCC != "")
                    {
                        string[] mailccs = MessageCC.Split(';');
                        foreach (string mailcc in mailccs)
                        {
                            message.CC.Add(new MailAddress(mailcc));
                        }
                    }
                    message.Subject = MessageSubject; //发送邮件的标题 
                    message.Body = MessageBody; //发送邮件的内容
                    if (MessageSubject.IndexOf("重要") > -1) //只有重要的才加上优先级别。
                    {
                        message.Priority = MailPriority.High; //发送邮件的优先等级
                    }

                    message.IsBodyHtml = true;

                    //string strSC = "smtp." + sFrom.Substring(sFrom.IndexOf("@") + 1);
                    string strSC = "mailgot.it.volvo.com";
                    SmtpClient client = new SmtpClient(strSC);
                    client.UseDefaultCredentials = true;
                    client.Port = 25;
                    //client.Credentials = new NetworkCredential("M003489", "dell6000");
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;

                    client.Send(message);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (InvalidOperationException io)
            {
                throw io;
            }
            catch (SmtpException se)
            {
                throw se;
            }
        }
        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值