发邮件怎么老发不成功?代码不多,高手过来接分
protected void btnSend_Click(object sender, EventArgs e){
string mailto = this.TextTo.Text;
string mailfrom = this.TextFrom.Text;
string mailpwd = this.TextPwd.Text;
string mailtile = this.Texttile.Text;
string mailcontent = this.TextContent.Text;
if ( SendMailTo(mailto, mailfrom, mailpwd, mailtile, mailcontent))
{
this.Label1.Text = "发送成功";
}
else
{
this.Label1.Text = "发送失败";
}
}
public bool SendMailTo(string mailto,string mailfrom,string mailpwd,string mailtile,string mailcontent)
{
try
{
MailMessage message = new MailMessage(mailfrom, mailto, mailtile, mailcontent);
message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.126.com");
client.Credentials = new NetworkCredential(mailfrom, mailpwd);
client.Send(message);
return true;
}
catch
{
return false;
}
}
catch 邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨ --------------------编程问答-------------------- public bool SendMailTo(string mailto,string mailfrom,string mailpwd,string mailtile,string mailcontent)
{
try
{
string name = mailfrom.Substring(0,mailfrom .IndexOf ("@"));
MailMessage message = new MailMessage(mailfrom, mailto, mailtile, mailcontent);
message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.126.com");
client.Credentials = new NetworkCredential(name, mailpwd);
client.Send(message);
return true;
}
catch
{
return false;
}
}
--------------------编程问答-------------------- 原来是126的问题 --------------------编程问答-------------------- 换成STMP.163.com的试一下,我曾经用163的做过,可以发送 --------------------编程问答-------------------- 用Tom的可以~ --------------------编程问答-------------------- 126smtp服务器有问题 反正以前是pop服务有问题
还有就是可能需要ssl --------------------编程问答-------------------- 这种情况大多是smtpserver的问题.换其他的试试,我原来做这个的时候用过21cn的可以 --------------------编程问答-------------------- 我用的smtp.163.com的smtpserver可以. 发邮件建议使用CDO组件来发送.好用 --------------------编程问答-------------------- 要不你改用IIS替发,其中必须注意的是你必须机子上装了SMTP服务,并启动该服务。才可以
用这个肯定行的 --------------------编程问答-------------------- 能不能发个可以发的原代码啊,感激不尽! --------------------编程问答--------------------
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = @"D:\asdf.txt";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"asdf@163.com",
"asdf@163.com",
"test",
"no du");
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Send(message);
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Second % 20 == 0)
{
CreateMessageWithAttachment("smtp.163.com");
}
}
补充:.NET技术 , ASP.NET