请教如何用Net.Mail发送需要SMTP认证的邮件 比如163等,都需要SMTP认证谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://csharp.xdowns.com/archives/2006/01/21/119.htmlhttp://www.cnblogs.com/stswordman/archive/2006/05/23/407263.html 第2个例子用的是.web.mail我的是2.0,只有.net.mail其中的类下没有这个方法"mail.Fields.Add" 给一个例子:1、在 Microsoft Visual Studio .NET 的“文件”菜单上,单击“新建”,然后单击“项目”。 2、在“新建项目”对话框中,选择“Visual C#项目”,再选择“Windows 应用程序”模板。 3、键入 MailApp 作为该程序的名称,然后单击确定。4、在Form1.cs的视图设计器中,添加如下控件:控件类型 控件名称 控件属性 属性值Button button1 Text 发送邮件Button button2 Text 取消发送 Enable FalseButton 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.comTextBox textBox2 Text [email protected]TextBox textBox3 Text [email protected]TextBox textBox4 Text 发送邮件测试TextBox textBox5 Text cq_lqjTextBox 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) MessageBox.Show(this, string.Format("用户已经取消({0})邮件的发送。",Subject),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); else if (e.Error != null) MessageBox.Show(this, string.Format("({0})邮件的发送时发生: {1}", Subject, e.Error.ToString()), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show(this, string.Format("({0})邮件的发送已经完成。", Subject), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); message.Dispose(); button1.Enabled = true; button2.Enabled = false;}10、编译要想轻松开发软件,详见:http://www.psec.net.cn winform 传值问题 access 写的更新语句,怎么弄都不能更新成功,真的“被access”。。。。。 主键跟标识列一样吗? 关于界面设计和数据库的连接实现 读取Excel中的文本 winform listbox 怎么树形显示 关于按钮的问题 鼠标键盘的禁止操作 wpf 播放器卡住问题 建数据库的时候要不要加约束!!! 下面人的问题大家不要回答 一大堆checkBox,如何用FOR循环来改变他们的Checked值?
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 [email protected]
TextBox textBox3 Text [email protected]
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) MessageBox.Show(this, string.Format("用户已经取消({0})邮件的发送。",Subject),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
else if (e.Error != null)
MessageBox.Show(this, string.Format("({0})邮件的发送时发生: {1}", Subject, e.Error.ToString()), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show(this, string.Format("({0})邮件的发送已经完成。", Subject), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
message.Dispose();
button1.Enabled = true;
button2.Enabled = false;
}
10、编译
要想轻松开发软件,详见:http://www.psec.net.cn