日期:2014-05-19  浏览次数:21071 次

请教如何用Net.Mail发送需要SMTP认证的邮件
比如163等,都需要SMTP认证
谢谢

------解决方案--------------------
http://csharp.xdowns.com/archives/2006/01/21/119.html
http://www.cnblogs.com/stswordman/archive/2006/05/23/407263.html

------解决方案--------------------
给一个例子:
1、在 Microsoft Visual Studio .NET 的“文件”菜单上,单击“新建”,然后单击“项目”。
2、在“新建项目”对话框中,选择“Visual C#项目”,再选择“Windows 应用程序”模板。
3、键入 MailApp 作为该程序的名称,然后单击确定。
4、在Form1.cs的视图设计器中,添加如下控件:
控件类型 控件名称  控件属性 属性值
Button button1 Text 发送邮件
Button button2 Text 取消发送
Enable False
Button button3 Text 浏览
Label label1 Text SMTP服务器:
Label label2 Text 发送邮箱:
Label label3 Text 接收邮箱:
Label label4 Text 主题:
Label label5 Text 用户名:
Label label6 Text 密码:
Label label7 Text 发送人:
Label label8 Text 收件人:
Label label9 Text 邮件内容
Label label10 Text 附件:
TextBox textBox1 Text smtp.163.com
TextBox textBox2 Text cq_lqj@163.com
TextBox textBox3 Text psec1@163.com
TextBox textBox4 Text 发送邮件测试
TextBox textBox5 Text cq_lqj
TextBox textBox6 Text (略)
TextBox textBox7 Text 鲁勤俭
TextBox textBox8 Text 某小姐
TextBox textBox9 Text
RichTextBox richTextBox1 Text 用NET的类发送邮件测试。
5、在Form1.cs的代码设计器中,引用添加如下代码
using System.Net;
using System.Net.Mail;
6、在Form1.cs的视图设计器中,选中button1,在属性框中选中事件,双击Click,在Form1.cs的代码设计器中,添加修改如下代码
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
client = new SmtpClient(textBox1.Text, 25);//发送邮箱的Smtp服务器和端口
client.Credentials = new NetworkCredential(textBox5.Text, textBox6.Text);//发送邮箱的用户名称和密码
MailAddress from = new MailAddress(textBox2.Text, textBox7.Text, Encoding.UTF8);//发送邮箱(包括邮件地址、发送人和字符编码)
MailAddress to = new MailAddress(textBox3.Text, textBox8.Text, Encoding.UTF8);//接收邮箱包括邮件地址、收件人和字符编码)
message = new MailMessage(from, to);//电子邮件
message.Subject = textBox4.Text;//邮件的主题
message.SubjectEncoding = Encoding.UTF8;
message.Body = richTextBox1.Text;//邮件的正文
message.BodyEncoding = Encoding.UTF8;
if (textBox9.Text!= " ") message.Attachments.Add(new Attachment(textBox9.Text));//附件(可以添加多个附件)
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);//邮件异步发送完成时事件
client.SendAsync(message, message.Subject);//异步发送邮件,并将邮件主题传送给发送完成事件
}
7、在Form1.cs的视图设计器中,选中button2,在属性框中选中事件,双击Click,在Form1.cs的代码设计器中,添加修改如下代码
private void button2_Click(object sender, EventArgs e)
{
client.SendAsyncCancel();//取消发送邮件
}
8、在Form1.cs的视图设计器中,选中button3,在属性框中选中事件,双击Click,在Form1.cs的代码设计器中,添加修改如下代码
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
if (OpenFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox9.Text=OpenFileDialog1.FileName;
}
}
9、在Form1.cs的代码设计器中,添加修改如下代码
private SmtpClient client;
private MailMessage message;

private void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
String Subject = (string)e.UserState;//邮件主题
if (e.Cancelled) Mes