日期:2014-05-19  浏览次数:20899 次

发送邮件错误
//create   the   mail   message
                        MailMessage   mail   =   new   MailMessage();

                        //set   the   addresses
                        mail.From   =   new   MailAddress( "xxx@163.com ");
                        mail.To.Add( "xxx@163.com ");

                        //set   the   content
                        mail.Subject   =   "This   is   an   email ";
                        mail.Body   =   "this   is   the   body   content   of   the   email. ";

                        //send   the   message
                        SmtpClient   smtp   =   new   SmtpClient( "smtp.163.com ");
                        smtp.Send(mail);

这是代码,总是报异常.Message   :   Sending   Mail


------解决方案--------------------
try
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add( "要发送的邮箱@163.com ");
mail.From = new System.Net.Mail.MailAddress( "xxx@163.com ", "题目 ", System.Text.Encoding.UTF8);
mail.Subject = "题目 ";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = System.Net.Mail.MailPriority.High;


System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
// client.Host = "smtp.gmail.com ";如果你邮箱是gmail的,用这个
client.Host = "smtp.163.com "; 如果你邮箱是163的,用这个
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential( "你的邮箱 ", "你邮箱的密码 ");
client.Send(mail);
mail.Dispose();
client = null;
}
catch
{
Response.Write( "error ");
GC.Collect();
return;
}

Response.Write( "true ");
------解决方案--------------------
一下代码是应用的别人博客上的,可以实现。
注:如果你的杀毒软件禁止了25端口请打开。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace SendEmail
{
class Program
{
static void Main(string[] args)
{
try
{

MailMessage m_message = new MailMessage();
m_message.From = new MailAddress( "pankh@inspur.com ");
m_message.To.Add(new MailAddress( "pankh_1983@hotmail.com "));
m_message.Subject = "使用.NET 2.0发送邮件 ";