user3933083
user3933083

Reputation: 11

Mail sending code works at local but not on server in asp.net mvc

My mail sending code using google stmp in asp.net MVC works fine at local server but gives internal server error 500 at remote server sage.arvixe.com

Here Is my Controller Code:

    string email = "**@**.com";
    string password = "***";

    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("my.smtp.server.com");

    mail.From = new MailAddress(email, "Company");
    mail.To.Add("[email protected]");
    mail.Subject = "Feedback";

    string body = "CompanyName " + data.CompanyName + "<br/> Contact Person Name " + data.ContactPersonName + "<br/> Designation " + data.Designation + "<br/> EmailID " + data.EmailID + "<br/> MobileCellNo " + data.MobileCellNo + "<br/> OfficePhoneNo " + data.OfficePhoneNo + "<br/> FaxNo " + data.FaxNo + "<br/> Address " + data.Address + "<br/> CityState " + data.CityState + "<br/> PostalCode " + data.PostalCode + "<br/> Country " + data.Country + "<br/> Description " + data.Description + "";
    mail.Body = body;
    mail.IsBodyHtml = true;
    //SmtpServer.UseDefaultCredentials = false;
    SmtpServer.Port =25;
    SmtpServer.Credentials = new System.Net.NetworkCredential(email, password);
    //SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);

Upvotes: 1

Views: 1874

Answers (2)

Balaram Hufet
Balaram Hufet

Reputation: 1

hi try this code it is working

[HttpPost]
    public ActionResult Form(string receiverEmail, string subject,string message)

{
   try
        {

            if (ModelState.IsValid) {
                var senderemail = new MailAddress("[email protected]", "Demo Mail");
                var receiveremail = new MailAddress(receiverEmail, "Receiver");

                var password = "xxxxx";
                var sub = subject;
                var body = message;

                var smtp = new SmtpClient {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(senderemail.Address, password)

                };

                using (var mess = new MailMessage(senderemail, receiveremail)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    smtp.Send(mess);
                }

                return View();
            }
        }
        catch(Exception){
            ViewBag.Error = "There is some thing went Wrong";
        }
        return View();
    }
}

Upvotes: 0

Oliboy50
Oliboy50

Reputation: 2731

I'm not sure about that, but it seems that you have to enable SSL to use properly this google service.

Maybe you'll find some more help in

this tutorial : http://www.dustinhorne.com/post/Sending-Email-With-Google-Mail-and-ASPNET

or this topic : https://productforums.google.com/forum/#!topic/apps/fg5RNrQhdFY

Upvotes: 0

Related Questions