private
void
btnSend_Click(
object
sender, System.EventArgs e)
{
// 创建一个MailMessage对象
MailMessage aMail = new MailMessage ();
// 收信人地址
aMail.To = txtTo.Text;
// 发信人地址
aMail.From = txtFrom.Text;
// 主题
aMail.Subject = txtSubject.Text;
// 正文
aMail.Body = txtBody.Text;
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " ); // 设置服务器验证
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendusername " , " songhuan1229@163.com " ); // 设置用户名
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , " 530520 " ); // 设置用户密码
// 传先级别
aMail.Priority = (MailPriority)ddPriority.SelectedIndex;
// 正文格式
aMail.BodyFormat = (MailFormat)ddBodyFormat.SelectedIndex;
ddPriority.SelectedIndex = 1 ;
ddBodyFormat.SelectedIndex = 0 ;
if (File1.PostedFile.FileName != " " )
{
// 上传文件的文件名(含完整路径)
String fileName = File1.PostedFile.FileName;
fileName = fileName.Substring(fileName.LastIndexOf( @" " ));
// 使用SaveAs方法,将文件保存在项目路径/temp 下,
// 需要在项目路径下创建TEMP 目录
fileName = Server.MapPath( @" emp " + fileName);
File1.PostedFile.SaveAs(fileName);
// 添加附件
aMail.Attachments.Add( new MailAttachment(fileName));
}
string result = "" ;
try
{
// 发送邮件
SmtpMail.SmtpServer = " smtp.163.com " ;
SmtpMail.Send(aMail);
result = " Email 发送成功! " ;
}
catch (Exception ex)
{
result = " Email 发送失败. " + ex.Message;
}
Response.Write( "" );
}
{
// 创建一个MailMessage对象
MailMessage aMail = new MailMessage ();
// 收信人地址
aMail.To = txtTo.Text;
// 发信人地址
aMail.From = txtFrom.Text;
// 主题
aMail.Subject = txtSubject.Text;
// 正文
aMail.Body = txtBody.Text;
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " ); // 设置服务器验证
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendusername " , " songhuan1229@163.com " ); // 设置用户名
aMail.Fields.Add( " http://schemas.microsoft.com/cdo/configuration/sendpassword " , " 530520 " ); // 设置用户密码
// 传先级别
aMail.Priority = (MailPriority)ddPriority.SelectedIndex;
// 正文格式
aMail.BodyFormat = (MailFormat)ddBodyFormat.SelectedIndex;
ddPriority.SelectedIndex = 1 ;
ddBodyFormat.SelectedIndex = 0 ;
if (File1.PostedFile.FileName != " " )
{
// 上传文件的文件名(含完整路径)
String fileName = File1.PostedFile.FileName;
fileName = fileName.Substring(fileName.LastIndexOf( @" " ));
// 使用SaveAs方法,将文件保存在项目路径/temp 下,
// 需要在项目路径下创建TEMP 目录
fileName = Server.MapPath( @" emp " + fileName);
File1.PostedFile.SaveAs(fileName);
// 添加附件
aMail.Attachments.Add( new MailAttachment(fileName));
}
string result = "" ;
try
{
// 发送邮件
SmtpMail.SmtpServer = " smtp.163.com " ;
SmtpMail.Send(aMail);
result = " Email 发送成功! " ;
}
catch (Exception ex)
{
result = " Email 发送失败. " + ex.Message;
}
Response.Write( "" );
}