c#求最精辟代码的邮件发送代码!只要能实现发送就可以!

用.Net.Mail,谢谢了!晚上请吃饭
求最简短的邮件发送代码!只要能实现发送就可以! 不需要什么大的功能,只要能发送
哎呀妈呀!太简短了 ,谢谢昂!

/// <summary>
/// Discuz!NT邮件发送类的调用封装类
/// </summary>
public class Emails
{
protected static GeneralConfigInfo configinfo = GeneralConfigs.GetConfig();

protected static ISmtpMail ESM;

protected static EmailConfigInfo emailinfo = EmailConfigs.GetConfig();

static Emails()
{
if (emailinfo.DllFileName.ToLower().IndexOf(".dll") <= 0)
{
emailinfo.DllFileName = emailinfo.DllFileName + ".dll";
}
try
{
//读取相应的DLL信息
Assembly asm = Assembly.LoadFrom(HttpRuntime.BinDirectory + emailinfo.DllFileName);
ESM = (ISmtpMail)Activator.CreateInstance(asm.GetType(emailinfo.PluginNameSpace, false, true));
}
catch
{
try
{
//读取相应的DLL信息
Assembly asm = Assembly.LoadFrom(Utils.GetMapPath("/bin/" + emailinfo.DllFileName));
ESM = (ISmtpMail)Activator.CreateInstance(asm.GetType(emailinfo.PluginNameSpace, false, true));
}
catch
{
ESM = new SmtpMail();
}
}
}

//重设置当前邮件发送类的实例对象
public static void ReSetISmtpMail()
{
try
{
emailinfo = EmailConfigs.GetConfig();
//读取相应的DLL信息
Assembly asm = Assembly.LoadFrom(HttpRuntime.BinDirectory + emailinfo.DllFileName);
ESM = (ISmtpMail)Activator.CreateInstance(asm.GetType(emailinfo.PluginNameSpace, false, true));
}
catch
{
try
{
//读取相应的DLL信息
Assembly asm = Assembly.LoadFrom(Utils.GetMapPath("/bin/" + emailinfo.DllFileName));
ESM = (ISmtpMail)Activator.CreateInstance(asm.GetType(emailinfo.PluginNameSpace, false, true));
}
catch
{
ESM = new SmtpMail();
}
}
}

/// <summary>
/// 定义邮件内容函数
/// </summary>
/// <param name="UserName">用户名</param>
/// <param name="Email">EMAIL地址</param>
/// <param name="pass">相应注册用户的密码(暂时无设置)</param>
/// <returns></returns>
public static bool DiscuzSmtpMail(string UserName, string Email, string pass)
{
string forumurl = "http://" + DNTRequest.GetCurrentFullHost() + BaseConfigs.GetForumPath.ToLower();

try
{
ESM.RecipientName = UserName;//设定收件人姓名
ESM.AddRecipient(Email);//设定收件人地址(必须填写)。
ESM.MailDomainPort = emailinfo.Port;
ESM.From = emailinfo.Sysemail;
ESM.FromName = configinfo.Webtitle;
ESM.Html = true;
ESM.Subject = "已成功创建你的 " + configinfo.Forumtitle + "帐户,请查收.";

StringBuilder body = new StringBuilder();
body.Append(emailinfo.Emailcontent.Replace("{webtitle}", configinfo.Webtitle));
body.Replace("{weburl}", "<a href=" + configinfo.Weburl + ">" + configinfo.Weburl + "</a>");
body.Replace("{forumurl}", "<a href=" + forumurl + ">" + forumurl + "</a>");
body.Replace("{forumtitle}", configinfo.Forumtitle);

ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "</pre>";
ESM.MailDomain = emailinfo.Smtp;
ESM.MailServerUserName = emailinfo.Username;
ESM.MailServerPassWord = emailinfo.Password;

//开始发送
return ESM.Send();
}
catch
{
return false;
}
}

/// <summary>
/// 定义邮件内容函数
/// </summary>
/// <param name="UserName">用户名</param>
/// <param name="Email">EMAIL地址</param>
/// <param name="pass">相应注册用户的密码(暂时无设置)</param>
/// <param name="authstr">相应注册用户的激活链接串参数</param>
/// <returns></returns>
public static bool DiscuzSmtpMail(string UserName, string Email, string pass, string authstr)
{
string forumurl = "http://" + DNTRequest.GetCurrentFullHost() + BaseConfigs.GetForumPath.ToLower();

try
{
ESM.RecipientName = UserName;//设定收件人姓名
ESM.AddRecipient(Email);
ESM.MailDomainPort = emailinfo.Port;
ESM.From = emailinfo.Sysemail;
ESM.FromName = configinfo.Webtitle;
ESM.Html = true;
ESM.Subject = "已成功创建你的 " + configinfo.Forumtitle + "帐户,请查收.";

StringBuilder body = new StringBuilder();
body.Append(emailinfo.Emailcontent.Replace("{webtitle}", configinfo.Webtitle));
body.Replace("{weburl}", "<a href=" + configinfo.Weburl + ">" + configinfo.Weburl + "</a>");
body.Replace("{forumurl}", "<a href=" + forumurl + ">" + forumurl + "</a>");
body.Replace("{forumtitle}", configinfo.Forumtitle);

ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "\r\n\r\n" + "激活您帐户的链接为:<a href=" + forumurl + "activationuser.aspx?authstr=" + authstr.Trim() + " target=_blank>" + forumurl + "activationuser.aspx?authstr=" + authstr.Trim() + "</a></pre>";
ESM.MailDomain = emailinfo.Smtp;
ESM.MailServerUserName = emailinfo.Username;
ESM.MailServerPassWord = emailinfo.Password;

return ESM.Send();
}
catch
{
return false;
}
}

/// <summary>
/// 邮件通知服务
/// </summary>
/// <param name="Email">email地址</param>
/// <param name="title">邮件的标题</param>
/// <param name="body">邮件内容</param>
/// <returns></returns>
public static bool SendEmailNotify(string Email, string title, string body)
{
try
{

ESM.AddRecipient(Email);
ESM.MailDomainPort = emailinfo.Port;
ESM.From = emailinfo.Sysemail;//设定发件人地址(必须填写)
ESM.FromName = configinfo.Webtitle;
ESM.Html = true;//设定正文是否HTML格式。
ESM.Subject = title;

ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "</pre>";
ESM.MailDomain = emailinfo.Smtp;
ESM.MailServerUserName = emailinfo.Username;
ESM.MailServerPassWord = emailinfo.Password;

//开始发送
return ESM.Send();
}
catch
{
return false;
}
}

/// <summary>
/// 向指定用户发送email,每次最多能给100人发邮件
/// </summary>
/// <param name="usernamelist">用户名列表</param>
/// <param name="subject">主题</param>
/// <param name="body">内容</param>
/// <returns></returns>
public static string SendMailToUsers(string usernamelist, string subject, string body)
{
DataTable dt = DatabaseProvider.GetInstance().GetMailTable(usernamelist);
if (dt.Rows.Count < 1)
{
return "";
}

Thread[] lThreads = new Thread[dt.Rows.Count];

int count = 0;
int percount = 5;
StringBuilder result = new StringBuilder();

int successfulCount = 0;
foreach (DataRow dr in dt.Rows)
{
if (successfulCount > 100)
break;
EmailMultiThread emt = new EmailMultiThread(dr["username"].ToString().Trim(), dr["email"].ToString().Trim(), subject, body);
lThreads[count] = new Thread(new ThreadStart(emt.Send));
lThreads[count].Start();

if (count >= percount)
{
Thread.Sleep(5000);
count = 0;
}
result.Append(dr["uid"].ToString());
result.Append(",");
successfulCount++;
count++;
}
return result.ToString().Trim().Remove(result.Length - 1);
}

/// <summary>
/// 用户邮件发送
/// </summary>
/// <param name="Email">email地址</param>
/// <param name="title">邮件的标题</param>
/// <param name="body">邮件内容</param>
/// <returns></returns>
public static bool DiscuzSmtpMailToUser(string Email, string title, string body)
{

try
{
ESM.AddRecipient(Email);

ESM.MailDomainPort = emailinfo.Port;
ESM.From = emailinfo.Sysemail;//设定发件人地址(必须填写)
ESM.FromName = configinfo.Webtitle;
ESM.Html = true;//设定正文是否HTML格式。
ESM.Subject = title;

ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + body.ToString() + "</pre>";
ESM.MailDomain = emailinfo.Smtp;//"mail.discuz.com";//"smtp.163.com";
//也可将将SMTP信息一次设置完成。写成
ESM.MailServerUserName = emailinfo.Username;
ESM.MailServerPassWord = emailinfo.Password;

//开始发送
return ESM.Send();
}
catch
{
return false;
}
}

}

