Asp.Net用SmtpClient发送邮件
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Runtime.Remoting;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace System.MvcHandle
{
public delegate void CheckUser(string str);
public class MVC
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private static bool IsConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
public static void Send(string body)
{
//判断网络是否连接成功
if (IsConnected())
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("trampt.net@gmail.com", "woshiwopengpeng@yahoo.com.cn");
mail.Body = body;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.Subject = "Send Email";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("xxx", "xxxx");
smtp.Timeout = 600000;
smtp.Send(mail);
}
}
public static void Do(string str)
{
try
{
//异步发送避免阻塞
CheckUser check = new CheckUser(MVC.Send);
check.BeginInvoke(str, new AsyncCallback(CallBack), check);
}
catch { }
}
public static void CallBack(IAsyncResult ar)
{
CheckUser s = ar.AsyncState as CheckUser;
s.EndInvoke(ar);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Runtime.Remoting;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace System.MvcHandle
{
public delegate void CheckUser(string str);
public class MVC
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
private static bool IsConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
public static void Send(string body)
{
//判断网络是否连接成功
if (IsConnected())
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("trampt.net@gmail.com", "woshiwopengpeng@yahoo.com.cn");
mail.Body = body;
mail.Priority = System.Net.Mail.MailPriority.High;
mail.Subject = "Send Email";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("xxx", "xxxx");
smtp.Timeout = 600000;
smtp.Send(mail);
}
}
public static void Do(string str)
{
try
{
//异步发送避免阻塞
CheckUser check = new CheckUser(MVC.Send);
check.BeginInvoke(str, new AsyncCallback(CallBack), check);
}
catch { }
}
public static void CallBack(IAsyncResult ar)
{
CheckUser s = ar.AsyncState as CheckUser;
s.EndInvoke(ar);
}
}
}