0

I have developed asp.net application which sends email to the domain of my company. Example : ex@companydomain.com, but I'm not able to send it to any other domains, like ex@gmail.com or ex1@hotmail.com. Do I need to make changes in the smtp server or in my code? Please let me know. I'm pretty new to .net.Below is the code which I used :

        MailMessage Msg = new MailMessage();
        var MailResult = false;

        var EmailUser = RFSvc.Users.Where(u => u.Email == email).SingleOrDefault();
        if (EmailUser != null)
        {

            Msg.From = new MailAddress("noreply@example.com", "Company name");
            Msg.Subject = "Password Recovery";
            Msg.IsBodyHtml = true;
            Msg.Body = "Your User Name is: " + EmailUser.Username + "<br /> Your password is: " + EmailUser.Password;
            Msg.To.Add(email);

            SmtpClient MailClient = new SmtpClient();

            MailClient.UseDefaultCredentials = false;
            MailClient.Host = "mail.example.com";
            MailClient.Port = 25;
            MailClient.Credentials = new NetworkCredential("username","password");
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            MailClient.EnableSsl = false;

            try
            {
                MailClient.Send(Msg);

                MailResult = true;
            }
            catch (Exception)
            {
            }
        }
  • Check [this](https://stackoverflow.com/questions/18326738/how-to-send-email-in-asp-net-c-sharp) to send mail in `.net`. Also gmail email ID should be like : example@gmail.com and not example@ex.gmail.com. It would help others to guide you, if you could post your code. – Shaiju T Dec 01 '17 at 05:00
  • @stom thank you for reply. I have made the necessary edit to the question. I also got to know that my code is correct, and I need to do cofigurations in smtp server. Do you know what all configurations I need to do? – Sachin Gotal Dec 01 '17 at 06:08
  • MailClient.EnableSsl = false; Change to MailClient.EnableSsl = True; – ILYAS PATEL Dec 01 '17 at 06:20

2 Answers2

0

Hey can you provide which port are you using ? so from that i can get more idea where you stuck

The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.

As suggested by Trimantra Software Solution:

try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("your_email_address@gmail.com");
            mail.To.Add("to_address");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            MessageBox.Show("mail Send");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

Hope so it can helpful to you

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
ILYAS PATEL
  • 278
  • 1
  • 12
  • Please Change this setting MailClient.EnableSsl = false; Change to MailClient.EnableSsl = True; and also try if that port is not work then 587 default and try to debug code – ILYAS PATEL Dec 01 '17 at 06:20
  • Hi @ILYAS, the code works fine. It sends the mail to my company domain. It just doesn't send to other domains. I think I need to configure the SMTP server. Do you know what configurations I need to do? – Sachin Gotal Dec 01 '17 at 06:39
  • Also I have tried enabling SSL and changing he port to 587 it works but doesn't send to other domains – Sachin Gotal Dec 01 '17 at 06:40
  • Are you sending mail from your domain ?? or google please can your configuration your Mail server from your domain and if you use gmail to sending mail then it EnableSsl =true and if you sending from private domain then made EnableSsl =False – ILYAS PATEL Dec 01 '17 at 06:47
  • Hi I'm sending with my mail server – Sachin Gotal Dec 01 '17 at 10:10
0

I am able to send mail from custom domain to gmail.com , Here is the code:

var fromAddress = new MailAddress("example@mydomain.com", "From Name");
var toAddress = new MailAddress("example@gmail.com", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";            

MailMessage message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;


var smtp = new SmtpClient
{
  Host = "smtp.mydomain.com",
  Port = 25,
  EnableSsl = true,
  DeliveryMethod = SmtpDeliveryMethod.Network,
  UseDefaultCredentials = false,
  Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
  Timeout = 20000
};            

smtp.Send(message);

Note:

If it still doesn't work for you , contact your domain registrar or mail provider and get smtp mail settings , or try changing port 465 , 587 , or try commenting out single property of SmtpClientclass at a time to check whether it works.

References:

1.

2.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • What exactly didn't work ? did you get any error ? Do you have exception handling in place in your code ? – Shaiju T Dec 01 '17 at 10:14
  • Hi @stom I tried sending mail to my company domain it works perfectly. But only when I try to send to other domain like google, yahoo it gives this exception :Mailbox unavailable. The server response was: 5.7.1 Unable to relay – Sachin Gotal Dec 01 '17 at 10:46
  • @SachinGotal , If you trying to send mail from site which is hosted in `IIS` then [this](https://stackoverflow.com/questions/3165721/mailbox-unavailable-the-server-response-was-5-7-1-unable-to-relay-for-abcxyz) may help you. – Shaiju T Dec 01 '17 at 11:23