关于Asp.net 发送邮件的问题

htltee 2009-03-22 01:57:07
我想知道下邮件到底是怎么发送的...原理是什么亚...
比如我在126写了封邮件...就相当于在126的服务器上写了东西...
然后服务器是怎么发送到指定的收件人的阿...
smtp协议是怎么回事的亚...

然后是关于asp.net的
我用.net.mail来实现发送邮件的
在web.config里配置<mailsettings><smtp>
host port应该是邮件服务器的相关配置
那userName password配置的到底是什么?
是发件人的username 和password 还是什么

另外关于邮件中继器到底是怎么回事情亚...
<mailsettings><smtp>莫非配置的userName 和password和他有没有关系亚

请各位大虾讲解下吧 谢谢
...全文
242 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sysping1 2009-06-07
  • 打赏
  • 举报
回复
//发邮件
//confusionFromMail:是否产生迷惑邮件服务器的邮件地址
//EncryptOption:是否加密邮件
//mailDESKey:加密邮件时的DES KEY
public static void SendMail(
string smtpHost, int smtpPort,
string smtpUser, string smtpPassword,
string fromMail, string sender,
string toMail, string receiver,
string subject, string body,
string attachment, string attachName,
bool smtpByIIS, bool smtpEnableSSL,
bool smtpEnableAuth, bool confusionFromMail, EncryptOption mailEncrypt, string mailDESKey
)
{
//Smtp Server
SmtpClient smtpServer = new SmtpClient(smtpHost, smtpPort);
smtpServer.UseDefaultCredentials = true;
if (smtpEnableAuth)
{
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword);
}
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.EnableSsl = smtpEnableSSL;

//Confusion Mail 产生迷惑邮件服务器的邮件地址
string frMail = fromMail;
if (confusionFromMail) frMail = BMailHelper.MakeConfusionMail(fromMail);

//Mail Message
Encoding encoding = Encoding.GetEncoding("GB2312");
MailAddress fromMailAddress = new MailAddress(frMail, sender, encoding);
MailAddress toMailAddress = new MailAddress(toMail, receiver, encoding);
MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress);
mailMessage.Priority = MailPriority.High;

//Attachment
string encryptAttachment = EncryptHelper.SignatureAndEncrypt(attachment, mailDESKey, mailEncrypt);
System.Net.Mail.Attachment att = System.Net.Mail.Attachment.CreateAttachmentFromString(encryptAttachment, BFormatingMail.AttachName);
att.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
att.NameEncoding = encoding;
att.Name = attachName;
mailMessage.Attachments.Add(att);

//Body
mailMessage.SubjectEncoding = encoding;
mailMessage.Subject = subject;
mailMessage.BodyEncoding = encoding;
mailMessage.Body = EncryptHelper.SignatureAndEncrypt(body, mailDESKey, mailEncrypt);

//Sending
smtpServer.Send(mailMessage);
smtpServer.ServicePoint.CloseConnectionGroup(smtpServer.ServicePoint.ConnectionName);
}

