Email not sending using SMTP in ASP.net - 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;

Related

Email not working from Godaddy Server

First of all, I hope to be at the right place, I apologize in advance if this is not the case, please re-orient myself.
So here I have a worry, I have a website www.artipik.com with two forms, online quote and contact form.
My website is hosted by godaddy and servers were updated summers there for 15 days and my form are no longer work, which put me in a very problematic position. I'm a graphic designer not a coder, hence my difficulty changed the code.
I had the godaddy technical service and he gave it to me I think part of the solution. I found the place to change the code, but I can not seem to fit, I'm testing, I no further error messages but I do not receive any email I send.
My current ASP code is as follows:
<%
'-----version CDONTS-----
Set Mail = Server.CreateObject("CDONTS.NewMail")
Mail.BodyFormat = 1 '0:html/1:plain text
Mail.MailFormat = 1 '0:MIME/1:plain text
Mail.From = envoyeur
Mail.To = receveur
if copie<>vbnullstring then Mail.Bcc = copie
if sujet=vbnullstring then sujet="Formulaire de contact Artipik.com"
Mail.Subject = sujet
Mail.Body = message
Mail.Send
set Mail=Nothing
'response.cookies("email")=envoyeur
%>
and the new code that I have to adapt is the following (Using CDOSYS to Send Email from Your Windows Hosting Account):
// language -- C#
// import namespace
using System.Web.Mail;
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Thank you for your help.
Julian
System.Web.Mail.MailMessage is obsolete. You should use System.Net.Mail.MailMessage instead. It works almost exactly the same (warning! untested):
private void SendEmail()
{
using (var message = new MailMessage())
{
message.From = new MailAddress("emailaddress#domainname");
var toAddress = new MailAddress("emailaddress#domainname");
message.To.Add(toAddress);
message.Subject = "Test email subject";
message.Body = "Sent at: " + DateTime.Now;
message.Priority = MailPriority.High;
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
}
Add this to your web.config:
<configuration>
<system.net>
<mailSettings>
<smtp from="emailaddress#domainname">
<network host="your-smtp-host-address" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Recently I've had the same problem, could solve it throug the next way:
in web.config:
<system.net>
<mailSettings>
<smtp from="contacto#mydomain.com">
<network host="relay-hosting.secureserver.net" password="MyPass" port="25" enableSsl="false" defaultCredentials="false" userName="contacto#mydomain.com"/>
</smtp>
</mailSettings>
</system.net>
in codebehind:
MailMessage m = new MailMessage();
m.IsBodyHtml = true;
m.Body = "My Message";
m.To.Add("jhon.doe#yahoo.com");
m.Subject = "Go Daddy Emails Go Live!";
SmtpClient smtp = new SmtpClient();
smtp.Send(m);

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);
}

Unable to send in asp.net application

I want to send an e-mail, but it shows the following error:
try
{
string filename = Server.MapPath("~/App_Data/FeedBack.txt");
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##Name##", txtName.Text);
mailbody = mailbody.Replace("##Email##",txtEmail.Text);
mailbody = mailbody.Replace("##contact##",txtContact.Text);
mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Responce from website";
myMessage.Body = mailbody;
myMessage.From = new MailAddress("amitverma0511#gmail.com","Amit Kumar");
myMessage.To.Add(new MailAddress("amitverma1150#gmail.com", "Amit Verma"));
SmtpClient mysmptclient = new SmtpClient("localhost");
mysmptclient.Host = "smtp.gmail.com";
mysmptclient.Port = 587;
mysmptclient.Credentials = new System.Net.NetworkCredential("amitverma0511#gmail.com", "Myemailpassword");
mysmptclient.Send(myMessage);
formatTable.Visible = false;
lblMail.Text = "Your Feedback has been sent successfully";
lblMail.ForeColor = Color.Green;
}
catch(Exception expt)
{
lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message;
lblMail.ForeColor = Color.Red;
}
My web.config file looks like this:
<system.net>
<mailSettings>
<smtp from="amitverma0511#gmail.com">
<network host="smtp.gmail.com" port='587' password="myemailpass" userName="amitverma0511#gmail.com"/>
</smtp>
</mailSettings>
</system.net>
As far as I know, you are overwriting your web.config settings by using localhost (your IIS) as a SMTP Server.
If you have not configured your IIS to serve as a SMTP server this naturally will not work.
But, if you want to use localhost and not the google servers just use this 3 lines:
SmtpClient MySmtpClient = new SmtpClient();
MySmtpClient.UseDefaultCredentials = true;
MySmtpClient.Send(mailMessage);
You should remove the string localhost from SmtpClient mysmptclient = new SmtpClient("localhost"); and it should work, if all your other settings are correct.
Further information can be found here.

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.. :)

SMTP server not sending mail

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

Resources