
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace SendMail
{
public partial class FormMain : Form
{
//用于发送邮件的线程
Thread threadSending = null;
//代表邮件的 CDO.Message对象
CDO.Message oMsg = null;
//邮件发送流
ADODB.Stream msgStream = null;
public FormMain()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button_Send_Click(object sender, EventArgs e)
{
if (this.threadSending != null && this.threadSending.IsAlive)
{
this.label_Info.ForeColor = Color.Red;
this.label_Info.Text = "目前正有一邮件发送中";
return;
}
else
{
ThreadStart ts = new ThreadStart(this.SendMail);
this.threadSending = new Thread(ts);
this.threadSending.Start();
}
}
private void SendMail()
{
this.label_Info.ForeColor = Color.Blue;
this.label_Info.Text = "正发送邮件“ " + this.textBox_Subject.Text + "”";
try
{
this.oMsg = new CDO.Message();
this.oMsg.From = this.textBox_Form.Text;
this.oMsg.To = this.textBox_To.Text;
this.oMsg.Subject = this.textBox_Subject.Text;
this.oMsg.HTMLBody = "<html><body>" + this.richTextBox_MessageBody.Text + "</body></html>";
//添加附件
string file = this.textBox_Attachment.Text.Trim();
if (!String.IsNullOrEmpty(file))
{
if (System.IO.File.Exists(file))
{
this.oMsg.AddAttachment(file, "", "");
}
else
{
this.label_Info.ForeColor = Color.Red;
this.label_Info.Text = "添加的附件不存在,发送失败!";
return;
}
}
CDO.IConfiguration iConfg = this.oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;
oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "32mid@zjjzx.mail"; //修改这里,并保持发件人与这里的地址一致
oFields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "32mid@zjjzx.mail"; //修改这里
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "32mid";//修改这里
oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "32mid";//修改这里
oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
oFields["http://schemas.microsoft.com/cdo/configuration/languagecode"].Value = 0x0804;
oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "192.168.0.111";
oFields.Update();
this.oMsg.BodyPart.Charset = "gb2312";
this.oMsg.HTMLBodyPart.Charset = "gb2312";
this.msgStream = this.oMsg.GetStream();
this.timer_Sending.Start();
this.oMsg.Send();
this.timer_Sending.Stop();
}
catch (Exception ex)
{
this.label_Info.ForeColor = Color.Red;
this.label_Info.Text = "发送邮件“ " + this.textBox_Subject.Text + "” 失败:\n" + ex.Message;
return;
}
this.label_Info.ForeColor = Color.Green;
this.label_Info.Text = "已将邮件“ " + this.textBox_Subject.Text + " ” 发送至:" + this.textBox_To.Text;
oMsg = null;
}
private void linkLabel_View_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox_Attachment.Text = this.openFileDialog1.FileName;
}
}
private void linkLabel_AttachmentCanel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.textBox_Attachment.Clear();
}
//监视发送邮件的进度
private void timer_Sending_Tick(object sender, EventArgs e)
{
this.label_Info.ForeColor = Color.Blue;
this.label_Info.Text = "正发送邮件“ " + this.textBox_Subject.Text + "”";
if (this.msgStream != null)
{
//不正确的方法
//this.label_Info.Text += this.msgStream.Position + "/" + this.msgStream.Size;
}
}
}
}
(这是我刚从网上找到的,还没来得急仔细琢磨,但发现能实现发邮件,用foxmail收到。不会发生乱码的问题,你可要仔细瞧勒!)
浙公网安备 33010602011771号