Announcement

Collapse
No announcement yet.

Net.Mail script help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Net.Mail script help

    I don't know if anyone can help with scripts buit I am trying to get this to work, it fails every time, not sure if it's a permissions thing. Password has been removed for obvious reasons, but correct one is in script on server.

    Any ideas anyone ??

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp.text.pdf;
    using System.IO;
    using System.Net.Mail;
    
    namespace RetrieveForm
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string id = Session.SessionID;
                string fileName = id + ".pdf";
                FileStream sw = new FileStream(Server.MapPath(fileName), FileMode.CreateNew);
                string file = Server.MapPath(fileName);
                PdfStamper ps = null;
                try
                {
                    // read existing PDF document
                    PdfReader r = new PdfReader(
                        // optimize memory usage
                      new RandomAccessFileOrArray(Server.MapPath("application.pdf")), null
                    );
                     ps = new PdfStamper(r, sw);
                    // retrieve properties of PDF form w/AcroFields object
                    AcroFields af = ps.AcroFields;
                    // fill in PDF fields by parameter:
                    // 1. field name
                    // 2. text to insert
                    foreach (string key in Request.Form.Keys)
                    {
                        string v = Request.Form[key];
                        if (af.Fields.Contains(key))
                        {
                            af.SetField(key, v);
                        }
                    }
                    // make resultant PDF read-only for end-user
                    ps.FormFlattening = true;
                    // forget to close() PdfStamper, you end up with
                    // a corrupted file!
                    ps.Close();
                    sw.Close();
                    sendEmail(file);
                    //File.Delete(file);
                    Response.Write("<br/><h3>Your application was successfuly sent!<br/>Thanks.</h3>");
                }
                catch (Exception err) { Response.Write(err.Message);  }
                finally { if (ps != null) ps.Close(); }
            
            }
    
            private void sendEmail(string attachment)
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.0-o-0.co.uk"; //Or Your SMTP Server Address 
                smtp.Credentials = new System.Net.NetworkCredential ("[email protected]", "BLANKEDOUT"); 
                smtp.EnableSsl = true;
    
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("[email protected]", "");
                mailMessage.To.Add(new MailAddress("[email protected]", ""));
                mailMessage.Subject = "Application submited";
                mailMessage.Body = "Please find attached the PDF filled.";
                mailMessage.Attachments.Add(new Attachment(attachment));
                smtp.Send(mailMessage);
            }
    
        }
    }

  • #2
    Good morning

    What do you mean by "it fails every time, not sure if it's a permissions thing".

    Do you get a run time error, is the email created but not sent or something else?

    Some Ideas whilst we are waiting.

    Is there any special reason why you are specifying credentials, in VB.Net I have
    DIm mcMailClient As SmtpClient
    ..
    mcMailClient.UseDefaultCredentials = True
    ..

    I have also found that if the from email address is not a valid account on the domain the email is never sent, is there anything in \inetpub\mailroot\BadMail?

    In my webconfig file I have

    <system.net>
    <mailSettings>
    <smtp from="[email protected]" deliveryMethod="PickupDirectoryFromIis">
    <network host="localhost" defaultCredentials="true"/>
    </smtp>
    </mailSettings>
    </system.net>


    Bye


    Ian
    Forums are about debating pleasantly not agreeing.

    Comment


    • #3
      Many Thanks

      No runtime error, I get a sending failed message, the created attachment is saved on the server ready for sending but no mail is sent, I am totally new to windows hosting so am unable to find where any error logs are located within the control pannel, this would give me a better idea of where it is falling over.

      I specified credentials as I thought this was why it wouldn't work when I tried it without, I thought it was because it needed account name and password to be able to send.

      Mail account is Valid, Also I don't even have the directory \inetpub\mailroot\BadMail

      i'm looking at your example and trying to see where to fit it in my script and the remove the problem/over complicated section.

      Thanks for your help so far

      Comment


      • #4
        Hi

        Will you cut and past the error message as it is hard to offer help without it.

        I have just run this and it works fine.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.IO;
        using System.Net.Mail;
        using System.Net.Mime;

        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
        sendEmail("c:\\Temp\\AdvertIPL.bmp");
        }

        private void sendEmail(string strattachment)
        {
        SmtpClient smtp = new SmtpClient("localhost");
        smtp.UseDefaultCredentials = true;

        MailMessage mailMessage = new MailMessage();
        mailMessage.From= new MailAddress("[email protected]","fred");
        mailMessage.To.Add(new MailAddress("[email protected]", "fred"));
        mailMessage.Subject = "Application submited";
        mailMessage.Body = "Please find attached the PDF filled.";

        Attachment myAttach = new Attachment(strattachment, MediaTypeNames.Application.Octet);
        myAttach.ContentDisposition.FileName="SampleFileNa me.bmp";
        mailMessage.Attachments.Add(myAttach);

        smtp.Send(mailMessage);
        }

        }
        and in web.config


        <system.net>
        <mailSettings>
        <smtp from="[email protected]" deliveryMethod="PickupDirectoryFromIis">
        <network host="localhost" defaultCredentials="true"/>
        </smtp>
        </mailSettings>
        </system.net>


        Bye

        Ian
        Forums are about debating pleasantly not agreeing.

        Comment


        • #5
          Hi

          I just thought, you do have an smtp service running don't you?

          Unfortunately I can't tell you how to check this as I am not on the same package as you.

          Bye

          Ian
          Forums are about debating pleasantly not agreeing.

          Comment


          • #6
            Many Thanks Once again

            The message is a script generated one saying "failure sending mail"

            I have changed the code to the below and the same thing happens, This code was working fine when it was first written and used on somone elses server and the credentials were set for thier own server.

            It must be a problem elswhere as there appears to be no obvious reason why it doesn't work.

            Thanks again for your help


            Code:
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using iTextSharp.text.pdf;
            using System.IO;
            using System.Net.Mail;
            
            namespace RetrieveForm
            {
                public partial class _Default : System.Web.UI.Page
                {
                    protected void Page_Load(object sender, EventArgs e)
                    {
                        string id = Session.SessionID;
                        string fileName = id + ".pdf";
                        FileStream sw = new FileStream(Server.MapPath(fileName), FileMode.CreateNew);
                        string file = Server.MapPath(fileName);
                        PdfStamper ps = null;
                        try
                        {
                            // read existing PDF document
                            PdfReader r = new PdfReader(
                                // optimize memory usage
                              new RandomAccessFileOrArray(Server.MapPath("application.pdf")), null
                            );
                             ps = new PdfStamper(r, sw);
                            // retrieve properties of PDF form w/AcroFields object
                            AcroFields af = ps.AcroFields;
                            // fill in PDF fields by parameter:
                            // 1. field name
                            // 2. text to insert
                            foreach (string key in Request.Form.Keys)
                            {
                                string v = Request.Form[key];
                                if (af.Fields.Contains(key))
                                {
                                    af.SetField(key, v);
                                }
                            }
                            // make resultant PDF read-only for end-user
                            ps.FormFlattening = true;
                            // forget to close() PdfStamper, you end up with
                            // a corrupted file!
                            ps.Close();
                            sw.Close();
                            sendEmail(file);
                            //File.Delete(file);
                            Response.Write("<br/><h3>Your application was successfuly sent!<br/>Thanks.</h3>");
                        }
                        catch (Exception err) { Response.Write(err.Message);  }
                        finally { if (ps != null) ps.Close(); }
                    
                    }
            
                    private void sendEmail(string attachment)
                    {
                        SmtpClient smtp = new SmtpClient("localhost");
                        smtp.UseDefaultCredentials = true;
            
                        MailMessage mailMessage = new MailMessage();
                        mailMessage.From = new MailAddress("[email protected]", "test");
                        mailMessage.To.Add(new MailAddress("[email protected]", "pat"));
                        mailMessage.Subject = "Application submited";
                        mailMessage.Body = "Please find attached the PDF filled.";
                        mailMessage.Attachments.Add(new Attachment(attachment));
                        smtp.Send(mailMessage);
                    }
            
                }
            }

            Comment


            • #7
              Morning,

              Given what you have said, the most likely reason is that there is no SMTP server running.

              As your code used to work and I know mine does, I would contact WHUK help desk to find out if your hosting package gives you access to such as server.

              I have a Windows VPS hosting and SmarterMail and SMTP was not enabled by default.

              Bye

              Ian
              Forums are about debating pleasantly not agreeing.

              Comment


              • #8
                Hi Pat,

                Can you try using replacing "smtp.0-o-0.co.uk" with "213.175.208.6" in your code as the mail server is a remote one. Let us know how that goes..
                Kind regards,
                Jack Daniel.

                Cloud Hosting || Managed Dedicated Server || Webhosting UK Knowledgebase

                Comment

                Working...
                X