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 = "cs@o4games.com";
                sFromUserDisplayName = "cs@o4games.com";




            //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 = "cs@o4games.com";//邮件地址gm@webgame.g365.net ;密码gm365qaz
            string sFromUserDisplayName = "cs@o4games.com";//显示名称


            


           


            //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);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值