/// 产生迷惑邮件服务器的邮件地址
/// <param name="targetMail">发件地址.</param>
public static string MakeConfusionMail(string targetMail)
{
string s1 = BFormatingMail.GetEmailPart(targetMail, 1) ?? string.Empty;

string[] domains = new string[] {
"asys.com", "aspt.com", "aast.com", "aaet.com",
"bsys.com", "bspt.com", "bast.com", "baet.com",
"csys.com", "cspt.com", "cast.com", "caet.com",
"dsys.com", "dspt.com", "dast.com", "daet.com",
"esys.com", "espt.com", "east.com", "eaet.com",
"fsys.com", "fspt.com", "fast.com", "faet.com",
"gsys.com", "gspt.com", "gast.com", "gaet.com",
"amou.com", "acat.com", "apig.com", "acar.com",
"bmou.com", "bcat.com", "bpig.com", "bcar.com",
"cmou.com", "ccat.com", "cpig.com", "ccar.com",
"dmou.com", "dcat.com", "dpig.com", "dcar.com",
"emou.com", "ecat.com", "epig.com", "ecar.com",
"fmou.com", "fcat.com", "fpig.com", "fcar.com",
"hmou.com", "hcat.com", "hpig.com", "hcar.com",
"imou.com", "icat.com", "ipig.com", "icar.com",
"jmou.com", "jcat.com", "jpig.com", "jcar.com",
"kmou.com", "kcat.com", "kpig.com", "kcar.com",
"lmou.com", "lcat.com", "lpig.com", "lcar.com",
"mmou.com", "mcat.com", "mpig.com", "mcar.com",
"nmou.com", "ncat.com", "npig.com", "ncar.com",
"ayal.com", "ayat.com", "aybt.com", "ayag.com",
"byal.com", "byat.com", "bybt.com", "byag.com",
"cyal.com", "cyat.com", "cybt.com", "cyag.com",
"dyal.com", "dyat.com", "dybt.com", "dyag.com",
"eyal.com", "eyat.com", "eybt.com", "eyag.com",
"fyal.com", "fyat.com", "fybt.com", "fyag.com",
"gyal.com", "gyat.com", "gybt.com", "gyag.com",
"hyal.com", "hyat.com", "hybt.com", "hyag.com",
"iyal.com", "iyat.com", "iybt.com", "iyag.com",
"jyal.com", "jyat.com", "jybt.com", "jyag.com",
"kyal.com", "kyat.com", "kybt.com", "kyag.com"
};
int index = (new Random()).Next(1, domains.Length);
if (index <= 0) index = 1;
string s2 = domains[index - 1];
return string.Format("{0}@{1}", s1, s2);
}
fuzijing 2009-06-04
  • 打赏
  • 举报
回复
...
DareOnly 2009-06-04
  • 打赏
  • 举报
回复
http://hi.baidu.com/pctonc/blog/item/a359b794d8aae0007af48058.html
End 2009-06-04
  • 打赏
  • 举报
回复

#region[发送邮件]
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void bt_send_Click(object sender, EventArgs e)
{
string attachmenturl = up_MailAttachment.PostedFile.FileName.Replace("\\", "\\\\");
//创建一个附件对象
MailAttachment objmailattachment = new MailAttachment(attachmenturl);
//创建邮件内容对象
MailMessage objmailmessage = new MailMessage();
objmailmessage.To = txt_to.Text.ToString();//目的邮件地址
objmailmessage.From = "sglcj@126.com";//源邮件地址
objmailmessage.Subject = "test send Email";//邮件主题
objmailmessage.Body = "test send Email,success or Failure";//邮件正文
objmailmessage.Attachments.Add(objmailattachment);//将附件添加到邮件中
//基本权限
objmailmessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
objmailmessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "sglcj");//sglcj为发信邮箱账号
//密码
objmailmessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "456456");//456456发信邮箱密码
//
SmtpMail.SmtpServer = "smtp.126.com";

//SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.Send(objmailmessage);
}
#endregion


亲测 可用
chen_ya_ping 2009-06-04
  • 打赏
  • 举报
回复
学习
shenweiblue 2009-06-04
  • 打赏
  • 举报
回复
ding
jin225 2009-06-04
  • 打赏
  • 举报
回复
System.Timers.Timer time = new System.Timers.Timer();
protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"E:\WebSites\ChinaStakes\SendEmailWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("启动成功" + DateTime.Now.ToString() + "\n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();


time.Elapsed += new System.Timers.ElapsedEventHandler(time_Elapsed);
time.Interval = 1000;
time.Start();
time.Enabled = true;

}

protected override void OnStop()
{
time.Enabled = false;
time.Stop();
time.Close();
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
FileStream fs = new FileStream(@"E:\WebSites\ChinaStakes\SendEmailWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("服务已停止" + DateTime.Now.ToString() + "\n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();
}

void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//&& DateTime.Now.Minute.ToString() == "47" DateTime.Now.Second.ToString() == "10" && && DateTime.Now.Hour.ToString() == "7" && DateTime.Now.DayOfWeek.ToString() == "Saturday"
if (DateTime.Now.Second.ToString() == "10" && DateTime.Now.Minute.ToString() == "30" && DateTime.Now.Hour.ToString() == "7" && DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
sengEmalToAll();
}
}

