在ASP.net中发送电子邮件

ASP.net中发送电子邮件

 .net里可以使用System.Web.Mail下的类进行邮件发送操作,如果SMTP服务器在本地,那就相当简单,在用户验证登陆后,重载几个发送邮件的方法就可以发送

    public class MyMail
    {
        
private MyMail()
        {
        }
        
public static bool SendMail(string smtpserver,MailMessage sem)
        {
            
try
            {        
                SmtpMail.SmtpServer=smtpserver;
                SmtpMail.Send(sem);
            }
            
catch
            {
                
return false;
            }
            
return true;    
        }
        
public static bool SendMail(string smtpserver,string from,string to,string subject,string mestext)
        {
              
try
           {
                SmtpMail.SmtpServer=smtpserver;
                SmtpMail.Send(from,to,subject,mestext);
            }
             
catch
            {
               
return false;
            }
                 
return true;         
        }
        
public static bool SendMail(string smtpserver,string from,string to,string subject,string body,string bcc,string cc,MailFormat mf,MailPriority mp,MailAttachment []ma)
        {
            MailMessage sem=
new MailMessage();
            sem.From=from;
            sem.To=to;
            sem.Subject=subject;
            sem.Body=body;
            sem.Bcc=bcc;
            sem.Cc=cc;
            sem.BodyFormat=mf;
            sem.Priority=mp;
            
if(ma!=null)
            {
                
for(int i=0;i<ma.Length;i++)
                sem.Attachments.Add(ma[i]);
            }
            
try
            {  
                SmtpMail.SmtpServer=smtpserver;
                SmtpMail.Send(sem);
            }
            
catch
            {
                
return false;
            }
            
return true;    
        }
    }


       
SMTP服务器不在本地时候,就要用到第三方免费邮件提供商,但是现在一些免费邮件提供商不再提供对所有邮件提供这项服务,我试了几个也没有成功,而且在发送邮件的时候,因为没有验证用户信息,发出的信件会被很多服务器所拒收,所以这种方式也不太可行,这时候就可以调用outlook组件,很多CRM系统都是这么做的。

      
我们也可以使用JMail组件进行发送
      
为了测试
       
新建一个asp.net工程,添加Jmail引用,新建一个Mail,实际邮件类应该比这个属性多,这里只为测试用
 

public class Mail
    {
        
string  _smtpserver;
        
string  _from;
        
string  _to;
        
string  _subject;
        
string  _content;
        
string _username;
        
string _password;
        
string [] _attatchment;

        
public Mail(string  _smtpserver,string  _from,string  _to,string  _subject,string  _content,string _username,string _password,string [] _attatchment)
        {
            smtpserver=_smtpserver;
            from=_from;
            to=_to;
            subject=_subject;
            content=_content;
            username=_username;
            password=_password;
            attatchment=_attatchment;
        }
        
public string smtpserver
        {
            
get
            {
               
return _smtpserver;
            }
            
set
            {
                _smtpserver=value;
            }
        }
        
public string from
        {
            
get
            {
                
return _from;
            }
            
set
            {
                _from=value;
            }
        }
        
public string to
        {
            
get
            {
                
return _to;
            }
            
set
            {
                _to=value;
            }

        }
        
public string subject
        {
            
get
            {
                
return _subject;
            }
            
set
            {
                _subject=value;
            }
        }
        
public string content
        {
            
get
            {
                
return _content;
            }
            
set
            {
                _content=value;
            }
        }
        
public string username
        {
            
get
            {
                
return _username;
            }
            
set
            {
                _username=value;
            }
        }
        
public string password
        {
            
get
            {
                
return _password;;
            }
            
set
            {
                _password=value;
            }

        }
        
public string [] attatchment
        {
            
get
            {
                
return  _attatchment;
            }
            
set
            {
                _attatchment=value;
            }
        }

        }

    
  
添加一个MyJmail类负责处理发送工作

    public class MyJmail
    {
        
private MyJmail()
        {
            
        }
        
public static bool SendMail(EMAIL.Mail mymail)
    {
            
try
            {

                Message  sem=
new Message();
                sem.From=mymail.from;                
//发件人
                sem.AddRecipient(mymail.to," "," ");//收件人
                sem.MailServerPassWord=mymail.password;//登陆密码
                sem.MailServerUserName=mymail.username;//用户名
                sem.Subject=mymail.subject;//主题
                sem.Body=mymail.content;//内容
                if(mymail.attatchment.Length!=0)
                    
for(int i=0;i<mymail.attatchment.Length;i++)
                        sem.AddAttachment(mymail.attatchment[i],
true,null);//附件
                sem.Send(mymail.smtpserver,false);//发送
                sem.Close();
            }
            
catch
            {
                
return false;
            }
            
return true;
        }
        }


 
新建一个页面,添加按钮,添加事件,这里附加了两个附件

        private void Button1_Click(object sender, System.EventArgs e)
        {
            
string[] attachment={"C://test.txt","d://test.txt"};
            Mail send=
new Mail("smtp.sina.com.cn","sb058@sina.com","maxwolf_net@hotmail.com","你好","你好你好","******","*******",attachment);
            
if(!MyJmail.SendMail(send))
                Label1.Text="wrong";
        }

      点击发送,测试成功

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值