Could not send email after creating Programmatically

Discussions on webmail and the Professional version.
Post Reply
maniish
Posts: 3
Joined: Sat Jun 28, 2014 3:01 pm

Could not send email after creating Programmatically

Post by maniish »

I have creating email as below in .NET. But in last part when I am sending using SendGrid, it do not send email and goes in badmail. Can somebody help me as it shows in mailbox but do not forward to forwarding setup.

Code: Select all

string sPostoffice = "xyz.com";

            MailEnable.Administration.Mailbox oMailbox = new MailEnable.Administration.Mailbox();
            MailEnable.Administration.Login oLogin = new MailEnable.Administration.Login();

            string sMailboxName = txtMailBox.Text;
            string sPassword = "password";
            string sRights = "USER";// USER – User, ADMIN – Administrator

            oLogin.Account = sPostoffice;
            oLogin.LastAttempt = -1;
            oLogin.LastSuccessfulLogin = -1;
            oLogin.LoginAttempts = -1;
            oLogin.Password = "";
            oLogin.Rights = "";
            oLogin.Status = -1;
            oLogin.UserName = sMailboxName + "@" + sPostoffice; // If the login does not exist we need to create it

            if (oLogin.GetLogin() == 0)
            {
                oLogin.Account = sPostoffice;
                oLogin.LastAttempt = 0;
                oLogin.LastSuccessfulLogin = 0;
                oLogin.LoginAttempts = 0;
                oLogin.Password = sPassword;
                oLogin.Rights = sRights;
                oLogin.Status = 1; // 0 – Disabled, 1 – Enabled
                oLogin.UserName = sMailboxName + "@" + sPostoffice;
                if (oLogin.AddLogin() != 1)
                // Error adding the Login
                {
                    MessageBox.Show("Error in creating email.");
                    return;
                }

            }


            // Now we create the mailbox
            oMailbox.Postoffice = sPostoffice;
            oMailbox.MailboxName = sMailboxName;
            // Set the Redirect Address if applicable
            oMailbox.RedirectStatus = 2;

            string[] Emails = txtCustomerEmail.Text.Split( new char[] { ',',';' });

            string Redirect = "[SMTP:forwardemail@email.com];";
            foreach (string email in Emails)
            {
                Redirect += ";[SMTP:" + email + "]";
            }
            oMailbox.RedirectAddress = Redirect;

            oMailbox.Size = 0;
            oMailbox.Limit = -1; // -1 – Unlimited OR size value (in KB)
            oMailbox.Status = 1;
            if (oMailbox.AddMailbox() != 1)
            {   // Failed to add mailbox
                MessageBox.Show("Error in creating email. - AddMailBox");
                return;
            }

            MailEnable.Administration.AddressMap oAddressMap = new MailEnable.Administration.AddressMap();
            oAddressMap.Account = sPostoffice;
            oAddressMap.DestinationAddress = "[SF:" + sPostoffice + "/" +
            sMailboxName + "]";
            oAddressMap.SourceAddress = "[SMTP:" + sMailboxName +
            "@xyz.com]";
            oAddressMap.Scope = "0";
            if (oAddressMap.AddAddressMap() != 1)
            {   // Failed to add Address Map for some reason!
                MessageBox.Show("Failed to add Address Map for some reason!");
                return;
            }
            

            MessageBox.Show("MailBox Created");

            // Create the email object first, then add the properties.
            SendGridMessage myMessage = new SendGridMessage();

            myMessage.AddTo(oLogin.UserName);
            myMessage.From = new MailAddress("xyz@email.com", "Manish");
            myMessage.Subject = "Test email ";
            myMessage.Text = @"Hi,    

This is test email from us.
";

            // Create a Web transport, using API Key
            var transportWeb = new Web("SendgridKey");

            // Send the email.
            transportWeb.DeliverAsync(myMessage);

            MessageBox.Show("Mail Sent");

Post Reply