发送邮件时出现附件问题 [英] attachment issue while sending mail

查看:94
本文介绍了发送邮件时出现附件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我无法在ASP.NET中发送SMTP邮件时发送附件.

Hi all,

Im not able to send the attachments while sending SMTP mails in ASP.NET.

using System.Web.Mail;


//在发送按钮事件中,我放置了此代码.


//In the send button event,i placed this code.

Server.ScriptTimeout = 1000;
        MailMessage msg = new MailMessage();
        msg.From = this.Txtfrom.Text;
        msg.To = this.TxtTo.Text;
        msg.Cc = this.TxtCC.Text;
        msg.Subject = this.Txtsub.Text;
        msg.Body = this.Txtbody.Text;

        if (filatchmnt1.PostedFile != null)
        {
            HttpPostedFile atchfile = filatchmnt1.PostedFile;
            int atchlen = atchfile.ContentLength;
            if (atchlen > 0)
            {
                strfilename = Path.GetFileName(filatchmnt1.PostedFile.FileName);
                filatchmnt1.PostedFile.SaveAs(Server.MapPath(strfilename));
                MailAttachment atch = new MailAttachment(Server.MapPath(strfilename));
                msg.Attachments.Add(atch);
                
            }
        }
        try
        {
            SmtpMail.Send(msg);
            MessageBox.Show("Mail has been sent to" + msg.To + '&' + msg.Cc);
          
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception:" + ex.Message);
        }
        Response.Flush();



错误名称strfilename在当前上下文中不存在;".
请帮忙..



The error "the name strfilename doesn''t exists in the current context;".
Kindly help..

推荐答案

声明strfilename 变量

并选中此复选框以通过电子邮件发送附件
使用SMTP服务器在ASP.NET中发送带有附件的电子邮件 [带有多个附件的ASP.NET电子邮件 [
Declare the strfilename variable

and check this to send attachments in email
Sending Email with attachment in ASP.NET using SMTP Server[^]

ASP.NET email with multiple attachments[^]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Data;
using System.Web.Mail;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Drawing;
using System.Xml; 

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
  System.Net.Mail.MailMessage obj = new System.Net.Mail.MailMessage();
          
            SmtpClient serverobj = new SmtpClient();
            serverobj.Credentials = new NetworkCredential("anilmeets4u@gmail.com", "########");
            serverobj.Port = 587;
            serverobj.Host = "smtp.gmail.com";
            serverobj.EnableSsl = false;
            obj = new System.Net.Mail.MailMessage();
            obj.From = new MailAddress("anilmeets4u@gmail.com", "AgileLearning.com", System.Text.Encoding.UTF8);
            obj.To.Add(ASPxtxtToUser.Text);
            obj.CC.Add(txtCcUser.Text);
            obj.Priority = System.Net.Mail.MailPriority.High;
            obj.Subject = txtSubject.Text;
            string date = DateTime.Now.ToString();
            obj.Body = ASPxMemo1.Text;
            HttpFileCollection hfc = Request.Files;
            for (int z = 0; z < hfc.Count; z++)
            {
                HttpPostedFile hpf = hfc[z];
                if (hpf.ContentLength > 0)
                {
                    uploadfile.SaveAs(Server.MapPath("MailFiles") + "\\" + Path.GetFileName(hpf.FileName));
                    string FileName = Server.MapPath("MailFiles") + "\\" + Path.GetFileName(hpf.FileName);

                    obj.Attachments.Add(new Attachment(FileName));
                    obj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;


                    serverobj.Send(obj);
                    Page.RegisterClientScriptBlock("pageClose", "<script>alert('Your Request Send Sucessfully');</script>");
                }
            }

           

        }


        catch (Exception ex)
        {
            ex.ToString();
        }


    }



试试这个将起作用



Try this it will work


这篇关于发送邮件时出现附件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