您的位置:首页 > 编程语言 > ASP

ASP.NET发送邮件代码

2011-03-08 17:03 603 查看
第一种方法:

private bool SendMail(string fromMail, string toMail, string ccMail, string bccMail, string subject, string body, string sendMode)
{

try
{
fromMail.Text = "yangtao1828@163.com"; //发送方邮件
MailMessage myMail = new MailMessage();
myMail.From = fromMail;
myMail.To = fromMail;
myMail.Cc = ccMail;
myMail.Bcc = bccMail;
myMail.Subject = subject;
myMail.Body = body;
myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html;

//附件
string ServerFileName = "";
if (this.upfile.PostedFile.ContentLength != 0)
{
string upFileName = this.upfile.PostedFile.FileName;
string[] strTemp = upFileName.Split('.');
string upFileExp = strTemp[strTemp.Length - 1].ToString();
ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
this.upfile.PostedFile.SaveAs(ServerFileName);
myMail.Attachments.Add(new MailAttachment(ServerFileName));
}
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "yangtao1828@163.com"); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ""); //发送方邮件密码

SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);

return true;
}
catch
{
return false;
}

}

效果截图:



第二种方法:

SmtpClient mailClient = new SmtpClient("smtp.163.com");

//Credentials登陆SMTP服务器的身份验证.
mailClient.Credentials = new NetworkCredential("yangtao1828", "");//登录邮箱的用户名和密码

//test@qq.com发件人地址、test@tom.com收件人地址
MailMessage message = new MailMessage(new MailAddress("yangtao1828@163.com"), new MailAddress ("warren_life@yahoo.com"));

// message.Bcc.Add(new MailAddress("tst@qq.com")); //可以添加多个收件人

message.Body = "Hello Word!";//邮件内容
message.Subject = "this is a test";//邮件主题

//Attachment 附件
Attachment att = new Attachment(@"d:/new/scat.txt");
message.Attachments.Add(att);//添加附件
Console.WriteLine("Start Send Mail....");
//发送....
mailClient.Send(message);

Console.WriteLine("Send Mail Successed");
Console.ReadLine();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: