各位老师,小妹今天我想做一个发邮件的测试,看了微软的KB,复制下来写的,也不对,弄得小妹很郁闷!
有两个问题:
第一:发送邮件到底是using System.net.mail还是using System.web.mail呢?
第二:
MailMessage mail = new MailMessage();
mail.To = "[email protected]";
mail.From = "[email protected]";
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 );我这样写有错吗?
我觉得是没什么错误的,但是编译器总提示我什么什么过期了,到底现在写一个发送邮件的程序应该怎么写呢?请帮小妹写一个例子吧,谢谢各位大哥!~:)
有两个问题:
第一:发送邮件到底是using System.net.mail还是using System.web.mail呢?
第二:
MailMessage mail = new MailMessage();
mail.To = "[email protected]";
mail.From = "[email protected]";
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 );我这样写有错吗?
我觉得是没什么错误的,但是编译器总提示我什么什么过期了,到底现在写一个发送邮件的程序应该怎么写呢?请帮小妹写一个例子吧,谢谢各位大哥!~:)
解决方案 »
- “,” 附近有语法错误 在线等
- StringBuilder.Length=0,和StringBuilder.Clear(),功能完全一样吗?
- 一个SQLSERVER数据库太大而且正在使用中,我想研究它的结构,如何复制它一个不包含记录的空数据库?
- 我做的网站的首页,客户端里视图状态的代码有巨巨巨多!!!但是禁用视图状态的话,网站就瘫了。 谁有什么好办法??
- 又一道面试题--一对大兔子生小兔子的算法---欢迎大家讨论!!!!
- WPF,请看下Grid的布局
- 如何设置TabControl背景为透明
- 数据库的连接问题 我感觉没什么问题的 请高手指点下
- 浮动窗体怎样设置?
- 救救我啊,线程释放问题!
- 快速开贴揭帖,求一个简单的字符串处理算法
- 关于画图的2个问题
请问是怎么回事呢?
那正确的程序应该怎么写呢?
2没有设置对邮件发送服务器
两种可能中的任一
第二个问题: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)
string strBody;
mailMessage.From=new MailAddress("[email protected]"); //发送人
mailMessage.To.Add("[email protected]"); //收信人
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("[email protected]","hacker");
try
{
smtpClient.Send(mail);
// Label1.Text = "ok";
}
catch
{
Response.Write("发送失败!");
// Label1.Text = "false";
}
}
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.Octet);
message.Attachments.Add(data); try
{
client.Send(message); //發送郵件。
Label1.Text = "發送成功";
}
catch (Exception ex)
{
Label1.Text = "發送失敗";
}
mail.To.Add(this.textBox1.Text);
mail.From = new MailAddress(this.textBox2.Text, this.textBox3.Text, Encoding.UTF8);
mail.Subject = this.textBox4.Text;
mail.SubjectEncoding = Encoding.UTF8;
mail.Body = this.richTextBox1.Text;
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High; //SMTP
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";
client.Port = 465;
client.EnableSsl = true;
//client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("163邮箱用户名", "邮箱密码"); object userState = mail; try
{
client.SendAsync(mail, userState);
MessageBox.Show("成功发送到" + this.textBox1.Text + "这儿"); }
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show("失败:" + ex.Message);
}
我用Try-Catch都捕获不了错误,提示的是每次都发送成功了,但是我的126.com邮箱就是收不到邮件啊!!!
郁闷死了!~请帮我看看这段代码哪儿有错误好吗?谢谢各位大哥
Message.To = to;
Message.From = "[email protected]";
Message.Subject = "web mail";
Message.Body = msg;
Message.BodyFormat = MailFormat.Html;
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);//基本权限
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username");//用户名
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");//密码
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 8025);//端口 Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendserver", "smtp.gmail.com");
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);//SSL登陆 SmtpMail.Send(Message);
现在编译器又报这个错误了,请问应该怎么办呢?谢谢各位大哥
mail.To.Add("[email protected]"); mail.From = new MailAddress("[email protected]", "zhangyi", Encoding.UTF8);
mail.Subject = "test";
mail.SubjectEncoding = Encoding.UTF8;
mail.Body = "testmail";
mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = false;
mail.Priority = MailPriority.High; //SMTP
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Host = "smtp.126.com";
object userState = mail; try
{
client.Send(mail);
Console.WriteLine("Successful");
}
catch (SmtpException ex)
{
Console.WriteLine("Err: " + ex.Message);
}