首页 > 开发 > 综合 > 正文

C#发邮件的代码 (vs2005)

2024-07-21 02:26:06
字体:
来源:转载
供稿:网友

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.net;
using system.net.mail;
public partial class sendemail : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {

    }
    protected void button1_click(object sender, eventargs e)
    {

        string s_from = textbox1.text.trim().tostring();
        string s_to = textbox3.text.trim().tostring();
        string pwd=textbox2.text .trim().tostring();
        string s_body =textbox5.text.trim().tostring();
        s_body = s_body.replace("/r/n","<br/>");
        s_body = s_body.replace("/n", "<br/>");
        s_body = s_body.replace(" ", "&nbsp");

        int i = s_from.indexof("@");
        string username = s_from.substring(0,i);


        mailaddress from = new mailaddress(s_from);
        mailaddress to = new mailaddress(s_to);
        mailmessage mailobj = new mailmessage(from ,to);
        mailobj.subject = textbox4.text.trim().tostring();
        mailobj.body = s_body;
        mailobj.isbodyhtml = true;
        mailobj.bodyencoding = system.text.encoding.getencoding("gb2312");
        mailobj.priority = mailpriority.high;

        if (fileupload1.postedfile.contentlength > 0)
        {
            mailobj.attachments.add(new attachment(  fileupload1.postedfile.filename));
        }

        smtpclient smtp = new smtpclient("smtp.163.com");
       // smtp.host = "smtp.163.com";
        smtp.usedefaultcredentials = false;
        smtp.credentials = new networkcredential(username,pwd);
        smtp.deliverymethod = smtpdeliverymethod.network;

        try {
            smtp.send(mailobj);
            response.write("ok!");
       
        }
        catch
       
        {
            response.write("sorry!");
        }
      
       
         }
}

////因为用的邮箱都是163的,所以就设置了smtp的地址是163的地址,如果用别的邮箱,只要smtp的地址改下就可以了.


 

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表