Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I got the error Invalid mail attachment '1.xml' at attachment line please .
Thanks


C#
using System;
using System.IO;
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.Web.Mail;


public partial class Web_careers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // clearfields();
    }
    public void clearfields()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtcomment.Text = "";
        txtapply.Text = "";
    }

    protected void imgsubmit_Click(object sender, ImageClickEventArgs e)
    {
      
        string sHost = System.Configuration.ConfigurationManager.AppSettings["Host"].ToString();
        string sUserName = System.Configuration.ConfigurationManager.AppSettings["AutorptUser"].ToString();
        string sPassword = System.Configuration.ConfigurationManager.AppSettings["AutorptPassword"].ToString();
        string sFromEmail = System.Configuration.ConfigurationManager.AppSettings["AutorptFromEmail"].ToString();
        int    nPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString());
        bool fSSL = true;

        string MessageBody = "";
        string result = "";
        try
        {
            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
            if (fSSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (sUserName.Length == 0)
            {
                //Ingen auth 
            }
            else
            {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
            }
           
            string file = fuResume.PostedFile.FileName;
            MessageBody = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("../web/careers.txt"));

            MessageBody = MessageBody.Replace("$name$", txtName.Text);
            MessageBody = MessageBody.Replace("$email$", txtEmail.Text);
            MessageBody = MessageBody.Replace("$ApplyFor$", txtapply.Text);
            MessageBody = MessageBody.Replace("$coments$", txtcomment.Text);
            MessageBody = MessageBody.Replace("$Attachment$", file);
           

           
            System.Web.Mail.MailAttachment attachment;
            attachment = new System.Web.Mail.MailAttachment(file);

            Mail.To = txtEmail.Text;
            Mail.From = sFromEmail;
            Mail.Subject = "Enquiry";
            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            Mail.Body = MessageBody;

            Mail.Attachments.Add(attachment);


            System.Web.Mail.SmtpMail.SmtpServer = sHost;
            System.Web.Mail.SmtpMail.Send(Mail);
            result = "MailSuccess";
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
    }
}
Posted
Updated 3-Dec-11 0:12am
v2
Comments
uspatel 3-Dec-11 6:13am    
Pre tag added

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900