You must Sign In to post a response.
  • Category: .NET

    Send Email functionality is not working at the server machine

    Hello Guys,
    I have created a Windows Application using C# and implemented the Send email code for sending emails. The code for sending email is working fine in the local but not sending email to a machine which is located in other country. Is the send email functionality dependent on the Country? I am using gmail as the intermediate server for sending email.
    Below is the code which I am using for sending email:

    private bool MailSend(string fromEmail, string toEmail, string subj, string content, string userName, string password, string smtpServer, int portNumber)
    {
    bool isSent = false;
    try
    {
    var msg = new MailMessage(fromEmail, toEmail, subj, content);
    var smtp = new SmtpClient(smtpServer)
    {
    Port = portNumber,
    EnableSsl = true,
    Credentials = new NetworkCredential(userName, password)
    };
    smtp.Send(msg);
    isSent = true;
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    return isSent;
    }

    And then I am calling like this:

    bool bSent = MailSend("pawansoftit@gmail.com", txtToEmail.Text.Trim(), txtSubject.Text.Trim(), txtMessage.Text.Trim(), "pawansoftit@gmail.com", "xxxxxxxxxx", "smtp.gmail.com",587));
    if (bSent)
    {
    MessageBox.Show("mail sent successfully", "send email");
    lblSendingEmail.Visible = false;
    this.Close();
    }
    else
    MessageBox.Show("there might be some issue in sending email.", "send email");

    The server machine is using Windows 7 as the Operating system. Do we need to do any settings in the Windows 7 machine. I tried by disabling all the antivirus and Windows Firewalls but still the same problem.
    Do we need to enable the SMTP in the Windows machine for sending email? If anyone has any idea, please share it.
    Thanks in advance.
  • #760137
    Hello Pawan Awasthi,

    I am using the below code for sending the email from Gmail. It is working :

    MailMessage mailmesssage = new MailMessage();
    mailmesssage.From = new MailAddress("username@gmail.com", "Full Name");
    mailmesssage.To.Add(TextBoxEmailAddress.Text);
    mailmesssage.Subject = "Test Mail";
    mailmesssage.Body = "Enter your body";
    if (FileUpload1.HasFile) // Optional code for attachment
    {
    string fn = Path.GetFileName(FileUpload1.PostedFile.FileName);
    mailmesssage.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, fn));
    }

    //G-mail settings
    try
    {
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    smtpClient.Port = 587;
    smtpClient.Credentials = new NetworkCredential("username@gmail.com", "password");
    smtpClient.EnableSsl = true;
    smtpClient.Send(mailmesssage);
    Response.Write("<script>alert('" + Server.HtmlEncode("We have received your request." + Environment.NewLine + "We will get back you soon.") + "')</script>");
    }
    catch (Exception ex)
    {
    Response.Write("<script>alert('" + Server.HtmlEncode(ex.Message) + "')</script>");
    }

    I have used this code on below link. Which is working fine on internet. :
    http://verticalsolutions.co.in/Feedback.aspx

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


  • Sign In to post your comments