首页

c#发送带附件的邮件 附源码

c#

2020-8-30

需要引用以下命名空间方可发送带附件的邮件

using System.Net;
using System.Net.Mail;
using System.Net.Mime

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;
using System.Net.Mime;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string file = Server.MapPath("testXML.xml");
        MailMessage myMail = new System.Net.Mail.MailMessage(); ;
        myMail = new System.Net.Mail.MailMessage(txtSender.Text.Trim(), txtReceiver.Text.Trim(),txtSubject.Text.Trim(),txtContent.Text.Trim());
        if (ddlAccessories.Items.Count > 0)
        {
            for (int i = 0; i < ddlAccessories.Items.Count; i  )
            {
                System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(Server.MapPath(ddlAccessories.Items[i].Value), System.Net.Mime.MediaTypeNames.Application.Octet);
                System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(file);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
                myMail.Attachments.Add(myAttachment);
            }
        }
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
        client.Credentials = new System.Net.NetworkCredential("wangxiaoke68", "sxwangxiaoke8568");
        client.Send(myMail);
    }
    protected void btnTake_Click(object sender, EventArgs e)
    {
        if (upFile.PostedFile.ContentLength != 0)
        {
            string clientFileName = upFile.PostedFile.FileName;
            string[] strTemp = clientFileName.Split('.');
            string upFileExp = strTemp[strTemp.Length - 1].ToString();
            string ServerFileName = DateTime.Now.ToString("yyyyMMddhhmmss")   "."   upFileExp;
            upFile.PostedFile.SaveAs(Server.MapPath(ServerFileName));
            ddlAccessories.Items.Add(new ListItem(clientFileName, ServerFileName));
            if (ddlAccessories.Items.Count > 0)
            {
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
        }
    }
}
资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
WebApplication1/Default.aspx , 7167
WebApplication1/Default.aspx.cs , 2700
WebApplication1/image/─┌╚▌.jpg , 52644
WebApplication1/image/▒Ω═╖.jpg , 45339
WebApplication1/image/╙╩╝■╖ó╦═╡╟┬╝╥│├µ.jpg , 148865
WebApplication1/image/┴┤╜╙.jpg , 15808
WebApplication1/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 8027
WebApplication1/obj/Debug/WebApplication1.csproj.FileListAbsolute.txt , 97
WebApplication1/Properties/AssemblyInfo.cs , 1350
WebApplication1/testXML.xml , 41
WebApplication1/Web.config , 311
WebApplication1/Web.Debug.config , 1245
WebApplication1/Web.Release.config , 1306
WebApplication1/WebApplication1.csproj , 5333
WebApplication1/WebApplication1.csproj.user , 1086
WebApplication1/WebApplication1.sln , 919
WebApplication1/WebApplication1.v11.suo , 20480
没有账号? 忘记密码?

社交账号快速登录