日期:2014-05-18  浏览次数:20855 次

C#发邮件的问题,很郁闷!
各位老师,小妹今天我想做一个发邮件的测试,看了微软的KB,复制下来写的,也不对,弄得小妹很郁闷!
有两个问题:
第一:发送邮件到底是using System.net.mail还是using System.web.mail呢?
第二:
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );

我这样写有错吗?
我觉得是没什么错误的,但是编译器总提示我什么什么过期了,到底现在写一个发送邮件的程序应该怎么写呢?

请帮小妹写一个例子吧,谢谢各位大哥!~:)

------解决方案--------------------
system.net.mail是.net2.0的,另外一个是1.1的,我用2.0写过的。smtpserver要规定好,比如用gmail的话,就用smtp.gmail.com等。。。
------解决方案--------------------
1你的smtp服务没开,或者设置有问题,和你的网络环境有关
2没有设置对邮件发送服务器
两种可能中的任一
------解决方案--------------------
第一个问题:using System.net.mail
第二个问题:
C# code

System.Net.Mail.MailMessage mss = new System.Net.Mail.MailMessage();       
mss =new  System.Net.Mail.MailMessage("__@__.com","__@__.com");
mss.Subject = "主题";
mss.Body = "内容";
System.Net.Mail.SmtpClient cl = new System.Net.Mail.SmtpClient("服务器",端口);
cl.Credentials = new System.Net.NetworkCredential("验证用户名","验证密码");
cl.Send(mss)

------解决方案--------------------
MailMessage mailMessage = new MailMessage();

string strBody;
mailMessage.From=new MailAddress("xxxxxx@163.com"); //发送人
mailMessage.To.Add("xxxxxx@yahoo.com.cn"); //收信人
mailMessage.Subject="Web order";
strBody = "<h2>" + ddlMainTag.SelectedItem.Text + "</h2><br>";
strBody += "<h4>" + tbTag.Text + "</h4><br><hr><br>";
strBody += "·" + tbAdv.Text;
sendMail(mailMessage);

foreach (string bo in c)
{
Bodytext = "邮件内容";
}

objMl.Body = Bodytext;
SmtpMail.Send(objMl); 
private void sendMail(MailMessage mail)

{
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.163.com";
smtpClient.Port = 465;
smtpClient.Credentials = new System.Net.NetworkCredential("p_bings@163.com","hacker");
try
{
smtpClient.Send(mail);
// Label1.Text = "ok";
}
catch
{
Response.Write("发送失败!");
// Label1.Text = "false";
}
}
------解决方案--------------------
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com"; //郵箱服務器
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("髮件人郵箱名", "密碼");
client.DeliveryMethod = SmtpDeliveryMethod.Network;

System.Net.Mail.MailMessage message = new MailMessage("髮件人郵箱名", "收件人郵箱名");
message.Subject = "主題";
message.Body = "正文";
//message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
//添加附件
Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Oct