邮件发送控件jmail使用方法之WinForm示例

来源:互联网 发布:用while编程n的阶乘 编辑:程序博客网 时间:2024/05/17 05:00

    近期需要写一个自动发送邮件的程序,本想使用SOCKET写,但搜索了一下,网上介绍JMAIL的很多,既然有现成的就拿来用吧,省点精力做其他事情。现将该控件在Winform和asp.net中的使用方法分别小结一下,本文主要阐述Winform中如何使用它。

    1.首先下载jmail控件,然后安装(其实不安装也行,只要你能找到jmail.dll文件,然后注册该dll文件。这里不支持上传附件,否则我就把我下载的传上来了),附上注册它的批处理:

 

echo off
copy jmail.dll C:/windows/system32
regsvr32 jmail.dll
echo 执行完毕!
pause

 

    2.新建项目,在解决方案管理器(solution explorer)中选中项目点击右键,选择添加引用(Add Refrence),在浏览(browers)中选择jmail.dll即可(鄙人用的开发工具是Visual Studio 2008);

    3.主要代码段如下:

 

        private void SendNow_Click(object sender, EventArgs e)
        {
            jmail.Message mymail = new jmail.MessageClass();
            mymail.Charset = "GB2312";
            mymail.From = textBox1.Text; //"daoba@e165.com";
            mymail.FromName = "QinKunming";
            mymail.ReplyTo = "daoba@e165.com";
            mymail.Subject = textBox3.Text; //"test email from jmessage";
            mymail.AddRecipient(textBox2.Text, "eric lv", "123");
            mymail.Body = textBox4.Text; // "jmail 内容";
            mymail.MailServerUserName = textBox7.Text; // "daoba";
            mymail.MailServerPassWord = textBox8.Text; // "123456&";
            if(!string.IsNullOrEmpty(textBox5.Text))
                mymail.AddAttachment(textBox5.Text, true, null);
            mymail.Send(textBox6.Text, false);
            MessageBox.Show("email sent successfully!");
            mymail.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            if (!string.IsNullOrEmpty(openFileDialog1.FileName))
                textBox5.Text = openFileDialog1.FileName;
        }

 

 

 

    实际应用中可以根据自己需要,将上述配置以参数文件单独存放,或在界面上设置手动填充栏,具体怎么做看自己需求啦。Jmail的主要参数列表可以敲出实例加.后自动列出,意思都容易理解,这里就不一一列举了。再说下载的jmail安装后在安装目录有很多例程。