Abhishek Biswas

Abhishek Biswas

  • NA
  • 14
  • 2.4k

Problem Sending E-mail in C# , Using smtp in Gmail...!!

Mar 22 2015 11:25 AM
I am using following code to send email. The Code works correctly in my local Machine. But not on Godaddy server. Kindly Help.
C# Code:
 using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Globalization;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;

public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
//Specify senders gmail address
string SendersAddress = "[email protected]";
//Specify The Address You want to sent Email To(can be any valid email address)
string ReceiversAddress = "[email protected]";
//Specify The password of gmial account u are using to sent mail(pw of [email protected])
const string SendersPassword = "Password";
//Write the subject of ur mail
string subject = "ENQUIRY FORM";
//Write the contents of your mail
string body = "Subject : " + txtSub.Text + "\nName : " + txtName.Text + "\nMobile : " + txtMobile.Text + "\nE-mail : " + txtEmail.Text + "\nField : " + drpdwnlstFields.Text + "\nMessage : " + multtxtMessage.Text;

try
{
//we will use Smtp client which allows us to send email using SMTP Protocol
//i have specified the properties of SmtpClient smtp within{}
//gmails smtp server name is smtp.gmail.com and port number is 587
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(SendersAddress, SendersPassword),
Timeout = 999999
};

//MailMessage represents a mail message
//it is 4 parameters(From,TO,subject,body)
MailMessage message = (new MailMessage(SendersAddress, ReceiversAddress, subject, body));
/*WE use smtp sever we specified above to send the message(MailMessage message)*/
smtp.Send(message);
lblResponse.Text = "Your query has been submitted successfully, We will get back to you shortly.";
}

catch (Exception ex)
{
lblResponse.Text = "Sorry server busy, Please try again later.";
}
Response.Redirect("Home");

}

protected void btnReset_Click(object sender, EventArgs e)
{
Response.Redirect(Request.Url.PathAndQuery, true);
}

}

WebConfig Code :
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>

<appSettings/>
<connectionStrings/>
<system.web>

<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<customErrors mode="RemoteOnly" defaultRedirect="Home.aspx"/>
<compilation debug="true" targetFramework="4.5">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="4.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>

</system.webServer>
<system.net>
<mailSettings>
<smtp deliveryMethod ="Network" from="[email protected]">
<network
host="smtp.gmail.com"
port="25"
defaultCredentials="false"
enableSsl="true"
/>
</smtp>
</mailSettings>
</system.net>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
</configuration>

 

Answers (1)