//多线程发送邮箱类
public class EmailMultiThread : Emails
{

private string m_username = "";

private string m_email = "";

private string m_title = "";

private string m_body = "";

private bool m_issuccess = false;

public string UserName
{
get { return m_username; }
}

public string Email
{
get { return m_email; }
}

public string Title
{
get { return m_title; }
}

public string Body
{
get { return m_body; }
}

public bool IsSuccess
{
get { return m_issuccess; }
set { m_issuccess = value; }
}

public EmailMultiThread(string UserName, string Email, string Title, string Body)
{
m_username = UserName;
m_email = Email;
m_title = Title;
m_body = Body;
}

public void Send()
{
lock (emailinfo)
{
try
{
ESM.MailDomainPort = emailinfo.Port;
ESM.AddRecipient(this.Email);
ESM.RecipientName = this.UserName;//设定收件人姓名

ESM.From = emailinfo.Sysemail;
ESM.FromName = configinfo.Webtitle;
ESM.Html = true;
ESM.Subject = this.Title;
ESM.Body = "<pre style=\"width:100%;word-wrap:break-word\">" + this.Body.ToString() + "</pre>";
ESM.MailDomain = emailinfo.Smtp;
ESM.MailServerUserName = emailinfo.Username;
ESM.MailServerPassWord = emailinfo.Password;

//开始发送
this.IsSuccess = ESM.Send();

}
catch
{ }

}
Thread.CurrentThread.Abort();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-06-01
确保你的主机安装了smtp服务,并在你的程序中引用了System.web然后调用下面的方法:

//此方法同时适用于Windows App与Web App

using System.Security.Permissions;
using System.Web.Mail;

[PermissionSet(SecurityAction.LinkDemand, Unrestricted=true)]
private void Send()
{
MailMessage message1;

message1.From = "abc@gmail.com"
message1.To = "def@microsoft.com";

message1.BodyFormat = MailFormat.Html;
message1.Body = "This is body text";

System.Web.Mail.SmtpMail.Send(message1);
}
第2个回答  2009-06-10
private void toolStripButton1_Click(object sender, EventArgs e)
{
//发送过程

if (this.textBox1.Text.Length < 1 | this.textBox2.Text.Length< 1 | this.textBox3.Text.Length < 1)
{
MessageBox.Show("必须填写收件人地址,发件人地址和内容", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

MailMessage MyMessage = new MailMessage();

MyMessage.Priority = MailPriority.Normal;
if (this.radioButton1.Checked)
MyMessage.Priority = MailPriority.High;
if (this.radioButton3.Checked)
MyMessage.Priority = MailPriority.Low;

MyMessage.IsBodyHtml = false;
if (this.hTML格式ToolStripMenuItem.Checked)
MyMessage.IsBodyHtml = true;

string[] mailTo = this.textBox1.Text.Split(',');
for (int i = 0; i < mailTo.Length; i++)
MyMessage.To.Add(mailTo[i]);

MyMessage.From = new MailAddress(this.textBox2.Text);

MyMessage.Subject = this.textBox3.Text;

if (this.hTML格式ToolStripMenuItem.Checked)
{
MyMessage.Body = this.richTextBox1.Text;
}
else
{
MyMessage.Body = this.textBox4.Text; ;
}

MyMessage.BodyEncoding = System.Text.Encoding.UTF8;

for (int i = 0; i < listView1.Items.Count; i++)
{
Attachment MyAttach = new Attachment(listView1.Items[i].Tag.ToString());
MyMessage.Attachments.Add(MyAttach);
}

DataProcess dp = new DataProcess();
dp.SqlString = "select account,password,smtp from mailUser where email = '" + textBox2.Text.Trim()+"'";
DataTable dt = dp.find();
string smtpserver = dt.Rows[0].ItemArray[2].ToString() ;
string myAccount = dt.Rows[0].ItemArray[0].ToString();
string myPassword = dt.Rows[0].ItemArray[1].ToString();

try
{
SmtpClient smtpclient = new SmtpClient(smtpserver,25);
smtpclient.Credentials = new NetworkCredential(myAccount,myPassword);
smtpclient.Send(MyMessage);

//把已发送邮件存入邮箱
string time = DateTime.Now.ToString();
string att = "";
for (int i = 0; i < listView1.Items.Count; i++)
{
att = att + listView1.Items[i].Tag.ToString() + ",";
}
string body = "";
if (this.hTML格式ToolStripMenuItem.Checked)
{
body = this.richTextBox1.Text;
}
else
{
body = this.textBox4.Text; ;
}

dp.SqlString = String.Format("insert into [mailSend]([from],[to],[subject],[body],[time],[att]) values('{0}','{1}','{2}','{3}','{4}','{5}')",
textBox2.Text.Trim(), textBox1.Text.Trim(), textBox3.Text.Trim(), body, time, att);

dp.execute();

//检查草稿箱,如果有已发送相关邮件,则删除
dp.SqlString = "select * from mailDraft where id = " + flag.ToString();
dt = dp.find();
if (dt.Rows.Count == 1)
{
dp.SqlString = "delete from mailDraft where id = " + flag.ToString();
dp.execute();
}

MessageBox.Show("邮件发送成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

c#求最精辟代码的邮件发送代码!只要能实现发送就可以!
ESM = (ISmtpMail)Activator.CreateInstance(asm.GetType(emailinfo.PluginNameSpace, false, true));} catch { ESM = new SmtpMail();} } } \/\/重设置当前邮件发送类的实例对象 public static void ReSetISmtpMail(){ try { emailinfo = EmailConfigs.GetConfig();\/\/读取相应的DLL信息 Assembly...

...发送邮件代码。不需要smtp服务器的那种。就是只要知道对方邮箱,然后...
public static bool SendSmtpMail(string strto,string strSubject,string strBody,string password,string From){ string strSmtpServer = "smtp.163.com";\/\/(用的是163的邮箱,如果要用其他邮箱自己改)string strFrom = From;string strFromPass =password;SmtpClient client =new SmtpClient(strSmtp...

C#中如何通过写好的程序向局域网内的其他电脑发送文件?
如果不在被控机器上装软件,你可以在被控机器上开一个共享文件夹,然后给你写入文件权限就可以。对方只要电脑开机就可以传输。通过域控进行管理,进行文件传输。

关于附件的上传和下载
若非如此就很难在传输过程中出现编码截位的问题,导致收信方出现乱码,特别是中文内码的文字,属于8位代码,并非标准的ASCII码形式,由于国内通行的大部分邮件服务器都能够处理GB内码文件,所以可以直接传送文件而不需要编码,但如果要将中文邮件发到国外或在某些不支持8位(非标准ASCII码格式)的某些邮件主机上传输,就会产生...

求一个很基础的c#代码!!万分感谢!!!
求一个很基础的c#代码!!万分感谢!!! 我是c#的初学者,老师要求编出一段代码,可以计算形如654*6+65-27\/654的多项式,多项式不能有括号,请大侠帮忙了!!老师的要求就是这个啊!写计算器不对啊!... 我是c#的初学者,老师要求编出一段代码,可以计算形如654*6+65-27\/654的多项式,多项式不能有括号,请大侠...

C#自动生成编号,代码求助
第一,你把部门做成一棵树,当你点击树的时候把编号取下来,如:销售部的:编号为:XS-000001 把此编号存下来,然后把前面的 ( int index = txtTypeId.Text.IndexOf('-'); string msg= txtTypeId.Text.Substring(0,index+1);\/\/截取编号)取出来的就是“XS-”截取下来,把这个值传给一个...

利用C#实现任务栏通知窗口
托管代码就可以调用在动态链接库中实现的非托管函数 并可以封送其参数 我们可以轻松的显示但不获得焦点的窗体 程序中用到的Windows API以及常量的定义都保存在WinUser h头文件中 其对应的动态链接库文件就是user dll 使用 Net框架提供的 DllImportAttribute类对导入的函数进行定义 然后就可以非常方便的在程序中调用该...

c# 求段判断task运行状态的代码
private void Form1_Load(object sender, EventArgs e) { Task task = new Task(test); if (!task.IsCompleted) { task.Start(); } } void test() { Console.WriteLine("test"); }只要task是非完成状态,都会启动 ...

C#接口中只能包含方法签名,那方法的具体实现应该写在哪?
1、C#接口的作用 :C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正认识到接口的作用的时候就觉得用接口是多此一举,当然你这样想那是绝对绝对错误的,比尔盖茨的微软...

C# Socket发送DataSet问题
分批发送, 在客户端中显示上一页和下一页让用户选择浏览,就像QQ相册一样 一次只显示10多张图片。点击下一页时,再从数据库里选择。不过这样要写一个好存储过程。我随便给你写一个吧:CREATE PROCEDURE Selects PageSize int,\/\/定义一次要选择多少个数据 PageSkip int \/\/每选择一次要跳过多少项 ...

相似回答