发送邮件
来源:百度文库 编辑:神马文学网 时间:2024/04/28 05:34:09
//创建一个MailMessage对象
MailMessage aMail = new MailMessage();
//收信人地址
aMail.To = txtTo.Text ;
//发信人地址
aMail.From = txtFrom.Text ;
//主题
aMail.Subject = txtSubject.Text;
//正文
aMail.Body = txtBody.Text ;
//优先级
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(@"\temp\" + fileName);
File1.PostedFile.SaveAs (fileName);
//添加附件
aMail.Attachments.Add(new MailAttachment(fileName));
}
string result = "";
try
{
//发送邮件
SmtpMail.Send(aMail);//SmtpMail发送邮件对象
result = "Email 发送成功!";
}
catch(Exception ex)
{
result = "Email 发送失败。" + ex.Message ;
}
Response.Write ("");
}
MailMessage aMail = new MailMessage();
//收信人地址
aMail.To = txtTo.Text ;
//发信人地址
aMail.From = txtFrom.Text ;
//主题
aMail.Subject = txtSubject.Text;
//正文
aMail.Body = txtBody.Text ;
//优先级
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(@"\temp\" + fileName);
File1.PostedFile.SaveAs (fileName);
//添加附件
aMail.Attachments.Add(new MailAttachment(fileName));
}
string result = "";
try
{
//发送邮件
SmtpMail.Send(aMail);//SmtpMail发送邮件对象
result = "Email 发送成功!";
}
catch(Exception ex)
{
result = "Email 发送失败。" + ex.Message ;
}
Response.Write ("");
}