Asp.Net用SmtpClient发送邮件

ContractedBlock.gif ExpandedBlockStart.gif 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);
        }
    }
}

转载于:https://www.cnblogs.com/trampt/archive/2009/08/21/1551562.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值