public void sengEmalToAll()
{
try
{
string _strSql = "select UserEmailURL from User_EmailUrl";

string sql2 = "select Email from User_SecurityInfo where AccountStatus=1";
SqlConnection con = new SqlConnection("Server=(local);User id=webuser;Pwd=webuser;Database=ChinaStakes2");//User id=webuser;Pwd=webuser;Database=ChinaStakes2
//string sql2 = "select Emails from TABLE1";
con.Open();
SqlCommand cmd = new SqlCommand(sql2, con);
SqlDataReader dr = cmd.ExecuteReader(); ;
List<string> list = new List<string>();
while (dr.Read())
{
list.Add(dr["Email"].ToString());

}
dr.Close();
con.Close();

con.Open();
SqlCommand cmd1 = new SqlCommand(_strSql, con);
SqlDataReader dr2 = cmd1.ExecuteReader();
while (dr2.Read())
{
list.Add(dr2["UserEmailURL"].ToString());
}
dr2.Close();
con.Close();
int ii = 0;
foreach (string Email in list)
{
if (Email == "" || Email == null)
{
Console.Write("aaa" + ii);
}
else
{
if (Email.IndexOf("@") > -1 && Email.IndexOf(".") > -1)
{
//服务器上的路径E:\WebSites\ChinaStakes\
FileStream fs = new FileStream(@"E:\WebSites\ChinaStakes\SendEmailWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamWriter = new StreamWriter(fs);

try
{
//创建一个MailMessage对象
MailMessage oMail = new MailMessage();
//oMail.To.Add(new MailAddress("wei_he_cool@163.com"));
//oMail.To.Add(new MailAddress("jiangongz@yahoo.ca"));
//oMail.To.Add(new MailAddress("bfp3@163.com"));
//oMail.To.Add(new MailAddress("pubzone@gmail.com"));
//oMail.To.Add(new MailAddress("banfuping@hotmail.com"));
oMail.To.Add(new MailAddress(Email));
oMail.From = new MailAddress("he.wei@chinastakes.com", "Chinastakes.com");
oMail.Subject = "Chinastakes Weekly"; //邮件标题
oMail.Body = GetEmailcontent(); //邮件内容
oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式

oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高
//发送邮件服务器
SmtpClient client = new SmtpClient("smtp.chinastakes.com");
client.Port = 25;
//client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("he.wei@chinastakes.com", "hwchinastakes"); ;//指定服务器邮件,及密码
//发送
try
{
client.Send(oMail); //发送邮件
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("发送成功" + Email + DateTime.Now.ToString() + "\n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();
// System.Threading.Thread.Sleep(6000 * 10);
}
catch (Exception ex)
{
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

m_streamWriter.WriteLine("发送失败" + ex.Message.ToString() + DateTime.Now.ToString() + "\n");

m_streamWriter.Flush();

m_streamWriter.Close();

fs.Close();
throw;

}
oMail.Dispose(); //释放资源
}
simplePJlife 2009-06-04
  • 打赏
  • 举报
回复
再给你上段VS2003 的MAILSENT方法。(在VS2005 会有警告, VS2005的 MAILSENT方法就是要配置MAILCLIENT,楼上的已经有正解了。VS2003 的MAILCLIENT 就看你的OUTLOOK 有没有配置过,配置过的话,打开IIS,选既定的SMTP 就能看到)
using System.Web.Mail;

public void sendMail(string strFrom, string strTo)
{

try
{
// 1つメールのメッセージオブジェクトを作る
MailMessage myMail = new MailMessage();

// 発信者のメールアドレスを設定
myMail.From = strFrom;
// myMail.From = "username@126.com";

// 送信者のメールアドレスを設定
myMail.To = strTo;
// myMail.Cc = "username@126.com";
// myMail.To = "username@126.com";

// メールのタイトルを設定
myMail.Subject = "mail title";

// メールのランクを設定
myMail.Priority = MailPriority.Low;

// メールの形式を設定
myMail.BodyFormat = MailFormat.Text;

// メールの内容を設定
myMail.Body = "mail 内容";

// メールのサーバを設定
SmtpMail.SmtpServer = "126.com";

//メールを発送
SmtpMail.Send(myMail);
}
catch
{
Response.Write("<script language=\"javascript\">alert('失敗して、主管者へメールを発送しない。!');</script>");
}
}
simplePJlife 2009-06-04
  • 打赏
  • 举报
回复
smtp 简单邮件传输协议。

POP 邮局传输协议。

126。COM的 SMTP 百度就知道咯

一般的都是 SMTP。****。COM
POP。****。COM


哟哟。我们公司 是直接 ****。COM
z275327170z 2009-06-04
  • 打赏
  • 举报
回复
学习,关注!
swe05046 2009-04-23
  • 打赏
  • 举报
回复
是怎么去配置smtp的呢?
artwl_cn 2009-03-22
  • 打赏
  • 举报
回复
学习,关注!
xhinker 2009-03-22
  • 打赏
  • 举报
回复
check this
http://blog.csdn.net/xhinker/archive/2009/02/24/3931231.aspx
hy_lihuan 2009-03-22
  • 打赏
  • 举报
回复
协议这个东西如果你只是写简单应用是不需要了解很清楚的;只要会使用就好了;
大部分内容framework都帮你完成了;
宝_爸 2009-03-22
  • 打赏
  • 举报
回复
smtp协议是RFC821

这里有一个中文的,不过协议看起来很枯燥。
http://blog.chinaunix.net/u1/50394/showart_469870.html
evennow 2009-03-22
  • 打赏
  • 举报
回复
俺来学习学习
htltee 2009-03-22
  • 打赏
  • 举报
回复
yagebu1983 能帮我讲讲smtp到底是怎么回事吗 谢了阿
yagebu1983 2009-03-22
  • 打赏
  • 举报
回复
老大,
这里涉及到很多的协议啊。。。
你可以去搜搜。。。
trueideal 2009-03-22
  • 打赏
  • 举报
回复
我是这样做的,现在不少mail是不支持smtp匿名了.
public string subSendMailByWebMail(string strSendMail, string strSendUserName, string strSendUserPassword, string strBody, string strSmtp, string strToMail, string strSubject, string strCc, bool isDellAttachment, string strBcc, ArrayList aryAttachment)
{
string strReturn = "";
System.Web.Mail.MailMessage objMailMessage = new System.Web.Mail.MailMessage();
objMailMessage.From = strSendMail;
objMailMessage.To = strToMail;
objMailMessage.BodyEncoding= Encoding.GetEncoding("GB2312");
//Cc
if (strCc != "")
{
objMailMessage.Cc = strCc;
}
//Bcc
if (strBcc != "")
{
objMailMessage.Bcc = strBcc;
}
objMailMessage.Subject =strSubject;
objMailMessage.Body = strBody;
objMailMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
System.Web.Mail.SmtpMail.SmtpServer = strSmtp;
//附件
if(aryAttachment!=null)
{
if (aryAttachment.Count > 0)
{
FileInfo objFile;
for (int i = 0; i < aryAttachment.Count; i++)
{
objFile = new FileInfo(aryAttachment[i].ToString());
if (objFile.Exists)
{
System.Web.Mail.MailAttachment objAttachment = new System.Web.Mail.MailAttachment(aryAttachment[i].ToString());
objMailMessage.Attachments.Add(objAttachment);
}
}
}
}
//验证
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "2"); //1,basic authentication,2,NTLM
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strSendUserName); //set your username here
objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strSendUserPassword); //set your password here
//发送
try
{
System.Web.Mail.SmtpMail.Send(objMailMessage);
}
catch (Exception ex)
{
strReturn= ex.Source + "\n" + ex.Message;
}
//删除文件
try
{
//删除文件
if (isDellAttachment)
{
for (int i = 0; i < aryAttachment.Count; i++)
{
File.Delete(aryAttachment[i].ToString());
}
}
return "";
}
catch
{
}
return strReturn;
}

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