开发电子邮件应用程序示例 代码

开发电子邮件应用程序示例
开发前需要引入一个dll文件System.Web.Mail

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Web;
using  System.Web.Mail;


namespace  SendMail
{
        
/// <summary>
        
/// Summary description for frmSendMail.
        
/// </summary>

        public class frmSendMail : System.Windows.Forms.Form
        
{
                
private System.Windows.Forms.Button btnRemove;
                
private System.Windows.Forms.ListBox lstFiles;
                
private System.Windows.Forms.Label lblSMTP;
                
private System.Windows.Forms.Label lblSubject;
                
private System.Windows.Forms.Label lblBcc;
                
private System.Windows.Forms.Label lblCc;
                
private System.Windows.Forms.Label lblTo;
                
private System.Windows.Forms.Label lblFrom;
                
private System.Windows.Forms.TextBox txtSubject;
                
private System.Windows.Forms.TextBox txtBcc;
                
private System.Windows.Forms.TextBox txtCc;
                
private System.Windows.Forms.TextBox txtTo;
                
private System.Windows.Forms.TextBox txtFrom;
                
private System.Windows.Forms.TextBox txtIP;
                
private System.Windows.Forms.Button txtAttach;
                
private System.Windows.Forms.Button btnSend;
                
private System.Windows.Forms.RichTextBox txtBody;
                
private System.Windows.Forms.OpenFileDialog ofdAttach;
                
/// <summary>
                
/// Required designer variable.
                
/// </summary>

                private System.ComponentModel.Container components = null;
                
                
//Create a message object to contain a complete email.
                private MailMessage message;
                
                
                
public frmSendMail()
                
{
                        
//
                        
// Required for Windows Form Designer support
                        
//
                        InitializeComponent();

                        
//
                        
// TODO: Add any constructor code after InitializeComponent call
                        
//
                        
                        
//Initialize the message object
                        message=new MailMessage();
                }


                
/// <summary>
                
/// Clean up any resources being used.
                
/// </summary>

                protected override void Dispose( bool disposing )
                
{
                        
if( disposing )
                        
{
                                
if (components != null
                                
{
                                        components.Dispose();
                                }

                        }

                        
base.Dispose( disposing );
                }


                
Windows Form Designer generated code

                
/// <summary>
                
/// The main entry point for the application.
                
/// </summary>

                [STAThread]
                
static void Main() 
                
{
                        Application.Run(
new frmSendMail());
                }

                
//Compose the email and then send it
                private void btnSend_Click(object sender, System.EventArgs e)
                
{
                        
//Check if SMTP Server IP address is specified.
                        
//If yes, compose email
                        
//else display appropriate message
                        if (txtIP.Text.Length >0
                        
{
                                
//Specify the SMTP server to use 
                                
//to send emails
                                
                                SmtpMail.SmtpServer 
=txtIP.Text.Trim() ;
                                
                                
//Compose email
                                message.From =txtFrom.Text ;
                                message.To 
=txtTo.Text ;
                                message.Cc 
=txtCc.Text ;
                                message.Bcc 
=txtBcc.Text ;
                                message.Subject 
=txtSubject.Text ;
                                message.Body
=txtBody.Text;

                                
//Add attachements to the email if any
                                message.Attachments.Clear();                
                                
for(int index=0;index<lstFiles.Items.Count;index++)
                                
{
                                        MailAttachment mAttach
=new MailAttachment(lstFiles.Items[index].ToString());
                                        message.Attachments.Add(mAttach);
                                }

                                                
                                
try
                                
{
                                        MessageBox.Show (message.From.ToString());
                    MessageBox.Show (message.To.ToString());
                                        MessageBox.Show (message.Subject.ToString());
                                        
                                        
//Send the composed email
                                SmtpMail.Send(message.From,message.To,message.Subject,message.Body);

                                        
//Reset the form
                                        txtFrom.Text="";
                                        txtTo.Text
="";
                                        txtCc.Text
="";
                                        txtBcc.Text
="";
                                        txtSubject.Text
="";
                                        txtBody.Text
="";
                                        lstFiles.Items.Clear();
                                }

                                
catch (Exception excep)
                                
{
                                        MessageBox.Show(excep.Message.ToString() );
                                        MessageBox.Show(excep.StackTrace.ToString() );
                                        
//Display appropriate message if
                                        
//From address is invalid
                                        
//MessageBox.Show("Enter a valid From email address");
                                        return;
                                }

                                
//Visually indicate that the email was sent
                                MessageBox.Show("您的邮件已发送");
                        }

                        
else
                        
{
                                MessageBox.Show(
"请输入 SMTP 服务器 IP 地址");

                        }

                }


                

                
private void txtAttach_Click(object sender, System.EventArgs e)
                
{
                        
//Display a dialog box to select
                        
//files to attach to the email.
                        
//Once selected, display the selected 
                        
//files in a listbox
                        if(ofdAttach.ShowDialog()==DialogResult.OK)
                        
{
                                
string [] files=new string[ofdAttach.FileNames.Length];
                                
for(int index=0;index<ofdAttach.FileNames.Length;index++)
                                
{
                                        lstFiles.Items.Add(ofdAttach.FileNames[index].ToString());
                                }

            }
                        
                }


                
private void btnRemove_Click(object sender, System.EventArgs e)
                
{
                        
//Remove the selected attachment from the list
                        lstFiles.Items.RemoveAt(lstFiles.SelectedIndex);
                }


        
private void frmSendMail_Load(object sender, EventArgs e)
        
{

        }

        }


}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值