locked
Webmail handler RRS feed

  • Question

  • User1481030129 posted

    Dear,

    I'm trying to send an email from a static email address using the Webmail handler, but it seems that the email is not send to the static email address that I would like to use.

    Can someone help me with this? Objective is that the customer is filling-in the form and that the application is sending the email to a support email address and in parallel sends an email towards the customer in cc.

    Please find hereby my code:

    Email Class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;  

    namespace Vidimus.Models
    {
        public class EmailSupport
        {
            [DataType(DataType.EmailAddress)]
            [Display(Name = "Customer Email")]
            public string FromEmail { get; set; }
            [Display(Name = "Subject")]
            public string EmailSubject { get; set; }
            [Display(Name = "Body")]
            [DataType(DataType.MultilineText)]
            public string EMailBody { get; set; }

    Email Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Helpers;
    using Vidimus.Models;


    namespace Vidimus.Controllers
    {
        public class EmailController : Controller
        {
            // GET: Email
            public ActionResult SendEmail()
            {

                return View();
            }

            [HttpPost]
            public ActionResult SendEmail(EmailSupport obj)
            {

                try
                {
                    //Configuring webMail class to send emails  
                    //gmail smtp server  
                    WebMail.SmtpServer = "smtp.server.com";
                    //gmail port to send emails  
                    WebMail.SmtpPort = 587;
                    WebMail.SmtpUseDefaultCredentials = true;
                    //sending emails with secure protocol  
                    WebMail.EnableSsl = true;
                    //EmailId used to send emails from application  
                    WebMail.UserName = "email address";
                    WebMail.Password = "security";

                    //Sender email address.  
                    WebMail.From = "domain@domain.com";

                    //Send email  
                    WebMail.Send(to:"support@support.com", from: obj.FromEmail, subject: obj.EmailSubject, body: obj.EMailBody, isBodyHtml: true);
                    ViewBag.Status = "Email Sent Successfully.";
                }
                catch (Exception)
                {
                    ViewBag.Status = "Problem while sending email, Please check details.";

                }
                return View();
            }
        }
    }

    The webpage is working correctly, but when sending the page, it gives me the catch error "problem while sending email, Please check details.".

    When I remove the first "support@support.com" within the "WebMail.Send(to:" option, the webmail handler works correctly, but sends the mail to the FromEmail address.

    Wednesday, June 14, 2017 6:27 AM

Answers

  • User753101303 posted

    Hi,

    For now my guess would be that your mail server is not allowed to send a mail with a from address that doesn't match the server domain ? The other address does match the domain handled by your mail server ?

    Generally speaking I would suggest to always start from the actual error ie look at the actual exception details by using something such as :

     catch (Exception ex)
                 {
                     ViewBag.Status = "Problem while sending email, Please check details.";
                     // Log ex.ToString() somewhere so that you can track what happens exactly (or look at it using the debugger etc...)
                 }

    It is then usually much easier to fix the error as you know which exact error you have.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 14, 2017 9:08 AM
  • User753101303 posted

    Ok or for now just use :

          catch (Exception ex)
                 {
                     ViewBag.Status = ex.ToString();
    
                 }

    and post the message you got or the best English translation you can.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 14, 2017 5:53 PM
  • User1481030129 posted

    Hello Patrice,

    Thank you for your reply and the code provided.

    I added this to within the Email controller and the message indicates that the provider doesn't allow to send emails with another domain name extension.
    After adding the domain name of my provider, the email is send & delivered correctly.

    The code above clarified a lot !

    Thanks again for you support!

    Bartvn

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 15, 2017 3:54 PM

All replies

  • User753101303 posted

    Hi,

    For now my guess would be that your mail server is not allowed to send a mail with a from address that doesn't match the server domain ? The other address does match the domain handled by your mail server ?

    Generally speaking I would suggest to always start from the actual error ie look at the actual exception details by using something such as :

     catch (Exception ex)
                 {
                     ViewBag.Status = "Problem while sending email, Please check details.";
                     // Log ex.ToString() somewhere so that you can track what happens exactly (or look at it using the debugger etc...)
                 }

    It is then usually much easier to fix the error as you know which exact error you have.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 14, 2017 9:08 AM
  • User1481030129 posted

    Hi PatriceSC,

    Thank you for your reply.
    Even when I use the same from address, it doesn't seem to work.

    What I would like to do is to fix the "To:" address, so when the form is filled-in by a customer, the mail is send to a dedicated functional mailbox and at the same time the content is also send to the "CC" email address provided by the customer.

    How can I export the actual exception you are referring to towards a log file?

    Thank you for you support!

    Bart

    Wednesday, June 14, 2017 2:09 PM
  • User753101303 posted

    Ok or for now just use :

          catch (Exception ex)
                 {
                     ViewBag.Status = ex.ToString();
    
                 }

    and post the message you got or the best English translation you can.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 14, 2017 5:53 PM
  • User1967761114 posted

    Hi bartvn,

    According to your description, there has more possibilities to occurred the error, such as the port error, couldn’t using SSL, SMTP error and so on.

    You could show the detail exception message in this thread to help us resolve this issue.  

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 15, 2017 9:48 AM
  • User1481030129 posted

    Hello Patrice,

    Thank you for your reply and the code provided.

    I added this to within the Email controller and the message indicates that the provider doesn't allow to send emails with another domain name extension.
    After adding the domain name of my provider, the email is send & delivered correctly.

    The code above clarified a lot !

    Thanks again for you support!

    Bartvn

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 15, 2017 3:54 PM