main.cs类
发送方法:
这个可以发送成功。
这里要注意的是,我用的邮箱是2004年注册的可以发送,但我用2009年注册的邮箱发送是不支持。
用QQ邮箱也不会支持,也就是说163对于新注册的用户(具体是以什么时间为分隔时间?)没有开放smtp功能,qq对所有用户也没有开放smtp功能。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace Common{
public partial class MailEntity
{
/**//// <summary>
/// /// default constructor
/// /// </summary>
public MailEntity()
{ }
/**//// <summary>
/// Send mail
/// </summary>
/// /// <param name="mailEntity">MailEntity object</param>
/// <returns></returns>
public bool SendMail(MailEntity mailEntity)
{
try
{
Encoding encoding = mailEntity.Encoding;
MailMessage mailMessage = new MailMessage(
new MailAddress(mailEntity.MailFrom, mailEntity.DisplaySenderName, encoding),
new MailAddress(mailEntity.MailTo, mailEntity.DisplayReceiverName, encoding)
);
mailMessage.SubjectEncoding = encoding;
mailMessage.Subject = mailEntity.Subject;
mailMessage.IsBodyHtml = mailEntity.IsBodyHtml;
mailMessage.Body = mailEntity.ContentBody;
SmtpClient smtpClient = new SmtpClient(mailEntity.Host);
smtpClient.Credentials = new NetworkCredential(mailEntity.UserName, mailEntity.Pwd);
smtpClient.Timeout = mailEntity.TimeOut;
smtpClient.Send(mailMessage);
return true;
}
catch(SmtpException sex)
{
Ex = sex.Message;
return false;
}
catch(Exception ex)
{
Ex = ex.Message;
return false;
}
}
Properties#region Properties
public string Ex
{
get; set;
}
/**//// <summary>
/// the email send timeout
/// </summary>
public Int32 TimeOut { set; get; }
/**//// <summary>
/// the email address that will send the email
/// </summary>
public String MailFrom { set; get; }
/**//// <summary>
/// the email address that will receive the email
/// </summary>
public String MailTo { set; get; }
/**//// <summary>
/// the Encoding type that will edcode the email content
/// </summary>
public Encoding Encoding { set; get; }
/**//// <summary>
/// the name of the user who send the email
/// </summary>
public String UserName { set; get; }
/**//// <summary>
/// the password of the user who send the email
/// </summary>
public string Pwd { get; set; }
/**//// <summary>
/// the sender name that will display in the sender item
/// </summary>
public String DisplaySenderName { set; get; }
/**//// <summary>
/// the receiver name that will display in the receiver item
/// </summary>
public String DisplayReceiverName { set; get; }
/**//// <summary>
/// mail content
/// </summary>
public String ContentBody { set; get; }
/**//// <summary>
/// mail subject
/// </summary>
public String Subject { set; get; }
/**////<summary>
/// mail host address
/// </summary>
public String Host { set; get; }
/**////<summary>
/// if the mail body is in html format
/// </summary>
public Boolean IsBodyHtml { set; get; }
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
namespace Common{
public partial class MailEntity
{
/**//// <summary>
/// /// default constructor
/// /// </summary>
public MailEntity()
{ }
/**//// <summary>
/// Send mail
/// </summary>
/// /// <param name="mailEntity">MailEntity object</param>
/// <returns></returns>
public bool SendMail(MailEntity mailEntity)
{
try
{
Encoding encoding = mailEntity.Encoding;
MailMessage mailMessage = new MailMessage(
new MailAddress(mailEntity.MailFrom, mailEntity.DisplaySenderName, encoding),
new MailAddress(mailEntity.MailTo, mailEntity.DisplayReceiverName, encoding)
);
mailMessage.SubjectEncoding = encoding;
mailMessage.Subject = mailEntity.Subject;
mailMessage.IsBodyHtml = mailEntity.IsBodyHtml;
mailMessage.Body = mailEntity.ContentBody;
SmtpClient smtpClient = new SmtpClient(mailEntity.Host);
smtpClient.Credentials = new NetworkCredential(mailEntity.UserName, mailEntity.Pwd);
smtpClient.Timeout = mailEntity.TimeOut;
smtpClient.Send(mailMessage);
return true;
}
catch(SmtpException sex)
{
Ex = sex.Message;
return false;
}
catch(Exception ex)
{
Ex = ex.Message;
return false;
}
}
Properties#region Properties
public string Ex
{
get; set;
}
/**//// <summary>
/// the email send timeout
/// </summary>
public Int32 TimeOut { set; get; }
/**//// <summary>
/// the email address that will send the email
/// </summary>
public String MailFrom { set; get; }
/**//// <summary>
/// the email address that will receive the email
/// </summary>
public String MailTo { set; get; }
/**//// <summary>
/// the Encoding type that will edcode the email content
/// </summary>
public Encoding Encoding { set; get; }
/**//// <summary>
/// the name of the user who send the email
/// </summary>
public String UserName { set; get; }
/**//// <summary>
/// the password of the user who send the email
/// </summary>
public string Pwd { get; set; }
/**//// <summary>
/// the sender name that will display in the sender item
/// </summary>
public String DisplaySenderName { set; get; }
/**//// <summary>
/// the receiver name that will display in the receiver item
/// </summary>
public String DisplayReceiverName { set; get; }
/**//// <summary>
/// mail content
/// </summary>
public String ContentBody { set; get; }
/**//// <summary>
/// mail subject
/// </summary>
public String Subject { set; get; }
/**////<summary>
/// mail host address
/// </summary>
public String Host { set; get; }
/**////<summary>
/// if the mail body is in html format
/// </summary>
public Boolean IsBodyHtml { set; get; }
#endregion
}
}
发送方法:
Code
private void button1_Click(object sender, EventArgs e)
{
Common.MailEntity mailEntity = new Common.MailEntity();
mailEntity.MailFrom = "[email protected]";
mailEntity.UserName = "test";
mailEntity.Pwd = "123456";
mailEntity.MailTo = "[email protected]";
mailEntity.DisplayReceiverName = "test";
mailEntity.DisplaySenderName = "test";
mailEntity.Subject = "Subject test";
mailEntity.ContentBody = " ContentBody Text";
mailEntity.Host = "smtp.163.com";
mailEntity.TimeOut = 999999;
mailEntity.Encoding = Encoding.GetEncoding("GB2312"); ;
mailEntity.IsBodyHtml = true;
bool ma= mailEntity.SendMail(mailEntity);
MessageBox.Show(ma.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
Common.MailEntity mailEntity = new Common.MailEntity();
mailEntity.MailFrom = "[email protected]";
mailEntity.UserName = "test";
mailEntity.Pwd = "123456";
mailEntity.MailTo = "[email protected]";
mailEntity.DisplayReceiverName = "test";
mailEntity.DisplaySenderName = "test";
mailEntity.Subject = "Subject test";
mailEntity.ContentBody = " ContentBody Text";
mailEntity.Host = "smtp.163.com";
mailEntity.TimeOut = 999999;
mailEntity.Encoding = Encoding.GetEncoding("GB2312"); ;
mailEntity.IsBodyHtml = true;
bool ma= mailEntity.SendMail(mailEntity);
MessageBox.Show(ma.ToString());
}
这个可以发送成功。
这里要注意的是,我用的邮箱是2004年注册的可以发送,但我用2009年注册的邮箱发送是不支持。
用QQ邮箱也不会支持,也就是说163对于新注册的用户(具体是以什么时间为分隔时间?)没有开放smtp功能,qq对所有用户也没有开放smtp功能。