asp.net(2.0) 发送邮件

本文介绍了一种利用C#实现从QQ邮箱发送带有附件的HTML格式邮件的方法。通过具体的代码示例展示了如何设置邮件内容、附件及SMTP认证等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    
int i = 0;
    
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 = TextBox2.Text.Trim().ToString();//收件人
        string s_body = TextBox4.Text.Trim().ToString();//邮件内容
        s_body = s_body.Replace(" ""<br/>");
        s_body 
= s_body.Replace(" ""<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 
= TextBox3.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.qq.com");
        smtp.UseDefaultCredentials 
= false;
        smtp.Credentials 
= new NetworkCredential("270332289","f5sd1e");
        smtp.DeliveryMethod 
= SmtpDeliveryMethod.Network;

        
try
        
{
            smtp.Send(mailobj);
            Response.Write(
"ok!");

        }

        
catch
        
{
            Response.Write(
"sorry!");
        }

    }

}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值