近期需要寫一個自動發送郵件的程序,本想使用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; //"[email protected]";
mymail.FromName = "QinKunming";
mymail.ReplyTo = "[email protected]";
mymail.Subject = textBox3.Text; //"test email from jmessage";
mymail.AddRecipient(textBox2.Text, "eric lv", "123");
mymail.Body = textBox4.Text; // "jmail 內容";
mymail.MailServerUserName = textBox7.Text; // "******";
mymail.MailServerPassWord = textBox8.Text; // "******";
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安裝後在安裝目錄有很多例程。