现在的位置: 首页 > 综合 > 正文

利用GMail和126的SMTP发送邮件

2013年03月28日 ⁄ 综合 ⁄ 共 4693字 ⁄ 字号 评论关闭

以下均测试通过

private readonly string strSmtpServer = "smtp.gmail.com";
        private readonly int IntPort = 587;
        private readonly string strMail = "service@cloud.com";
        private readonly string strName = "Cloud By Yi";
        private readonly string strUserName = "service@cloud.com";
        private readonly string strPassword = "service";

        //private readonly string strSmtpServer = "smtp.126.com";
        //private readonly int IntPort = 587;
        //private readonly string strMail = "laoyi@126.com";
        //private readonly string strUserName = "laoyi";
        //private readonly string strPassword = "888888";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendGMail("laoyi@126.com", "service@cloud.com", "", "邮件标题测试", "邮件正文测试");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void SendGMail(string SendTo, string CC, string BCC, string Subject, string Body)
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(strMail, strName); // 发件人邮箱地址和显示名称
            if (SendTo != "")
                message.To.Add(SendTo);// 收件人地址,可以设置多个
            if (CC != "")
                message.CC.Add(CC);
            if (BCC != "")
                message.Bcc.Add(BCC);

            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = true; // 设置邮件正文是否为 html 格式的值
            message.BodyEncoding = System.Text.Encoding.UTF8;  // 设置邮件正文编码
            message.Priority = MailPriority.Normal; // 设置电子邮件的优先级

            // 包含附件
            //string attachPath = "附件地址";
            //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachPath);
            //attachment.Name = System.IO.Path.GetFileName(attachPath); // 附件名称
            //attachment.NameEncoding = System.Text.Encoding.GetEncoding("gb2312"); // 附件名称的编码
            //attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; // 设置附件的编码
            //attachment.ContentDisposition.Inline = true;
            //attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
            //string cid = attachment.ContentId; // 关键性的地方,这里得到一个id数值
            //message.Attachments.Add(attachment); // 可以添加多个附件

            SmtpClient client = new SmtpClient();
            client.Host = strSmtpServer; // 设置 smtp 事务的主机名称或 IP 地址
            client.Port = IntPort; // 端口号           
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true; // 经过ssl加密,gmail 邮箱必须设置为 ture
            client.Credentials = new System.Net.NetworkCredential(strUserName, strPassword); // Your mail address & password
            try
            {
                client.Send(message);
                MessageBox.Show("邮件发送到" + message.To.ToString());
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + "<br>" + ee.InnerException.Message);
            }

        }

        private void Send126Mail()
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(strMail, "Cloud By Yi"); // 发件人邮箱地址和显示名称
            message.To.Add(new MailAddress("laoyi@qq.com")); // 收件人地址,可以设置多个
            message.CC.Add("service@cloud.com,laoyi@126.com");

            message.Subject = "邮件标题测试";
            message.Body = "邮件正文测试";
            message.IsBodyHtml = false; // 设置邮件正文是否为 html 格式的值
            message.BodyEncoding = System.Text.Encoding.UTF8;  // 设置邮件正文编码
            message.Priority = MailPriority.Normal; // 设置电子邮件的优先级

            // 包含附件
            //string attachPath = "附件地址";
            //System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(attachPath);
            //attachment.Name = System.IO.Path.GetFileName(attachPath); // 附件名称
            //attachment.NameEncoding = System.Text.Encoding.GetEncoding("gb2312"); // 附件名称的编码
            //attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; // 设置附件的编码
            //attachment.ContentDisposition.Inline = true;
            //attachment.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
            //string cid = attachment.ContentId; // 关键性的地方,这里得到一个id数值
            //message.Attachments.Add(attachment); // 可以添加多个附件

            SmtpClient client = new SmtpClient();
            client.Host = strSmtpServer; // 设置 smtp 事务的主机名称或 IP 地址
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential(strUserName, strPassword); // Your mail address & password
            try
            {
                client.Send(message);
                MessageBox.Show("邮件发送到" + message.To.ToString());
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + "<br>" + ee.InnerException.Message);
            }

        }

【上篇】
【下篇】

抱歉!评论已关闭.