SMTP server not sending mail - asp.net

I got Message that mail sent. But in Inbox there is no new email i have found that email in
server inetpub-> mailroot-> Queue
Please tell me the solution for this
Edited
public string btnSendmail()
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("myemail#itaxsmart.com","");
smtpClient.Host = "localhost";
smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("mailsendtol#itaxsmart.com");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = "Hello World" ;
smtpClient.Send(message);
return "Email successfully sent.";
}
catch (Exception ex)
{ return "Send Email Failed." + ex.Message;
}

Is your SMTP started? If not, start it. If it is already started, try restarting it.
Also turn on logging and see if that helps.

The moment asp.net manages to sent the message to the smtp server, you will get a success message. This only means that the message has reached your smtp server.
asp.net cannot control what your smtp server does with it.
Try using a public smtp server like gmail, if your local smtp server is giving trouble.

Add This code to your web.config
<system.net>
<mailSettings>
<smtp>
<network host="relay-hosting.secureserver.net"/>
</smtp>
</mailSettings>
</system.net>
and Remove This
smtpClient.Host = "localhost";
smtpClient.Port = 25;
Thanks,
Subhankar

Related

Email not sending using SMTP in ASP.net

I am try to send email through SMTP in ASP.net but email not sending and not giving any error. My code:
//create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//set the FROM address
mail.From = new MailAddress("aaa#abc.com");
//set the RECIPIENTS
mail.To.Add("bbb#abc.com");
//enter a SUBJECT
mail.Subject = "Set the subject of the mail here.";
//Enter the message BODY
mail.Body = "Enter text for the e-mail here.";
//set the mail server (default should be smtp.1and1.com)
SmtpClient smtp = new SmtpClient("smtp.1and1.com");
//Enter your full e-mail address and password
smtp.Credentials = new NetworkCredential("example#abc.com", "xxxxxxx");
//send the message
smtp.Send(mail);
And my code in web.config is
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Mail\" />
</smtp>
</mailSettings>
<defaultProxy enabled="true" />
</system.net>
Use breakpoint to display each parameter's value. Even if there is no error in the codes, parameter's value may be null or wrong. Before breakpoint, use try-catch as it below;
try
{
// Your codes
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
Check First you TLS Version.
Open your Site in IE(Internet Explorer) browser and go to page properties.
If your TLS 1.2 then Add below code before client.Send(Object);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
like as per bellow
System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient();
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
client.Send(oMail);
client.Dispose();
client = null;

Locally the mail is sent but when uploaded to server the mail is not sent in asp.net

I am using the following piece of code to send mail which is working perfectly when run on local host.I uploaded the site to the server and now when i try to achieve the same thing the mail won't get sent.Please help.
public void send_click(object sender, EventArgs e)
{
mail();
}
public void mail()
{
SmtpClient smtpClient = new SmtpClient();
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
MailAddress fromAddress = new MailAddress(tb_email.Text, tb_name.Text);
smtpClient.Host = "smtp.gmail.com";
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential("infoinnovative77#gmail.com", "***Password***");
smtpClient.Port = 587;
message.From = fromAddress;
message.To.Add(new MailAddress("info#mblube.com"));
// message.Subject = ddlSubject.SelectedItem.Text;
//message.CC.Add("taxistax#outlook.com");
message.IsBodyHtml = true;
StringBuilder msg = new StringBuilder();
msg.Append(tb_msg.Text);
msg.Append("<br/>");
msg.Append(tb_email.Text);
message.Body = msg.ToString();
//this code adds event handler to notify that mail is sent or not
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.Send(message);
if (smtpClient.EnableSsl == true)
{
string CloseWindow = "alert('Message Sent Successfully!');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem in Sending mail...try later!');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
System.Net.Mail.MailMessage mailMessage = e.UserState as System.Net.Mail.MailMessage;
if (e.Cancelled || e.Error != null)
{
Response.Write(e.Error.Message);
Response.Write(e.Error.StackTrace);
}
else
{
Response.Write("Email sent successfully");
}
}
If you have uploaded to an enterprise server:
1. Make sure your firewall is not blocking outgoing requests for the w3wp.exe which is the IIS Worker Process;
2. Verify which error message you are getting on the page as well as on the event viewer;
The way you have sent is almost the same as i have been sending but without the sendCompleted event so it shouldn't be a problem with the code. Also it is almost the same as this post and this one.
Instead of SmtpClient smtpClient = new SmtpClient();
Do
SmtpClient smtpClient = new SmtpClient("xx.xx.xxx.xx");
Where xx.xx.xxx.xx is the ip address of the domain.If you are using say godaddy server, it will provide the ip address for you to host your site.If you dont know the ip address simply ping the site name.Example if your site name is sum1.com then do ping sum1.com..it will show the ip address provided by your server.
We can't tell from here if the error is located within your application or your hosting environment.
There are a few steps you can take to narrow down the source of your problem.
First of all: check the Windows EventLog. If there are Asp.net errors or smtpsvc warnings, this might be related and give you a clue on what is failing!
To check if your application is per se working and not throwing exceptions while trying to send a mail change the mail settings in your web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\smtp"/>
</smtp>
</mailSettings>
</system.net>
If you now want to send a mail from within you application, it will get dropped into c:\smtp. If it is there your application is working fine and the problem is likely to be caused from your server, firewall or ISP.
Secondly fireup a cmd shell and check if your smtp settings are correct by telnetting onto it:
telnet mail.yourmailrpvider.net 25
If this throws an error, it is very likely that your firewall is blocking smtp traffic and you need to get in contact with your administrator
pls change the smtpClient.Host and credentials according to the server where you have uploaded the application.

Subscription Page in asp.net

I have a registration page I am sending an email from asp.net ,one mail to the registered user and other mail to the admin that too with different body...Ho can I achieve this? Also, User should give a confirmation link saying that you have subscribed and when the user clicks on the mail given link , a mail should fire to the admin saying that This User has been Registered.
http://www.webcodeexpert.com/2013/08/create-registration-form-and-send.html
You can use this link to achive the activation for user. But for sending mail to the admin, Use simple send email method.
Try this way, Send the from address and body details by yourself .
public void SendMail(string FromEmail, string Subject, string Body)
{
string ToEmail="YourAdmin#Gmail.com";
System.Net.Mail.MailMessage eMail = new System.Net.Mail.MailMessage();
eMail.From = new System.Net.Mail.MailAddress(FromEmail);
eMail.To.Add(ToEmail);
eMail.Subject = Subject;
eMail.IsBodyHtml = true;
eMail.Body = Body;
System.Net.Mail.SmtpClient SMTP = new System.Net.Mail.SmtpClient();
SMTP.Send(eMail);
eMail.Dispose();
}
Web.Config:
<system.net>
<mailSettings>
<smtp>
<network host="your stmp server" port="25" userName="your from email" password="your password"/>
</smtp>
</mailSettings>
</system.net>
Should be you have mail hosting server .
and if you want to send mail without page postback , See my little bit blog
These lines of code is used to send mail in aspx.cs page,make a try.if yo are using asp.net with c#
in aspx
<asp:CheckBox ID="CheckBoxMark1" runat="server" />
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
//other code
}
public void sendMail(String toemail)
{
MailMessage mail = new MailMessage();
mail.To.Add(toemail);
mail.From = new MailAddress("sender#gmail.com");
mail.Subject = "Subject";
mail.Body = " Your Contents in the mail";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("sender#gmail.com", "Password");
smtp.EnableSsl = true;
smtp.Send(mail);
}

how to send a confirmation email in asp.net, cannot get IIS pickup directory error

I want to send a confirmation email to user in registration page. The following code is the related part:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sc.Send(mail);
MESSAGE(true, "Sending mail is successful");
}
catch (Exception)
{
MESSAGE(true, "Sending mail is unsuccessful!");
}
But it does not send an email to related user. I have looked at forums and I added to web.config the following part:
<system.net>
<mailSettings>
<smtp from="myaddress#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="myaddress#gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
But anything didn't change. Then i have debugged and it enters into the try statement and when it comes to sc.Send(mail);, it drops to catch. Where is my mistake?
Additionally, during debug i realized that it shows this error: cannot get IIS pickup directory. I have controlled whether I have a smtp service or not from services but i couldn' t see this service. Is this error related to that service?
Thanks in advance.
Why not use GMAIL as the SMTP server, since you are defining a GMAIL account, by defining the host as "smtp.gmail.com", and port at 587?
Specific details here: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
Thanks for your helps.
I have solved this problem. I have changed my code as the following one:
try
{
SmtpClient sc = new SmtpClient();
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
string Ema = u.UserMail.ToString();
MailAddress gonderen = new MailAddress("admin#gmail.com", "Hello");
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.EnableSsl = true;
mail.To.Add(Ema);
mail.Subject = "Confirmation Message";
mail.From = gonderen;
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-9");
mail.Body = "<html><body>";
mail.Body += "</body></html>";
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(mail);
MESAJ(true, "Sending mail is successful");
}
catch (Exception)
{
MESAJ(true, "Sending mail is unsuccessful!");
}
and then i have set my web.config like the following :
<mailSettings>
<smtp deliveryMethod="Network"
from="admin#gmail.com">
<network defaultCredentials="false" host="smtp.gmail.com" port="587"
userName="admin#gmail.com" password="password" />
</smtp>
</mailSettings>
that works.. :)

