先把我的代码贴出来,就是从网上找的,其实大致都一样,因为就用到那么两个类,但是我连最简单的都发不出去这段是从http://blog.csdn.net/zhoufoxcn/archive/2008/08/09/2789413.aspx周公的专栏找的,怎么别人都能发呢? 我一点击发送UI就挂在那里不动了,然后很长时间后,异常抛出,显示“发送邮件失败”。大家都是怎么发邮件的??
public void Send(string to, string from, string subject, string body, string userName, string password, string smtpHost)
{
MailAddress from = new MailAddress(from);
MailAddress to = new MailAddress(to);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;//设置邮件主题
message.IsBodyHtml = true;//设置邮件正文为html格式
message.Body = body;//设置邮件内容
SmtpClient client = new SmtpClient(smtpHost);
//设置发送邮件身份验证方式
//注意如果发件人地址是[email protected],则用户名是abc而不是[email protected]
client.Credentials = new NetworkCredential(userName, password);
client.Send(message);
}
public void Send(string to, string from, string subject, string body, string userName, string password, string smtpHost)
{
MailAddress from = new MailAddress(from);
MailAddress to = new MailAddress(to);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;//设置邮件主题
message.IsBodyHtml = true;//设置邮件正文为html格式
message.Body = body;//设置邮件内容
SmtpClient client = new SmtpClient(smtpHost);
//设置发送邮件身份验证方式
//注意如果发件人地址是[email protected],则用户名是abc而不是[email protected]
client.Credentials = new NetworkCredential(userName, password);
client.Send(message);
}
2007年之后申请的126,163免费邮箱都不支持SMTP的
gmail是开放SMTP。gmail的端口号是465,而且还用来SSL只要你找个支持SMTP的就行。一般企业邮局,收费的邮箱都可以。
#region 邮件信息 private string from_addressee = "";
private string from_pwd = "";
private string to_addressee = "";
private string _title = "";
private string _body = "";
private string _smtp = "";
private string code = "gb2312";
public string Err = "";
/// <summary>
/// 发件人地址
/// </summary>
/// <value>发件人地址</value>
public string FromAddressee
{
set { this.from_addressee = value; }
}
/// <summary>
/// 收件人地址
/// </summary>
public string ToAddressee
{
set { this.to_addressee = value; }
}
/// <summary>
/// 邮件标题
/// </summary>
public string Title
{
set { this._title = value; }
}
/// <summary>
/// 邮件正文
/// </summary>
public string Body
{
set { this._body = value; }
}
/// <summary>
/// smtp服务器地址
/// </summary>
public string Smtp
{
set { this._smtp = value; }
}
/// <summary>
/// 发件人的邮箱密码
/// </summary>
public string FromPwd
{
set { this.from_pwd = value; }
}
public string Code
{
set { this.code = value; }
}
#endregion
/// <summary>
/// 发送邮件
/// </summary>
/// <returns>返回“1”为发送成功,“-1”为发送失败</returns>
public int Send()
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from_addressee);
message.To.Add(to_addressee);
message.Body = _body;
message.Subject = _title;
message.SubjectEncoding = System.Text.Encoding.GetEncoding(this.code);
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.GetEncoding(this.code);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from_addressee, from_pwd);
client.Host = _smtp;
client.Send(message); return 1;
}
catch (Exception ex)
{
Err = ex.Message;
return -1;
} }
这是我的发邮件代码,用126和企业邮箱一直没问题。
是不是你的smtp没设置对呢?