All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP+MYSQL建设网站程序需要注意两点_php资料_编程技术
PHP初学:实例详细学习PHP的简单语法_php资料_编程技术
ASP.NET,2.0服务器控件之复合控件样式_[Asp.Net教程]
Sql联合查询_mssql学习_编程技术
ASP.NET实现投票结果的图片进度条显示代码_[Asp.Net教程]
使用ASP.NET,AJAX的注意事项_[Asp.Net教程]
用asp.net把Excel转换为SQL,Server_[Asp.Net教程]
如何将索引碎片数量降至最低_[SQL Server教程]
在gridview中使用DataFromatString的小技巧_[Asp.Net教程]
PHP技巧:php过滤危险html代码_[PHP教程]
Reading number is top 10 pictures
青春清纯美女大集合3
青春清纯美女大集合2
网上疯传的一篇小学作文《爸爸也治不了妈妈》
Summer is most suitable for young people to travel in China5
Take you to walk into the most true north Korea rural2
In the world the most mysterious 21 place landscape4
乳娘帕梅拉安德森4
鸡也看毛片
More attractive than sora aoi4
Parking technology is great, that give you the keys can't stolen
Download software ranking
Boxer vs Yellow3
c#程序设计案例教程
Visual C++界面编程技术
The Bermuda triangle1
Boxer Classic video1
Boxer's Top ten classic battle5
株洲本地在线棋牌游戏
Boxer's Top ten classic battle1
Rio big adventure
Sora aoi, the nurse, uniform ,nursing assistant
qq published in(发表于) 2014/7/11 9:21:00 Edit(编辑)
C#教程: 电子邮件的发送使用实例

C#教程: 电子邮件的发送使用实例

C#教程: 电子邮件的发送使用实例|方法

电子邮件的发送

本实例通过使用SMTP协议,实现发送电子邮件功能。实例运行结果如图1和图2所示。



图1 “发送邮件”窗体



图2 提示信息

服务器端实现的具体步骤如下所示。

(1)在VS2005工程中添加一个窗体。

(2)在窗体中添加3个TextBox文本框,分别用于获取发件人邮箱、收件人邮箱以及主题。

(3)在窗体中添加一个Button按钮和3个Label标签,它们分别用于激发事件和显示信息。

(4)在窗体中添加一个richTextBox控件,用于书写内容。

(5)程序主要代码。

首先添加必要的命名空间,代码如下:

using System.Net.Mail;

using System.IO;

using System.Net;

自定义一个CreateTimeoutTestMessage方法,该方法中通过MailMessage类与SmtpClient类实例化Mail对象与Client对象,然后使用Mail对象来完成对邮件信息的定制,如收件人、主题和内容等, 最后调用Client对象的Send方法来完成信息的发送功能。CreateTimeoutTest Message方法关键代码如下:

public void CreateTimeoutTestMessage(string server)

{

try

{

Mail = new MailMessage(this.txtFrom.Text, this.txtGet.Text);

Mail.Subject =this.txtSubject.Text.Trim().ToString();

Mail.Body = this.richTextBox1.Text.Trim().ToString();

Client = new SmtpClient(server, 25);

Client.Send(Mail);

MessageBox.Show("Success!!!");

}

catch (Exception ey)

{

MessageBox.Show(ey.Message);

}

}

单击【发送】按钮时,程序调用CreateTimeoutTestMessage方法完成邮件发送功能。【发送】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

if (this.richTextBox1.Text.Trim().ToString() != "")

{

CreateTimeoutTestMessage("hywork");

}

else

{

MessageBox.Show("you can not write info");

return;

}

}

注意:MailMessage类还提供群发功能以及发送附件(通过Attachments类)的功能。

完整程序代码如下:

★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Net.Sockets;

using System.IO;

namespace _6_06

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

}

TcpClient server;

string sendstring;

byte[] bufferstring;

NetworkStream networkstream;

StreamReader streamreader;

private void button2_Click(object sender, EventArgs e)

{

server = new TcpClient(this.textBox1.Text, 110);

networkstream = server.GetStream();

streamreader = new StreamReader(networkstream);

try

{

sendstring = "USER "+this.textBox2.Text+"rn";

bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());

networkstream.Write(bufferstring, 0, bufferstring.Length);

richTextBox1.AppendText(streamreader.ReadLine() + "rn");

sendstring = "PASS " + this.textBox3.Text + "rn";

bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());

networkstream.Write(bufferstring, 0, bufferstring.Length);

richTextBox1.AppendText(streamreader.ReadLine() + "rn");

sendstring = "STAT " +"rn";

bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());

networkstream.Write(bufferstring, 0, bufferstring.Length);

string strResult=streamreader.ReadLine();

if (strResult.IndexOf('-') == -1)

{

richTextBox1.AppendText(strResult + "rn");

sendstring = "LIST " + "rn";

bufferstring = Encoding.GetEncoding("gb2312").GetBytes(sendstring.ToCharArray());

networkstream.Write(bufferstring, 0, bufferstring.Length);

string strInfo = streamreader.ReadLine();

string[] str = strInfo.Split(' ');

richTextBox1.AppendText("邮件数量:" + str[1] + "rn");



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.