当前位置: 首页>>代码示例>>C#>>正文


C# Attachment.Dispose方法代码示例

本文整理汇总了C#中Attachment.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Attachment.Dispose方法的具体用法?C# Attachment.Dispose怎么用?C# Attachment.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Attachment的用法示例。


在下文中一共展示了Attachment.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EmailAlert

    public static bool EmailAlert(string to_add, string from_add, string em_sub, string em_bd, string smtp_server = null, string sAttachment = null)
    {
        int i = 0;
        string[] sTempA = null;
        SmtpClient SmtpMail = new SmtpClient();
        MailMessage MailMsg = new MailMessage();

        sTempA = to_add.Split(',');

        try
        {
            for (i = 0; i < (sTempA.Length -1); i++)
            {
                MailMsg.To.Add(new MailAddress(sTempA[i]));
            }

            MailMsg.From = new MailAddress(from_add);
            MailMsg.Subject = em_sub.Trim();
            MailMsg.Body = em_bd.Trim() + Environment.NewLine;

            if (sAttachment != null)
            {
                Attachment MsgAttach = new Attachment(sAttachment);
                MailMsg.Attachments.Add(MsgAttach);

                if (smtp_sender == null)
                {
                    // default
                    SmtpMail.Host = "";
                }
                else
                {
                    SmtpMail.Host = smtp_sender;
                }
                SmtpMail.Send(MailMsg);
                MsgAttach.Dispose();
            }
            else
            {
                SmtpMail.Host = smtp_sender;
                SmtpMail.Send(MailMsg);
            }
            return true;
        }
        catch (Exception ex)
        {
            sTempA = null;
            SmtpMail.Dispose();
            MailMsg.Dispose();
            //Console.WriteLine(ex);
            return false;
        }
    }
开发者ID:pchnet,项目名称:SQLServer,代码行数:53,代码来源:al.cs

示例2: CreateMessageWithAttachment

    public static void CreateMessageWithAttachment(string filePath,string EmailTo,string EmailFrom,string Subject,string Body)
    {
        // Specify the file to be attached and sent.
        // This example assumes that a file named Data.xls exists in the
        // current working directory.

        // Create a message and set up the recipients.
        //MailMessage message = new MailMessage(EmailFrom,EmailTo,Subject,Body);
        MailMessage message = new MailMessage();
        message.From = new MailAddress(EmailFrom);
        message.Subject = Subject;
        message.Body = Body;
        string mTo =EmailTo;
        if (mTo != "" || mTo != string.Empty)
        {
            string[] strTo = mTo.Split(';');
            foreach (string strThisTo in strTo)
            {
                strThisTo.Trim();
                message.To.Add(strThisTo);
            }
        }

        // Create  the file attachment for this e-mail message.
        Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
        disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
        // Add the file attachment to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient("smtp.bizmail.yahoo.com", 25);
        // Add credentials if the SMTP server requires them.
        client.Credentials = new System.Net.NetworkCredential("[email protected]", "VtsApps");
        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            SendMailException = ex.StackTrace;
        }
        // Display the values in the ContentDisposition for the attachment.

        data.Dispose();
    }
开发者ID:manivts,项目名称:impexcubeapp,代码行数:49,代码来源:SendMail.cs

示例3: Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder saPage = new StringBuilder();

        if (!this.Context.Request.IsLocal)
        {
            saPage.Append(LoadError(ERR_NOT_LOCAL));
        }
        else
        {
            if (!this.Page.IsPostBack)
            {
                SmtpClient sendClient = null;
                MailMessage message = null;
                Attachment attach = null;

                try
                {
                    sendClient = new SmtpClient(Page.Session["smtpServer"].ToString(), Convert.ToInt32(Page.Session["smtpPort"].ToString()));
                    message = new MailMessage(Page.Session["emailAddressFrom"].ToString(), Page.Session["emailAddressTo"].ToString(), "From EkStatus", "");
                    attach = new Attachment(HttpContext.Current.Server.MapPath("") + "\\EkStatusOutput.htm");

                    message.Body = "Comments from sender:\r\n";

                    if (Page.Session["emailComments"] != null)
                    {
                        message.Body += Page.Session["emailComments"].ToString();
                    }

                    message.Body += "\r\n\r\nReport attached from " + Server.MachineName + "\r\n\r\nPlease save this report in workarea\\diagnostics\\ directory.";

                    if (Page.Session["smtpUser"].ToString() != "Value not set in configuration")
                    {
                        NetworkCredential netCred = new NetworkCredential(Page.Session["smtpUser"].ToString(), Page.Session["smtpPass"].ToString());
                        sendClient.Credentials = netCred;
                        netCred = null;
                    }

                    message.Attachments.Add(attach);
                    sendClient.Send(message);
                    m_bMailSentOk = true;
                }
                catch (Exception exThrown)
                {
                    m_sLastError = exThrown.Message;
                    m_bMailSentOk = false;
                }
                finally
                {

                    //----- Cleanup
                    if (message != null)
                    {
                        message.Dispose();
                        message = null;
                    }
                    if (attach != null)
                    {
                        attach.Dispose();
                        attach = null;
                    }
                    if (sendClient != null)
                    {
                        sendClient = null;
                    }
                }
            }
        }

        saPage.Append(LoadHeader());

        if(m_bMailSentOk)
            saPage.AppendLine("       <p>Mail sent successfully.&nbsp;&nbsp;You will be redirected in 5 seconds.</p>");
        else
            saPage.Append(LoadError(ERR_DEFAULT, m_sLastError));

        saPage.Append(LoadFooter());

        if (Page.Session["smtpServer"] != null)
            Page.Session.Remove("smtpServer");
        if (Page.Session["smtpPort"] != null)
            Page.Session.Remove("smtpPort");
        if (Page.Session["emailAddressTo"] != null)
            Page.Session.Remove("emailAddressTo");
        if (Page.Session["emailAddressFrom"] != null)
            Page.Session.Remove("emailAddressFrom");
        if (Page.Session["emailComments"] != null)
            Page.Session.Remove("emailComments");

        this.Response.Write(saPage.ToString());
    }
开发者ID:jaytem,项目名称:minGit,代码行数:91,代码来源:sendMail.aspx.cs


注:本文中的Attachment.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。