How to use gmail SMTP in ASP.NET form

I am a ASP novice and am troubleshooting a form for work. None of us here are ASP experts as we use PHP. But I am on the bottom of PHP experience as well, mostly working with HTML/CSS alone. My current forms credentials look like:
rotected Sub SubmitForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsValid Then Exit Sub
Dim SendResultsTo As String = "email to"
Dim smtpMailServer As String = "email server"
Dim smtpUsername As String = "email username"
Dim smtpPassword As String = "password"
Dim MailSubject As String = "Form Results"
How would I go about making this form send to a Gmail address? I know I must include the port (587) somewhere, but cannot figure out where, as this form doesn't match the syntax of any other forms I have seen. Any help would be greatly appreciated!
You can add this in your web.config file
<system.net>
<mailSettings>
<smtp from="yourMailId#gmail.com ">
<network host="smtp.gmail.com" defaultCredentials="false"
port="587" userName ="yourmail#gmail.com" password="yourpassword" />
</smtp>
</mailSettings>
</system.net>
protected void SendMail()
{
MailMessage msg = new MailMessage();
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
try
{
msg.Subject = "Add Subject";
msg.Body = "Add Email Body Part";
msg.From = new MailAddress("Valid Email Address");
msg.To.Add("Valid Email Address");
msg.IsBodyHtml = true;
client.Host = "smtp.gmail.com";
System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("Valid Email Address", "Password");
client.Port = int.Parse("587");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicauthenticationinfo;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(msg);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
Create a System.Net.Mail.SmtpClient object, set the SMTP server info you are using.
Then create a System.Smtl.MailMessage with the message data and send it:
using (System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient()) {
using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("from*where.com", "to#where.com") {
IsBodyHtml = true,
Subject = "Subject text",
Body = messageBody,
}) {
mail.Send(message);
} // using
You can configure your SmtpClient in the constructor, we use web.comfig, so I don't have that code.
Google now blocks sign-ins from an app (even over SSL) to send an email. You have two options:
Enable less-secure apps for the account you are using; OR
Generate an app-password from Google (recommended)
For option 2, you will have to enable 2-factor authentication before you can generate an app pasword.
Here are the steps I took to get this working on my ASP.NET web app
How to configure an ASP.NET web app with Gmail SMTP
Try this.
Dim client As New Net.Mail.SmtpClient
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential("sender#gmail.com", "password")
client.Host = "smtp.gmail.com"
client.Port = 587
client.EnableSsl = True
client.Send("sender#gmail.com","reciever#gmail.com","subject","body")
If you're using 2-step verification, don't forget to generate an application specific password and use that, not the password you use to log on to Gmail.
(Sorry I can't add this as a comment. Not enough rep at the time of posting this.)
There's no shortage of tutorials on how to send an email from within .NET.
Essentially you want a System.Net.Mail.SmtpClient object to interact with the SMTP server, a System.Net.Mail.MailMessage object to hold the message data, and configuration data in your config file to direct the client on how/where to send the message.

Resources