asp.net给邮箱发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;


using System.IO;


namespace Portal_Utility.Email
{
    public class SendEmail
    {


        public bool SendWebEmail(string sToMailUser, string sSubject, string sBody)
        {
            if (sToMailUser == "")
            {
                return false;
            }


            string MailServerPort = "";
            string MailServerHost = "";
          //  string MailServerMailID = "GM";
            string MailServerPassWord = "";


            string sFromUserName = "";//
            string sFromUserDisplayName = "";//显示名称


           
                MailServerPort = "25";//服务端口号
                MailServerHost = "smtp.ym.163.com";
                //MailServerMailID = "GM";
                MailServerPassWord = "cso4games#@!";


                sFromUserName = "[email protected]";
                sFromUserDisplayName = "[email protected]";




            //System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage(MailServerUserName, ToMailID);    对于部分的邮箱这种方式是不行的,不知道为什么
            System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
            myEmail.From = new MailAddress(sFromUserName, sFromUserDisplayName);
            myEmail.To.Add(sToMailUser);
            myEmail.Subject = sSubject;
            myEmail.Body = sBody;
            myEmail.Priority = MailPriority.Normal;
            myEmail.IsBodyHtml = true; //邮件形式,.Text、.Html 


            System.Net.Mail.SmtpClient smtpclient = new SmtpClient();
            smtpclient.Host = MailServerHost;
            smtpclient.Port = Convert.ToInt32(MailServerPort);
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(sFromUserName, MailServerPassWord);
            smtpclient.Timeout = 1000 * 100;
            try
            {
                smtpclient.Send(myEmail);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Second exception.", e.Message);  
                return false;
            }
        }




        public bool SendWebEmailAttachment(string sToMailUser, string sSubject, string sBody, string AttachmentPath)
        {
            string MailServerPort = "25";
            string MailServerHost = "smtp.ym.163.com";
            string MailServerMailID = "CS";
            string MailServerPassWord = "cso4games#@!";


            string sFromUserName = "[email protected]";//邮件地址[email protected] ;密码gm365qaz
            string sFromUserDisplayName = "[email protected]";//显示名称


            


           


            //System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage(MailServerUserName, ToMailID);    对于部分的邮箱这种方式是不行的,不知道为什么
            System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
            myEmail.From = new MailAddress(sFromUserName, sFromUserDisplayName);
            myEmail.To.Add(sToMailUser);
            myEmail.Subject = sSubject;
            myEmail.Body = sBody;
            myEmail.Priority = MailPriority.Normal;
            myEmail.IsBodyHtml = true; //邮件形式,.Text、.Html


            //附件 
            string strFilePath = AttachmentPath;
            System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//添加附件 
            attachment1.Name = System.IO.Path.GetFileName(strFilePath);
            attachment1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
            attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            attachment1.ContentDisposition.Inline = true;
            attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
            string cid = attachment1.ContentId;//关键性的地方,这里得到一个id数值 
            myEmail.Attachments.Add(attachment1);




            System.Net.Mail.SmtpClient smtpclient = new SmtpClient();
            smtpclient.Host = MailServerHost;
            smtpclient.Port = Convert.ToInt32(MailServerPort);
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Credentials = new System.Net.NetworkCredential(MailServerMailID, MailServerPassWord);
            smtpclient.Timeout = 1000 * 100;
            try
            {
                smtpclient.Send(myEmail);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

}

以上部分为发送邮件的一个类,直接建这个类就好了


调用此类中定义该方法的地方

 Portal_Utility.Email.SendEmail sendmail = new Portal_Utility.Email.SendEmail();
            bool falg = false;
            string sEmailInfo = "From:www.o4games.com <br>";
            string time=System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            sEmailInfo += "亲爱的用户您好:您的账号为 :" + Account + "<br>" + " 正在申请修改密码。请点击链接<a href=' http://localhost:5843/CheckUrl.aspx?Semail=" + Semail + "&&Account=" + Account + "&&CurrentTime=" + time + "'>www.o4games.com.</a>" + "<br>" + "进行下一步操作";
                sEmailInfo+="该地址有效期为20分钟,请您在20分钟以内点击该链接。";
            string sEmailTitle = "尊敬的" + Account + "用户,您正在进行修改您的密码,请谨慎操作。";
            falg = sendmail.SendWebEmail(Semail, sEmailTitle, sEmailInfo);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章