asp.net 邮件发送程序
如何用asp.net 邮件发送程序作邮件发送程序 --------------------编程问答-------------------- 你太懒了. --------------------编程问答----------------------------------------编程问答-------------------- 很多..開源的組件也不少.....搜搜... --------------------编程问答-------------------- private bool SendMail(string toMail, string subject, string body)
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="sRecipientEmail">收件人地址</param>
/// <param name="sSubject">主题</param>
/// <param name="sMessage">内容</param>
/// <returns>发送是否成功</returns>
public static bool SendMail(string sRecipientEmail, string sSubject, string sMessage,string sSendName)
{
//邮件对象
MailMessage emailMessage;
//smtp客户端对象
SmtpClient client;
// 初始化邮件对象
if (string.IsNullOrEmpty(sSendName))
{
sSendName = "客户服务中心";
}
//String sSenderEmail = ConfigurationManager.AppSettings["EmailAddress"];
String sSenderEmail = ""+sSendName+"<" + ConfigurationManager.AppSettings["EmailAddress"] + ">";
emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
emailMessage.IsBodyHtml = true;
emailMessage.SubjectEncoding = System.Text.Encoding.Default;
emailMessage.BodyEncoding = System.Text.Encoding.Default;
//加入
emailMessage.Headers.Add("X-Priority", "3");
emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
emailMessage.Headers.Add("ReturnReceipt", "1");
//邮件发送客户端
client = new SmtpClient();
//邮件服务器及帐户信息
client.Host = ConfigurationManager.AppSettings["EmailServer"];
//client.Host = "smtp.163.com";
//client.Port = 465;
//client.EnableSsl = true;
System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
Credential.UserName = ConfigurationManager.AppSettings["EmailUserName"];
Credential.Password = ConfigurationManager.AppSettings["EmailPassword"];
//TODO: 需要更新该密码到web.config
client.Credentials = Credential;
try
{
client.Send(emailMessage);
}
catch (Exception e)
{
//错误处理待定
string sMsg = e.Message;
return false;
}
return true;
}
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath.ToString()+"\\data.xml");
string fromMail = ds.Tables[0].Rows[0]["frommail"].ToString();
MailMessage myMail = new MailMessage();
myMail.From = fromMail;
myMail.To = toMail;
//myMail.Cc = ccMail;
//myMail.Bcc = bccMail;
myMail.Subject = subject;
myMail.Body = body;
myMail.BodyFormat = 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", ds.Tables[0].Rows[0]["username"].ToString()); //发送方邮件帐户
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ds.Tables[0].Rows[0]["password"].ToString()); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
}
自带的很好用的 --------------------编程问答-------------------- you too 懒 ! --------------------编程问答-------------------- 用JMail,很简单。 --------------------编程问答-------------------- 上面已经很详细了。楼主照着瞧出来就行了 --------------------编程问答-------------------- 楼上的人太惯了,所以很多人都懒了
----------------------------------------------------------------------------------------
欢迎访问我的新站:http://www.FavNet.cn —— 专勤致精 想您所思 专业IT技术服务
展示技术实力,寻求合作伙伴、合作项目中…… --------------------编程问答-------------------- Dim message As new Mail.MailMessage
message.From = "web3@163.com"
message.To = "web3@163.com"
message.Subject = "测试"
message.Body = "内容"
Mail.SmtpMail.SmtpServer = "localhost"
Mail.SmtpMail.Send(message)
参考 --------------------编程问答-------------------- private static string SendMail(string MailFrom, string MailTo, string MailTocc, string Subject, string Content, string PRI, string MailServer)
{
if ((((MailFrom != null) && (MailFrom.Trim() != "")) && ((MailTo != null) && (MailTo.Trim() != ""))) && (((Subject != null) && (Subject.Trim() != "")) && ((MailServer != null) && (MailServer.Trim() != ""))))
{
try
{
MailMessage message = new MailMessage();
message.From = MailFrom;
message.To = MailTo;
if ((MailTocc != null) && (MailTocc.Trim() != ""))
{
message.Cc = MailTocc;
}
message.Subject = Subject;
message.Body = Content;
SmtpMail.SmtpServer = MailServer.Trim();
message.BodyFormat = MailFormat.Html;
if ((PRI != null) && (PRI.Trim() != "0"))
{
if (PRI == "1")
{
message.Priority = MailPriority.Normal;
}
else
{
message.Priority = MailPriority.Low;
}
}
else
{
message.Priority = MailPriority.High;
}
SmtpMail.Send(message);
return "1";
}
catch (Exception exception)
{
return exception.Message;
}
}
return "Email必要参数缺失";
}
--------------------编程问答-------------------- 楼上这些方法,在邮件服务器要求加密的时候就不能用了。 --------------------编程问答-------------------- 有大虾可以介绍一下邮件服务器吗?我做的邮件发送程序显示发送成功,可实际上并没有发送出去.是不是因为我机器的邮件服务器没有设置或根本就没有邮件服务器? --------------------编程问答-------------------- 用2005 我是没有实现过,学习
--------------------编程问答-------------------- 什么也不想说。
补充:.NET技术 , ASP.NET