-
实现Email传送
using System.Web.Mail;
using System.IO;
private void btnSend_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//分别取得邮件的收信人的地址、发信人的地址、抄送、主题、内容等信息
string strTo = tbTo.Text;
string strFrom = tbFrom.Text;
string strPwd = tbPwd.Text;
string strCopyTo = tbCopyTo.Text;
string strSubject = tbSubject.Text;
string strBody = tbBody.Text;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MailMessage ms=new MailMessage();
ms.To = strTo; //收信人的地址
ms.From = strFrom; //发信人的地址
ms.Cc = strCopyTo; //抄送
ms.Subject = strSubject; //主题
ms.BodyFormat = MailFormat.Html; //正文格式html/text
ms.Body = strBody; //正文
string strPathOfAttachFile = ""; //初始化附件
//如果有附件则上传
HttpPostedFile hpPFile = AttachFile.PostedFile; //获得上传文件的访问
if(hpPFile.FileName != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//有附件,则上传到Temp目录中
//判断是否存在Temp目录,若无,则创建
string FolderName = Server.MapPath(".") + "\\Temp";
if(Directory.Exists(FolderName) == false)
Directory.CreateDirectory(FolderName);
//取得文件名(不含路径)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] separator =
{'\\'}; //separator的值为"\"
string[] AFileName = hpPFile.FileName.Split(separator);
string strFileName = AFileName[AFileName.Length-1];
strPathOfAttachFile = Server.MapPath(".")+"\\Temp\\"+strFileName;
hpPFile.SaveAs(strPathOfAttachFile);
//添加附件
ms.Attachments.Add(new MailAttachment(strPathOfAttachFile));
}
//从发信人的地址计算出邮件服务器
string[] strTemp = strFrom.Split('@');
string strPartOfSmtpServer = strTemp[strTemp.Length-1];
string strSmtpServer = "smtp." + strPartOfSmtpServer;
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//value=0 No Check value=1 Basic Check Value=2 Exchage Check
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strFrom); //发信人的邮箱地址
ms.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPwd);//验证信息
SmtpMail.SmtpServer=strSmtpServer; //邮件服务器ip或域名
SmtpMail.Send(ms); //发送
//清除控件中的内容
tbTo.Text = "";
tbCopyTo.Text = "";
tbSubject.Text = "";
tbBody.Text = "";
//删除Temp目录中的附件
if(File.Exists(strPathOfAttachFile) == true)
File.Delete(strPathOfAttachFile);
//确认邮件发送成功
string strScript = "<script>alert('邮件发送成功!')</script>";
if (! Page.IsStartupScriptRegistered("Alert"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.RegisterStartupScript("Alert", strScript);
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strScript = "<script>alert('邮件发送失败!')</script>";
if (! Page.IsStartupScriptRegistered("Alert"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.RegisterStartupScript("Alert", strScript);
}
}
}
-
相关阅读:
AX88772B 驱动移植
USB 驱动之 usb_register 函数解析
am335x USB 驱动框架记录
warning: assignment from incompatible pointer type [enabled by default]
WARNING: arch/arm/mach-omap2/built-in.o(.text+0x12cdc): Section mismatch in reference from the function mmc0_init() to the (unknown reference) .init.data:(unknown)
Oracle11g客户端client的下载与安装
sqlserver2008R2 评估期已过
SQL2008 提示评估期已过的解决方法
Windows下Oracle定时备份(全量备份)
某客的《微信小程序》从基础到实战视频教程
-
原文地址:https://www.cnblogs.com/xiaodi/p/121621.html
Copyright © 2020-2023
润新知