C#,WinForm发送邮件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;


namespace SendMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                

            string [] recs = rec.Text.Split(';');
            bool isyes =  SendMail(server.Text, 25, this.txt_account.Text, recs, this.subject.Text, this.content.Text, true, this.txt_pwd.Text);

              }
            //if (isyes)
            //{
            //    MessageBox.Show("邮件发送成功");
            //}
        }
        NetworkStream ns;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="smtpServer">smtp服务器</param>
        /// <param name="point">发送端口</param>
        /// <param name="_from">发件人</param>
        /// <param name="_tos">收件人</param>
        /// <param name="_subject">主题</param>
        /// <param name="_content">内容</param>
        /// <param name="isESMTP">是否需要身份验证</param>       
        /// <param name="userPWD">发件人密码</param>       
        /// <returns></returns>
        public bool SendMail(string smtpServer, int point, string _from, string[] _tos, string _subject, string _content, bool isESMTP, string userPWD)
        {
            try
            {
                TcpClient smtpClient = new TcpClient();
                IPAddress ip = Dns.GetHostAddresses(smtpServer)[0];
                smtpClient.Connect(ip, point);//连接服务器
                ns = smtpClient.GetStream();
                if (SMTPReceive() != 220)
                    return false;
                Send("HELO/r/n");
                if (SMTPReceive() != 250)
                    return false;
                //身份验证
                if (isESMTP)
                {
                    Send("AUTH LOGIN/r/n");
                    if (SMTPReceive() != 334)
                        return false;
                    string uname=_from.Substring(0,_from.IndexOf('@'));
                    Send(ConvertToBase64(uname) + "/r/n");
                    if (SMTPReceive() != 334)
                        return false;
                    Send(ConvertToBase64(userPWD) + "/r/n");
                    if (SMTPReceive() != 235)
                        return false;
                }

                Send("Mail From:<" + _from + ">/r/n");//发件人
                if (SMTPReceive() != 250)
                    return false;
                if (_tos.Length < 0)
                    return false;
                foreach (string to in _tos)
                {
                    Send("RCPT TO:<" + to + ">/r/n");
                    if (SMTPReceive() != 250)
                        return false;
                }
                Send("DATA /r/n");
                if (SMTPReceive() != 354)
                    return false;
                StringBuilder content = new StringBuilder();             
                content.Append("From:" + _from + "/r/n");
                if (_tos.Length <= 0)
                    return false;
                foreach (string to in _tos)
                {
                    content.AppendFormat("To:{0}/r/n", to);
                }              
                //邮件标题
                content.AppendFormat("Subject:{0}/r/n", _subject);
                //邮件编码格式
                content.AppendFormat("Charset=/"{0}/"/r/n", "gb2312");

                content.Append("/r/n");
                content.Append(_content);//追加发送的内容
                content.Append("/r/n./r/n");
                Send(content.ToString());
                if (SMTPReceive() != 250)
                    return false;
                else
                    return true;
            }
            catch (Exception exc)
            {
                return false;
            }

        }

        /// <summary>
        /// 将string型转换为Base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        string ConvertToBase64(string str)
        {
            byte[] data = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(data);
        }

        /// <summary>
        /// 发送消息至服务器 
        /// </summary>
        /// <param name="msg"></param>
        public void Send(string msg)
        {
            byte[] data = Encoding.UTF8.GetBytes(msg);
            ns.Write(data, 0, data.Length);
            ns.Flush();
        }

        public int SMTPReceive()
        {
            try
            {
                byte[] data = new byte[1024];
                int size = ns.Read(data, 0, 1024);
                string result = Encoding.UTF8.GetString(data, 0, size);
                int index = result.IndexOf(' ');
                if (index == -1)
                    index = result.Length;
                result = result.Substring(0, index);
                return int.Parse(result);
            }
            catch
            {
                return 0;
            }
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值