资源简介

C# 基于SMTP协议和SOCKET发送邮件及附件。 在WIN7的VS2010环境下编译测试OK。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using UtilSp.ClassLib;
using System.Threading;
using System.Linq.expressions;

namespace SmtpTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private Helper helper_ = new Helper();

        private void buttonSend_Click(object sender EventArgs e)
        {
            SmtpSp smtpSp = new SmtpSp();
            SmtpSp.MailInfo mailInfo = new SmtpSp.MailInfo();
            mailInfo.senderAddress_pro = textBoxSender.Text;
            mailInfo.receiverAddresses_pro = new List(textBoxReceivers.Text.Split(‘;‘));
            mailInfo.subject_pro = textBoxSubject.Text;
            mailInfo.content_pro = textBoxContent.Text;
            mailInfo.userName_pro = textBoxUserName.Text;
            mailInfo.password_pro = textBoxPassword.Text;
            if (listBoxAttachment.Items.Count > 0)
            {
                string[] attachments = new string[listBoxAttachment.Items.Count];
                listBoxAttachment.Items.CopyTo(attachments 0);
                mailInfo.attachments_pro = new List(attachments);
            }
            helper_.statusInfo_pro = “Sending...“;
            this.Refresh();
            Thread sendThread = new Thread(() =>
            {
                bool isSendOK = smtpSp.send(textBoxServer.Text 25 mailInfo);
                string sendResult = ““;
                if (isSendOK)
                {
                    sendResult = “Send ok“;
                }
                else
                {
                    string failMessage = “Send Fail!“;
                    if (smtpSp.exception_pro != null)
                    {
                        failMessage += smtpSp.exception_pro.Message;
                    }
                    sendResult = failMessage;
                }
                this.Invoke((Action)(() => { 
                    helper_.statusInfo_pro =sendResult;
                }));
            });
            sendThread.IsBackground = true;
            sendThread.Start();
        }

        private void FormMain_Load(object sender EventArgs e)
        {
            buttonStatus.DataBindings.Add(“Text“ helper_ “statusInfo_pro“);
        }

        private void listBoxAttachment_MouseDown(object sender MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStripAttachment.Show(sender as Control e.X e.Y);
            }
        }

        private void addToolStripMenuItem_Click(object sender EventArgs e)
        {
            string attachmentPath = FileSp.getOpenDialogFileName();
            if (string.IsNullOrEmpty(attachmentPath))
            {

评论

共有 条评论