电子邮件的发送

 

1SmtpMail.Send(from,to,subject,message)

2MailMessage类(SvstemWebMail内)

MailMessage类中包含了与电子邮件各部分一一对应的属性,  因此通过使用MailMessage类就可以整合一封复杂的邮件,下面列出了MailMessage类中的常用属性:

From:邮件发送者的邮件地址,即寄信人地址。

To:邮件接收者的邮件地址,即收传人地址。

Cc:邮件抄送者的地址

Bcc:邮件暗抄送地址。

Subject:邮件主题。

Body:邮件内容。

BodyEncoding:邮件内容的编码方式。

BodyFormail:邮件内容的格式。可以设定两种格式:Html与Text

Priority:邮件的优先度,可以设定三种优先度:High,Normal,Low。

例://创建MailMessage对象

MailMessage MyMsg=new MailMessage();

MyMsg.From=tbFrom.Text;

MyMsg.To=tbTo.Text;

MyMsg.Cc=tbCc.Text;

MyMsg..Bcc=tbBcc.Text;

MyMsg.Subject=tbSubject.Text;

MyMsg.Prority=(MailPrority)ddlPriority.SelectedIndex;

MyMsg.BodyFormat=(MailFormat)ddlBodyFormat.SelectedIndex;

MyMsg.Body=tbBody.Text;

try

{

  SmtpMail.Send(MyMsg);

  lblShowMsg.Text="发送成功";

  tbTo.Text="";

  ……

}

catch(Exception ee)

{

  lblShowMsg.Text="发送失败:"+ee.ToString();

}

3、发送带有附件的电子邮件

ASP.Net发送电子邮件附件的步骤如下:

在发送邮件附件时,首先要做的事情就是上传附件文件,所以还必须设置HtmlForm控件的Enctype属性,将其值设置为multipart/form-data,这样才能保证附件文件能够上传。

    (1)将文件上传至跟务器。

    (2)创建MailAttachment类实例,引用刚刚上传的文件。

    (3)将刚才创建的MailAttachment类实例通过Add方法添加至MailMessage类

Attachments属性,最后与MailMessage一并发送。

例:在以上代码的try前加入:

//如果有附件则上传

HttpPostedFile hpfFile=AttachFile.PostedFile;

1f(hPfFile.FileName!="")

{

//有附件.则上传到Temp目录中

//取得文件名(不合路径)

char[] de="{'\\'};

string[]  AFilename    hPfFile.FileName.Split(de);

string strFilename=AFilename[AFilename.Length-1];

string strPath=Server.MapPath(".")+"\\Temp\\"+strFilename;

hPfFile.SaveAs(strPath);

//添加附件

MyMsg.Attachments.Add(new MailAttachment(strPath));

}

posted @ 2005-06-11 13:52  虚空境界  Views(785)  Comments(2)    收藏  举报