try
{
MailMessage myMail = new MailMessage();
//myMail.From = "[email protected]";// fromMail;//发件人
myMail.From = "[email protected]";// fromMail;//发件人
myMail.To = toMail;//收件人
myMail.Cc = ccMail;//抄送人
myMail.Bcc = bccMail;//暗送人
myMail.Subject = subject;//主题
myMail.Body = body;
myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html;
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*******"); //发送方邮件密码 SmtpMail.SmtpServer = "mail.sttrc.cn";
myMail.Priority = MailPriority.High;
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
跳入catch,不能发送邮件,不知何故啊?
{
MailMessage myMail = new MailMessage();
//myMail.From = "[email protected]";// fromMail;//发件人
myMail.From = "[email protected]";// fromMail;//发件人
myMail.To = toMail;//收件人
myMail.Cc = ccMail;//抄送人
myMail.Bcc = bccMail;//暗送人
myMail.Subject = subject;//主题
myMail.Body = body;
myMail.BodyFormat = sendMode == "0" ? MailFormat.Text : MailFormat.Html;
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*******"); //发送方邮件密码 SmtpMail.SmtpServer = "mail.sttrc.cn";
myMail.Priority = MailPriority.High;
SmtpMail.Send(myMail);
return true;
}
catch
{
return false;
}
跳入catch,不能发送邮件,不知何故啊?
//方法如下:
//一:用自己的服务器作为邮件服务器时:
//如出现:邮箱不可用。 服务器响应为: 5.7.1 Unable to relay for ***@gmail.com
//解决办法如下:在IIS中,右击“默认SMTP虚拟服务器”,选择“属性”,切换到“访问”页,点击“中继”按钮,在弹出框中选择“仅以下列表除外”,确定。
//二:用其它的邮件服务器发送,经测试大多数免费邮箱都不提供SMTP服务,用qq邮箱的就可以,方法:进入qq邮箱-->设置-->帐户-->开通POP3/IMAP/SMTP服务,即可用smtp.qq.com作为邮件服务器了,可用它来发送邮件using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
//第一个参数是邮箱服务器
//第二个参数发件人的帐号
//第三个参数发件人密码
//第四个参数收件人帐号
//第五个参数主题
//第六个参数内容.
protected void Button1_Click(object sender, EventArgs e)
{//发送邮件
try
{
//SendSMTPEMail("nz.oicp.net", "[email protected]", "123", "[email protected]", "123", "用asp.net发送邮件,用自己的邮件服务器,测试成功");
SendSMTPEMail("smtp.qq.com", "[email protected]", "123xxxx邮箱密码", "[email protected]", "123", "用asp.net发送邮件,用qq的smtp.qq.com服务器,测试成功");}
catch (Exception er)
{
Label1.Text = er.Message;
}
}
}
提供SMTP/POP3服务不?
如果不支持SMTP功能的哈,发不了的,就像之前163的邮箱一样关闭了SMTP功能,不过2010年又开启了SMTP功能。
public static void Sends(string email, string pass)
{
string formto = "[email protected]";
string to = email;
string content = " ";
string body = "";
body += pass;
string name = "邮箱名";
string upass = "密码";
string smtp = "smtp地址";
SmtpClient _smtpClient = new SmtpClient();
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
_smtpClient.Host = smtp; //指定SMTP服务器
_smtpClient.Credentials = new System.Net.NetworkCredential(name, upass);//用户名和密码
MailMessage _mailMessage = new MailMessage();
//发件人,发件人名
_mailMessage.From = new MailAddress(formto, "邮件显示的名字");
//收件人
_mailMessage.To.Add(to);
_mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
_mailMessage.Subject = content;//主题 _mailMessage.Body = body;//内容
_mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");//正文编码
_mailMessage.IsBodyHtml = true;//设置为HTML格式
_mailMessage.Priority = MailPriority.High;//优先级
try
{
_smtpClient.Send(_mailMessage);
}
catch (Exception)
{
}
}
#endregion
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
//第一个参数是邮箱服务器
//第二个参数发件人的帐号
//第三个参数发件人密码
//第四个参数收件人帐号
//第五个参数主题
//第六个参数内容.
protected void Button1_Click(object sender, EventArgs e)
{//发送邮件
try
{
SendSMTPEMail("smtp.sttrc.cn", "[email protected]", "你的[email protected]邮箱密码", "[email protected]", "123", "用smtp.sttrc.cn发送邮件,用smtp.sttrc.cn服务器,测试成功");//[email protected]这个是我的邮箱,换成你自己的}
catch (Exception er)
{
Label1.Text = er.Message;
}
}
}
**
** DotNet1.1环境下 Code CreateBy BANLAO 2005.6.14
**
**********************************************************************/using System.Web.Mail;public void _Dotnet1.1SendMail()
{
strMessage = ""; //定义错误信息 MailMessage mailMsg = new MailMessage();
mailMsg.From = strMailFrom; mailMsg.To = strMailTo;
mailMsg.Subject = strMailTitle;
mailMsg.Priority = MailPriority.High; //邮件级别,.High、.Low、.Normal
mailMsg.BodyFormat = MailFormat.Text; //邮件形式,.Text、.Html
mailMsg.Body = strEmailBody; mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //设置支持服务器验证
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strMailFrom); //设置用户名
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPsd); //设置用户密码 SmtpMail.SmtpServer = strMailSMTP; //or SmtpMail.SmtpServer.Insert( 0, strMailSMTP);
try
{
//发送邮件
SmtpMail.Send( mailMsg );
}
catch( Exception Message )
{
strMessage = "发送失败" + Message.Message.ToString();
} if( strMessage != "" )
{
Response.Write(strMessage );
}
else
{
Response.Write("发送成功!");
}
}/**********************************************************************
**
** DotNet2.0环境下 Code CreateBy BANLAO 2006.11.23
**
**********************************************************************/using System.Net.Mail;public void _Dotnet2.0SendMail()
{
strMessage = ""; MailMessage mailMsg = new MailMessage(); mailMsg.To.Add(new MailAddress("收件人邮箱集合")); //设置收件人的邮件地址
mailMsg.From = new MailAddress("发件人邮箱地址"); //设置发送者的邮件地址
mailMsg.Subject = strMailTitle; //设置邮件主题
mailMsg.Priority = MailPriority.High; // 设置邮件级别:.High、.Low、.Normal
mailMsg.IsBodyHtml = true; //设置邮件形式:是否为HTML格式
mailMsg.SubjectEncoding = System.Text.Encoding.UTF8;
mailMsg.BodyEncoding = System.Text.Encoding.UTF8;
mailMsg.Body = strEmailHTML; //设置发送邮件服务器
SmtpClient client = new SmtpClient(strMailSMTPServer); client.Credentials = new System.Net.NetworkCredential("发件人邮箱", "发件人邮箱密码");
client.EnableSsl = true; //经过SSL加密 try
{
client.Send(mailMsg);
}
catch (Exception Message)
{
strMessage = "发送失败" + Message.Message.ToString();
} if( strMessage != "" )
{
Response.Write(strMessage );
}
else
{
Response.Write("发送成功!");
}
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/abandonship/archive/2008/12/22/3580975.aspx
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;public partial class E3_SB_Goods3_SendMail : System.Web.UI.Page
{
string userId = null;
string mail = null;//发送者邮箱
string pwd = null;//发送者邮箱密码
string tomail = null;//发送给
string fileName = null;//附件名
string smtpString = null; protected void Page_Load(object sender, EventArgs e)
{
this.Label4.Text = "";
userId = Request.QueryString["userId"].ToString();
if (userId == null || userId == "")
{
Response.Redirect("../Default.aspx");
}
else
{
SqlConnection conn = new SqlConnection("Data Source=.;Database=information;User Id=sa;Password=sa2005;");
conn.Open();
string sql = "select Email,other1 from sb_users where userId='" + userId + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
mail = reader["email"].ToString();
pwd = reader["other1"].ToString();
}
}
} public void send()
{
MailAddress EmailFrom =new MailAddress(mail);
tomail = this.TextBox1.Text;
MailAddress EmailTo = new MailAddress(tomail); MailMessage Email = new MailMessage(EmailFrom, EmailTo);
Email.IsBodyHtml = true;
Email.Priority = MailPriority.High; Email.Subject = this.TextBox2.Text;
Email.Body = this.Editor1.Text;
//添加抄送电子邮件
Email.CC.Add(EmailTo);
string fm = Session["fileName"].ToString();
// string str1 = fm.Substring(0,str1.("_"));
string[] _arr = fm.Split('_');
string _sOut = "";
if (_arr.Length > 2)
{
_sOut = string.Join("_", _arr, 2, _arr.Length - 2);
}
if (fm != null)
{
Attachment data = new Attachment(fm, System.Net.Mime.MediaTypeNames.Application.Octet);
data.Name = _sOut;
// Add time stamp information for the file.
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
//disposition.CreationDate = System.IO.File.GetCreationTime(fm);
//disposition.ModificationDate = System.IO.File.GetLastWriteTime(fm);
//disposition.ReadDate = System.IO.File.GetLastAccessTime(fm);
// Add the file attachment to this e-mail message.
Email.Attachments.Add(data);
}
Regex reg = new Regex(@"@([^\.]*?)\.");
Match m = reg.Match(mail);
string mailStr = null;
if (m.Success)
{
mailStr = m.Result("$1");
} if (mailStr.Equals("qq"))
{
smtpString = "smtp.qq.com";
}
if (mailStr.Equals("163"))
{
smtpString = "smtp.163.com";
}
if (mailStr.Equals("sohu"))
{
smtpString = "smtp.sohu.com";
}
if (mailStr.Equals("sina"))
{
smtpString = "smtp.sina.com";
} SmtpClient Client = new SmtpClient(smtpString); //验证 (Credentials 凭证)
Client.Credentials = new System.Net.NetworkCredential(mail, pwd); //处理待发的电子邮件的方法 (Delivery 发送,传输)
Client.DeliveryMethod = SmtpDeliveryMethod.Network; try
{
Client.Send(Email);
Response.Write("<script>alert('已发送成功啦!');window.close()</script>");
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable)
{
Response.Write("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
Client.Send(Email);
}
else
{
Response.Write("Failed to deliver message to {0}" + ex.FailedRecipient[i].ToString().Trim() + "");
}
} }
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text == null || this.TextBox1.Text == "")
{
Response.Write("<script>alert('请填写邮件地址!');</script>");
}
send();
} protected void Button2_Click(object sender, EventArgs e)
{
Boolean fileOk = false;
string path = Server.MapPath("~/upload/");
//判断是否已经选取文件
if (FileUpload1.HasFile)
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
//限定只能上传jpg和gif图片
string[] allowExtension = { ".jpg", ".gif", ".jpeg", ".doc", ".xls" };
//对上传的文件的类型进行一个个匹对
for (int i = 0; i < allowExtension.Length; i++)
{
if (fileExtension == allowExtension[i])
{
fileOk = true;
break;
}
}
//对上传文件的大小进行检测,限定文件最大不超过10M
if (FileUpload1.PostedFile.ContentLength > 10240000)
{
fileOk = false;
}
//最后的结果
if (fileOk)
{
try
{
string time = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString("00") + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00");
fileName = userId + "_" + time + "_" + this.FileUpload1.FileName;
this.FileUpload1.PostedFile.SaveAs(path + fileName);
Session["fileName"] = path+fileName;
this.Label4.Text = "上传成功!上传的文件名是:"+FileUpload1.FileName;
}
catch
{
this.Label4.Text = "上传失败!";
}
}
else
{
this.Label4.Text = "文件类型或者文件大小超出10M!";
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
//第一个参数是邮箱服务器
//第二个参数发件人的帐号
//第三个参数发件人密码
//第四个参数收件人帐号
//第五个参数主题
//第六个参数内容.
protected void Button1_Click(object sender, EventArgs e)
{//发送邮件
try
{
SendSMTPEMail("smtp.sttrc.cn", "[email protected]", "你的[email protected]邮箱密码", "[email protected]", "123", "用smtp.sttrc.cn发送邮件,用smtp.sttrc.cn服务器,测试成功");//[email protected]这个是我的邮箱,换成你自己的}
catch (Exception er)
{
Label1.Text = er.Message;
}
}
}
是不是你的smtp.sttrc.cn不支持发邮件服务???
用[email protected]邮箱向你的[email protected]邮箱发送了一个邮件,请查看你的[email protected]邮箱