using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
namespace TestEmail
{
public class Email
{
private string title;
/// <summary>
/// 邮件标题(主题)
/// </summary>
public string Title
{
get { return title; }
set { title = value; }
}
private string from;
/// <summary>
/// 发件人(XXXX@XXX.XXX)
/// </summary>
public string From
{
get { return from; }
set { from = value; }
}
private string[] to;
/// <summary>
/// 收件人(XXXX@XXX.XXX)可多发
/// </summary>
public string[] To
{
get { return to; }
set { to = value; }
}
private string content;
/// <summary>
/// 邮件内容
/// </summary>
public string Content
{
get { return content; }
set { content = value; }
}
private string serverName;
/// <summary>
/// 邮件服务器的地址(stmp.XXX.XXX)
/// </summary>
public string ServerName
{
get { return serverName; }
set { serverName = value; }
}
private int serverPort;
/// <summary>
/// 邮件服务器的端口(一般为25)
/// </summary>
public int ServerPort
{
get { return serverPort; }
set { serverPort = value; }
}
private string[] attach;
/// <summary>
/// 附件地址
/// </summary>
public string[] Attach
{
get { return attach; }
set { attach = value; }
}
public bool IsSSL { get; set; }
/// <summary>
/// 发邮件的方法(bool ishaveAtt 表示是否有附件)
/// </summary>
public Boolean sendMail(string user, string password, bool ishaveAtt)
{
MailMessage mymail = new MailMessage();
mymail.From = new MailAddress(From);
foreach (string tmp in To)
{ mymail.To.Add(tmp); }
if (ishaveAtt == true)
{
foreach (string att in Attach)
{
Attachment atta = new Attachment(att);
mymail.Attachments.Add(atta);
}
}
mymail.IsBodyHtml = true;
mymail.Body = Content;
mymail.Subject = Title;
SmtpClient sc = new SmtpClient(ServerName, ServerPort);
sc.EnableSsl = IsSSL;
sc.Credentials = new System.Net.NetworkCredential(user, password);
try
{
sc.Send(mymail);
return true;
}
catch
{
return false;
}
}
}
}
可以参考一下啊
MailAddress in ASP.NET 2.0
http://hi.baidu.com/marsbook/blog/item/70dd206314b53d640d33fa0f.html
http://www.cnblogs.com/freshman0216/archive/2008/06/05/1213984.html
最主要是需要自己实践,找出问题的原因。
例子: http://www.cnblogs.com/files/freshman0216/CNBlogsDemo.rar